dakota97 Posted January 11, 2006 Posted January 11, 2006 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 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 Quote if(computer.speed == "slow") { hamster.feed(); } if(computer.speed == "really slow") { hamster.kill(); BuyNewHamster(); }
Joe Mamma Posted January 11, 2006 Posted January 11, 2006 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. Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Cags Posted January 11, 2006 Posted January 11, 2006 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... 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. Quote Anybody looking for a graduate programmer (Midlands, England)?
dakota97 Posted January 11, 2006 Author Posted January 11, 2006 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 Quote if(computer.speed == "slow") { hamster.feed(); } if(computer.speed == "really slow") { hamster.kill(); BuyNewHamster(); }
Administrators PlausiblyDamp Posted January 11, 2006 Administrators Posted January 11, 2006 http://www.xtremedotnettalk.com/showthread.php?t=49597 and then http://www.xtremedotnettalk.com/showthread.php?t=70920 might be worth having a look at, the 1st gives a good example of how to develop a plugin based system and the 2nd extends it to include GUI elements. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Joe Mamma Posted January 11, 2006 Posted January 11, 2006 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(); } Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Cags Posted January 11, 2006 Posted January 11, 2006 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. Quote Anybody looking for a graduate programmer (Midlands, England)?
dakota97 Posted January 11, 2006 Author Posted January 11, 2006 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 Quote if(computer.speed == "slow") { hamster.feed(); } if(computer.speed == "really slow") { hamster.kill(); BuyNewHamster(); }
Joe Mamma Posted January 11, 2006 Posted January 11, 2006 Whilst that would work for creating an instance of the forms' date=' 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.[/quote'] hmmm. . . I was thinking that adding a new menu item would be editing your form. . . Didn't really look at the plug-in suggestion. Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.