|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
I am creating an application that has menu options, with a 'Navigation' menu that will re-direct the user to the appropriate form when clicked. I have a Case structure in a class file for the application to access because there are about 12 different forms to choose from. When the appropriate selection is clicked, it will call a method in the aforementioned class file, and pass a global variable to the method. Is the menu index based? I tried to start the Case at: Case = 0 show form a Case = 1 show form b etc. This doesn't work. Any suggestions are appreciated. Thanks! Dave, I assume from your question that your using the same Click event
hhandler for all items in your Navigation menu, eg this.menuItem1.Click += new System.EventHandler(this.menuItem_Click); this.menuItem2.Click += new System.EventHandler(this.menuItem_Click); this.menuItem3.Click += new System.EventHandler(this.menuItem_Click); where all click events go to menuItem_Click. If that's the case can you use the Index property of the clicked menu to work out it's 0-bssed index in its parent menu, thus: private void menuItem_Click(object sender, System.EventArgs e) { MenuItem menuItem = sender as MenuItem; switch (menuItem.Index) { case 0: //show form a break; case 1: //show form b break; } }
Show quote
"Jason Hales" wrote: Thanks for the reply. I tried what you suggested, with this sub for the > Dave, I assume from your question that your using the same Click event > hhandler for all items in your Navigation menu, eg > > this.menuItem1.Click += new System.EventHandler(this.menuItem_Click); > this.menuItem2.Click += new System.EventHandler(this.menuItem_Click); > this.menuItem3.Click += new System.EventHandler(this.menuItem_Click); > > where all click events go to menuItem_Click. > > If that's the case can you use the Index property of the clicked menu > to work out it's 0-bssed index in its parent menu, thus: > > private void menuItem_Click(object sender, System.EventArgs e) > { > MenuItem menuItem = sender as MenuItem; > switch (menuItem.Index) > { > case 0: > //show form a > break; > case 1: > //show form b > break; > > } > > } > > Jason, click event: Private Sub menuNavigation_FromTheGround_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuNavigation_FromTheGround.Click Methods.ShowForms(g_intAction) End Sub The others are similar in form, just a different navigational name. I have it set up as a Shared Sub so it can be accessed by multiple other subs. Anyways, I attempted what you suggested, but VS didn't like it very much. Here is what I did: I imported these lines at the top of the class file: Imports System.Object Imports System.Windows.Forms.MenuItem Here is the code I added, as per your suggestion: { (it didn't like any curly brace added) ShowForms showForms = sender as ShowForms; Switch(ShowForms.index) { Select Case g_intAction Case 0 'frmFromTheGround is selected frmFromTheGround.Show() with 11 other case structures of similar layout The following errors resulted: showForms-Argument not specified for parameter 'g_intAction' of 'Public Shared Sub ShowForms(g_intAction as integer) sender-sender is not declared as-Comma, ')' or a valid expression continuation expected switch(ShowForms.index)-same as the showForms error, above. I must admit I am new to menu navigation, with the user being re-directed to different forms within an application. I know that I can add a line that will say: frma.show, but I want to reduce the code substantially and only make changes to one block, if needed. Thanks for your help, Dave Hi Dave,
Jason's solution was in C-sharp, therefore it had curly brackets and stuff. You are coding in VB.NET, so you will have to make syntactical changes as necessary. You cannot just remove the curly brackets and expect it to work. In other words, the Switch structure is C# specific and when using VB.NET, you should use the Select Case structure. Here is a VB.NET version of the code that Jason supplied (Thank you Jason !) --------------------------------- Private Sub menuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim menuItem As MenuItem = sender as MenuItem Select Case menuItem.Index Case 0 'show form a Exit Sub Case 1 'show form b Exit Sub End Select End Sub --------------------------------- Note that this method is not wired up the click events of your menus yet. This would be done by using the "this.menuItem1.Click += new System.EventHandler(this.menuItem_Click);" statement if this were C#. But in VB, we have two ways of doing this : a) Either add the following after the menuItem_Click method above "Handles MenuItem1.Click, MenuItem2.Click, MenuItem3.Click..." (Here you should add click events for each of the menuItems that you need to catch) b) Use the AddHandler statement in your Sub New procedure of the form, after InitializeComponent() AddHandler MenuItem1.Click, AddressOf menuItem_Click AddHandler MenuItem2.Click, AddressOf menuItem_Click AddHandler MenuItem3.Click, AddressOf menuItem_Click : and so on for all your menuItems.: So, my suggestion is that there is no need to use a separate class for your menu navigation. It can be easily handled within the Main form itself, as mentioned. This is also what Jason was suggesting. HTH, Regards, Cerebrus. |
|||||||||||||||||||||||