If m.Name = Items(1) Then ' Top level menu item found ("mnuFile" in this case)įor Each d As ToolStripMenuItem In m.DropDownItems() If c.Name = Items(0) Then ' Menu "root" control found ("MenuStrip1" in this case)įor Each m As ToolStripDropDownItem In CType(c, ToolStrip).Items() ' Search the right menu item, first locate the menu itself 'Items(0) = "MenuStrip1" ' "Root" menu control ' Dim xString As String = "FrmMain.MnuFile" ' A dot separated value read from the DB (or file etc.)ĭim xString As String = "" ' I used my naming convention, but still a menu naming hierarchyĭim xValue As String = "False" ' Allow/disallow (for _this_ user), value is either True or False Here's an example Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click Then you'll have to a) locate the menu control itself and b) locate the menu item from that menu hierarchy. How to solve this problem in simple way? Or maybe in Some Other Way? ![]() manipulate xString into an object, so i can do "xString.visible=xValue"īut I cannot do that because I don't know how to change STRING into OBJECT. set xValue as boolean = DataTable.rows(i).item("View") set xString as string = DataTable.rows(i).item("ModuleName") create DataTable X, fill with query select * from t_Authority where UserName='xxx' MY GOAL : I want to set authority for user on Event FrmMainMenu_Load base on table t_Autority, so I can control which user can use (visible true) or cannot use (visible false) module in that MenuStripItem. Actually in my case, my form called "FrmMainMenu" already had a menustrip complete with it's item and sub-item, and sub-sub-item, and so on. Sorry before, maybe I didn't explain my needed clearly. It's good example to create dynamic menustrip. The important point is to remember add event handler for menu items' click event. MessageBox.Show("MnuFile selected", "Menu Event", MessageBoxButtons.OK, MessageBoxIcon.Information) ' Notice: I had to hard code "MnuFile" in here If CType(sender, ToolStripMenuItem).Name = "MnuFile" Then Private Sub onMenuClick(ByVal sender As Object, ByVal e As EventArgs) ![]() XMenu.Name = Items(1) ' Name for this controlĪddHandler xMenu.Click, AddressOf onMenuClick Me.MainMenuStrip = FormMenu ' Set also as a main menuĭim xMenu As ĭim xString As String = "FrmMain.MnuFile" ' A dot separated value read from the DB (or file etc.) Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click Menu creation happens when a button is clicked but should be done in some other way. Here's an example how to create a menu dynamically. You want to build a form's menu dynamically from the database, am I right?
0 Comments
Leave a Reply. |