Open form dynamically

dakota97

Centurion
Joined
Nov 14, 2003
Messages
116
Location
Pennsylvania
Hi all,

What I'm trying to do is dynamically add menu items to a menubar. I've got the code working, but now I'm trying to add an event handler to the menuitem so that when the user clicks on it, it opens the correct form. The problem is, the program doesn't know that the form exists. So, while creating the menuitem, I've inserted the name of the form that it should open into the tag property.

The problem that I'm having is converting the tag value into a form, so that the correct form is opened. I've tried the following code (setting the tag value to the name of one of my forms), but it's not working for me, as the SelectedForm is showing up as null during runtime

Code:
private void OpenForm(object sender, System.EventArgs e)
        {
            MenuItem ClickedMenu = sender as MenuItem;
            ClickedMenu.Tag = "Splash";
            Form SelectedForm = new Form();
            SelectedForm = ClickedMenu.Tag as Form;
            SelectedForm.ShowDialog();
            
        }

Is there a way the I can convert (or cast) the ClickedMenu.Tag to a form so that I can show the correct form?

Thanks in advance,

Chris
 
are these singleton forms??? meanin you can only have one form of one type open at a time??? I have a neat solution for MDI apps if this is the case.
 
I don't quite understand what your trying to achieve here with the code you posted you are applying the tag as part of the method anyway so you could just specify the form anyway. Unless you simply put that in this method so we would understand how you are doing it.

What exactly is Splash? Is it a Form with the name splash that has already been create, is it a class that Inherits from Form or none of the above?

The reason your code doesn't work is because you are trying to take a string (the ClickedMenu.Tag property) and cast it into a Form, the .Net framework has no concept of how to achieve this.

If Splash is infact a form that has been declared already along with some other forms, then if you create a public form collection, you can loop through them looking for the form with the name Splash, something like this...

C#:
for(int i = 0; i < myForms.Length; i++)
{
    if(myForms[i].Name == ClickedMenu.Tag)
    {
        // found form so show it
        myForms[i].Show()
        break;
    }
}
NB. 'True' programmers would probably do a while loop as opposed to using a for loop and breaking early, I just personally find this method easier.

Hope that helps, but its possible I got the wrong end of the stick.
 
I understand your code, and it would work great. Only one problem. The forms are not previously declared, therefore I cannot create a collection to loop through. The program has now knowledge of what forms exist.

Let me try to explain things a little better. Overall, I'm trying to make the program as modular as possible so that future updates are easier for me. The entire menu system is generated dynamically by reading entries in my database. From there, each menuitem's click event points to the OpenForm method in my first post. What I want to be able to do in the future is create a new form, add a menu item to the database, pacakge everything up and send it out as an update.

I have a completed version of the program that I wrote in VB.Net, and I am now going through and converting all of the code to C#. Originally, since this project was a learning project, any updates that I made included modifying the existing code, and then packaging up the entire program once again and distributing it. Since the setup files are approximately 8.5mb, this isn't a very efficient approach.

I've spoken with a couple people, and the topic of late binding has come up. I'm not really sure how that works, but I'm assuming that is what I need to do in order to get this to work properly. Can anyone give me some input here?

Thanks again,

Chris
 
suppose you have two form types -

public class ThisForm:Form {}

and

public class ThatForm:Form {}


now, in the Main form of your app, you have two menuItems -

ShowThisFormMenuItem and ShowThatFormMenuItem

put this code in your form:

protected override OnLoad(EventArgs e)
{
ShowThisFormMenuItem.Tag = typeof(ThisForm);
ShowThatFormMenuItem.Tag = typeof(ThatForm);
base.OnLoad(e);
}

wire both menuitems to this event handler-

protected void ShowFormMenuItemClick(object sender, System.EventArgs e)
{
using (Form frm = Activator.CreateObject(((sender as MenuItem).Tag) as Type) as Form)
frm.ShowDialog();
}
 
Whilst that would work for creating an instance of the forms, it requires the main form to be edited whenever a new form type is added to the application. Using a plugin-in system as PlausiblyDamp suggests would eliminate this need.
 
First off, thanks for the replies.

All 3 of you have given good examples and ideas. After looking at it, I think the plug-in example is the way that I want to go with this. I'm going to take a look at the examples in more depth later on tonight, and I'll get back to everyone on the outcome.

Again, thanks for the suggestions.

Chris
 
Cags said:
Whilst that would work for creating an instance of the forms, it requires the main form to be edited whenever a new form type is added to the application. Using a plugin-in system as PlausiblyDamp suggests would eliminate this need.
hmmm. . . I was thinking that adding a new menu item would be editing your form. . .

Didn't really look at the plug-in suggestion.
 
Back
Top