0

I have a windows form that I am displaying as non-modal dialog. As a result I am calling the overloaded Show(IWin32Window owner) method on that form. Only problem is that one of the parent forms that I want to use here is not accessible in the project. As a result I want to load it using reflection using something like code below.

var frm = Assembly.GetEntryAssembly().GetTypes().Where(f => f.Name == "ParentForm").FirstOrDefault();

However this give following compilation errors.

The best overloaded method match for

'System.Windows.Forms.Form.Show(System.Windows.Forms.IWin32Window)' has some invalid arguments

Argument 1: cannot convert from 'System.Type' to 'System.Windows.Forms.IWin32Window'

Any suggestions on how to achieve this?

5
  • With reflection, you're getting the Type corresponding to ParentForm, not an actual ParentForm instance. Commented Nov 21, 2013 at 21:51
  • is it possible to get it using reflection? Commented Nov 21, 2013 at 21:51
  • 2
    You cannot get instances using reflection. It is used only for discovering a Type and related properties Commented Nov 21, 2013 at 21:52
  • 1
    To get what, exactly? An existing instance of it, or a new instance of it? The latter is easy, Activator.CreateInstance(frm). For the former, you need to know how to get a reference to it, it can't just be done automatically. Commented Nov 21, 2013 at 21:52
  • 1
    @Tim: For forms, it can. Commented Nov 21, 2013 at 21:58

1 Answer 1

2

You probably will want to search through the Application.OpenForms collection.

Form f = Application.OpenForms.Where(x => x.GetType().Name == "ParentForm").FirstOrDefault();
Sign up to request clarification or add additional context in comments.

2 Comments

That's a great idea. By reflection I first searched for the type and then looked for that type in all open forms from the API you mentioned. Problem solved... You are the man!!!!!!!!!!!
@PaulSnow: I think I would have called GetType() on each Form in the collection, and checked the name. Getting the name of the type of each form is a bit less work for .NET than getting the name and other metadata of every type. But glad you got it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.