Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening Form2 from Form1 1

Status
Not open for further replies.

bb2003

Programmer
Dec 15, 2003
1
GB
I have 3 menu items in Form1 and when I select any of them I want to open Form2. However, I need to show different label captions and make some contols visible or invisible depending on the menu item I select. I think I need to pass a flag either 0,1,2 depending on the menu selected and then show the form depending on the flag. I am currently using this in the menu click event of Form1 to invoke Form2

Form2 frm = new Form2();
frm.OnOkClickedEvent += new Form2.OnOkClicked(HandleEventSet);
frm.ShowDialog(this);

This currently handles the selections made in Form2. I am very new to C# so please be gentle.

Thanks
 
Hi,

The trick is to work the .NET way and use a custom enumeration. This makes your code much more readable.

For instance, Before the declaration for your first form (you can also create all enumerators ina seperate file), declare a new enumerator:
[tt]
public enum MenuSelected
{
None,
ItemOne,
ItemTwo,
ItemThree
}
[/tt]

The next step is to make sure that all your delegates point to the same event handler. This can be done in the designer by selecting the same method for each menuItem.Click event. However, the code is the following:

[tt]
menuItem2.Click += new System.EventHandler(OnMenuSelect);
menuItem3.Click += new System.EventHandler(OnMenuSelect);
menuItem4.Click += new System.EventHandler(OnMenuSelect);
[/tt]

The event method itself must:
1. Instantiate Form2.
2. Make sure that the "sender" is a menu item
3. Find out which object actually initiated the event
4. Run a PrepareForm method on Form2 which will do any pre-processing before...
5. Showing the form.


Here is the code:

[tt]
private void OnMenuSelect(object sender, System.EventArgs e)
{
// Instantiate Form2
Form2 frm2 = new Form2();
if (sender is MenuItem)
{
// Cast sender to MenuItem in order to access the specific methods
MenuItem mi = (MenuItem)sender;
MenuSelected selectedItem = MenuSelected.None;
// Find out which menuitem initiated the event.
switch (mi.Text)
{
case "Menu Item 1":
selectedItem = MenuSelected.ItemOne;
break;
case "Menu Item 2":
selectedItem = MenuSelected.ItemTwo;
break;
case "Menu Item 3":
selectedItem = MenuSelected.ItemThree;
break;
}
// execute method passing our enumerator
frm2.PrepareForm(selectedItem);
frm2.Show();
}
}
[/tt]


In Form2, all you need to implement is the PrepareForm method:

[tt]
public void PrepareForm(MenuSelected selectedItem)
{
if (selectedItem == MenuSelected.ItemOne)
MessageBox.Show("Item One Selected");
if (selectedItem == MenuSelected.ItemTwo)
MessageBox.Show("Item Two Selected");
if (selectedItem == MenuSelected.ItemThree)
MessageBox.Show("Item Three Selected");

}
[/tt]

That's it. Hope it helps.

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top