Context Menu's
-
I'm having a problem in how to implement context menu's I have a main form that has a 2 listviews on it and a set of radio buttons. the 2 radio buttons effect the contents of one of the listviews. Depending upon the listview selected (and its contents in the case of the first listview) I want to create context menu's I can create context based upon which listview called it but dont seem to be able to reference back to determine which radio button is set OR add a value to the function in the addhandler item when adding the event handler to the context menu item. Any ideas on ways to implement this would be greatly appreciated. Basically want to make a framework where I can say create a context menu for a specific type - so that if that listview is used on different forms can simply call the CreateContextMenu Function which will create the menu with appropriate hookups to the procedures.
-
I'm having a problem in how to implement context menu's I have a main form that has a 2 listviews on it and a set of radio buttons. the 2 radio buttons effect the contents of one of the listviews. Depending upon the listview selected (and its contents in the case of the first listview) I want to create context menu's I can create context based upon which listview called it but dont seem to be able to reference back to determine which radio button is set OR add a value to the function in the addhandler item when adding the event handler to the context menu item. Any ideas on ways to implement this would be greatly appreciated. Basically want to make a framework where I can say create a context menu for a specific type - so that if that listview is used on different forms can simply call the CreateContextMenu Function which will create the menu with appropriate hookups to the procedures.
I'm assuming that since the values change based on what radion button is selected, and that you want to display a different context menu based on what is selected. I'd suggest creatting 2 different context menu's in design view, then when a radio button is selected, change the listview.contextmenu property to the respective context menu.
-
I'm assuming that since the values change based on what radion button is selected, and that you want to display a different context menu based on what is selected. I'd suggest creatting 2 different context menu's in design view, then when a radio button is selected, change the listview.contextmenu property to the respective context menu.
That may work in this instance - but the concept is that I want something that is generic enough that I can re-use anywhere in app. Having to create many context menu's of different forms is not really what I want to occur. So What I'd like is that I can Call a routine CreateContextMenu(byval MenuType as integer) which would create a Context Menu for a specific type of Menu (Say Business Code, Order, Invoice etc.) This adds Standard menu item such as "Open", "New" etc. which at the moment are calling functions called OpenFunctions and NewFunctions. which determine the actual real function to be called such as OpenOrder, OpenBusinessCode etc. Determining which specific function to be called is handled by looking at a application level property which changes depending upon the listview currently selected. Ultimately what I'm trying to do is pass a parameter into the OpenFunctions and NewFunctions procedures which are used to determine the specifc type of menu to create. But that would involve when setting up menuItems making the AddressOf clause pointing to the function accept a paramater So Ideally I'd like to do something like Sub OpenFunctions(byval iType as integer) Dim conMenu As New ContextMenu Dim menuItem1 As New MenuItem IF iType <> 0 then menuItem1.Text = "&test" conMenu.MenuItems.Add(menuItem1) AddHandler menuItem1.Click, AddressOf OpenFunctions(iType) else throw new exception ("Invalid Type!!!") end if End Sub But cant seem to get the following line .... to work AddHandler menuItem1.Click, AddressOf OpenFunctions(iType) I think this is the key - being able to pass the addressof a function with parameters.
-
That may work in this instance - but the concept is that I want something that is generic enough that I can re-use anywhere in app. Having to create many context menu's of different forms is not really what I want to occur. So What I'd like is that I can Call a routine CreateContextMenu(byval MenuType as integer) which would create a Context Menu for a specific type of Menu (Say Business Code, Order, Invoice etc.) This adds Standard menu item such as "Open", "New" etc. which at the moment are calling functions called OpenFunctions and NewFunctions. which determine the actual real function to be called such as OpenOrder, OpenBusinessCode etc. Determining which specific function to be called is handled by looking at a application level property which changes depending upon the listview currently selected. Ultimately what I'm trying to do is pass a parameter into the OpenFunctions and NewFunctions procedures which are used to determine the specifc type of menu to create. But that would involve when setting up menuItems making the AddressOf clause pointing to the function accept a paramater So Ideally I'd like to do something like Sub OpenFunctions(byval iType as integer) Dim conMenu As New ContextMenu Dim menuItem1 As New MenuItem IF iType <> 0 then menuItem1.Text = "&test" conMenu.MenuItems.Add(menuItem1) AddHandler menuItem1.Click, AddressOf OpenFunctions(iType) else throw new exception ("Invalid Type!!!") end if End Sub But cant seem to get the following line .... to work AddHandler menuItem1.Click, AddressOf OpenFunctions(iType) I think this is the key - being able to pass the addressof a function with parameters.
Ok did some testing Here is what I'm going to suggest. Since you are wanting to reuse the creation of these contextmenu's throughout your program, What I did, is create a class library within your project, create your function to create the contextmenu, and return the context menu. Here is what my class looks like Imports System.Windows.Forms Public Class Class1 Public Function createmenu() Dim conMenu As New ContextMenu Dim menuItem1 As New MenuItem menuItem1.Text = "&test" conMenu.MenuItems.Add(menuItem1) AddHandler menuItem1.Click, AddressOf opentest Return conMenu End Function Public Sub opentest(ByVal sender As Object, ByVal e As System.EventArgs) MsgBox("Test for opentest worked") End Sub End Class Make sure, on your Forms, you set reference for this class library, My Form code looks as follows Imports testing_contextmenu.Class1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim conmenu2 As New ContextMenu Dim testing As New testing_contextmenu.Class1 conmenu2 = testing.createmenu() ListView1.ContextMenu = conmenu2 End Sub So I believe the easiest solution would be to create the class library, create your functions to create your menu's, and returrn those as I have done, Hope this helps, or gets ya in the direction you want.
-
Ok did some testing Here is what I'm going to suggest. Since you are wanting to reuse the creation of these contextmenu's throughout your program, What I did, is create a class library within your project, create your function to create the contextmenu, and return the context menu. Here is what my class looks like Imports System.Windows.Forms Public Class Class1 Public Function createmenu() Dim conMenu As New ContextMenu Dim menuItem1 As New MenuItem menuItem1.Text = "&test" conMenu.MenuItems.Add(menuItem1) AddHandler menuItem1.Click, AddressOf opentest Return conMenu End Function Public Sub opentest(ByVal sender As Object, ByVal e As System.EventArgs) MsgBox("Test for opentest worked") End Sub End Class Make sure, on your Forms, you set reference for this class library, My Form code looks as follows Imports testing_contextmenu.Class1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim conmenu2 As New ContextMenu Dim testing As New testing_contextmenu.Class1 conmenu2 = testing.createmenu() ListView1.ContextMenu = conmenu2 End Sub So I believe the easiest solution would be to create the class library, create your functions to create your menu's, and returrn those as I have done, Hope this helps, or gets ya in the direction you want.
Thanks for the idea - but heres the problem.... Lets say the function OpenTest that your calling, Is as a result of a right click on a listview. From a listview I need to be able to pass a parameter of which Item I want to open. Which is the listview item ID column. The Sender and e values are that of the menuitem1 and not of those of the listview that was right clicked.... So in this case I need the menu that is not generic enough that I can either pass a reference to a listview or have a parameter passed to tell it what item to Open. Simply displaying a messagebox when value is clicked is not enough. So possibly its trying to pass a value into createcontextmenu function and pass this through to the Opentest - but the signature then doesnt match and the line AddHandler menuItem1.Click, AddressOf opentest will not work.