
String Seqtx = "SELECT FRM_CODE FROM MNU_SUBMENU WHERE FRM_NAME='" + sender.ToString() + "'" MessageBox.Show(string.Concat("You have Clicked ", sender.ToString(), " Menu"), "Menu Items Event",MessageBoxButtons.OK, MessageBoxIcon.Information) Private void ChildClick( object sender, EventArgs e) Here we loop each form and registerd the events with the ChildClick.
#Menustrip vb net code
The above piece of code loads the SubMenus for each Parent. ToolStripMenuItem SSMenu = new ToolStripMenuItem(dr.ToString(), null, new EventHandler(ChildClick)) SqlDataAdapter dachildmnu = new SqlDataAdapter(Seqchild, conn) String Seqchild = "SELECT FRM_NAME FROM MNU_SUBMENU WHERE MENUPARVAL='" + submenu + "'" Public void SubMenu( ToolStripMenuItem mnu, string submenu) To add Sub Menu to the Transactions Menu, we use the ToolStripMenuItem Class I have included this piece of Code to Load the SubMenu. In the above code, we read the Settings of the Configuration File and load the Parent Menu, The Form.MainMenuStrip property determines the merge target. MnuStripItem = new ToolStripMenuItem(dr.ToString()) SqlDataAdapter da = new SqlDataAdapter(Sequel, conn) String Sequel = "SELECT MAINMNU,MENUPARVAL,STATUS FROM MNU_PARENT" Private void FrmParent_Load( object sender, EventArgs e)ĬonnectionString = Ĭonn = new SqlConnection(connectionString)

In the Form Load event Paste the below code: Menu Items are added to the MenuStrip using the Items Collection and we can also add propertiesįor handling shortcut keys and supporting checked menu items. When it a Sub-Menu it shows a margin to the left where images are displayed if we have added. The ToolStripMenuItem shows differently depending on whether it is a Top-level Menu Item in
#Menustrip vb net windows
To Start with add 2 Windows Forms to the Project using Project -> Add Windows Form and rename it and create a structure as shown belowĥ. Here, the MENUPARVAL value in Table MNU_SUBMENU is 1 as this will be the submenu for Transactions Menu.Ĥ. VALUES ( 1, 'FrmFinance', 'Finance', 'Y' ) VALUES ( 1, 'FrmAccount', 'Accounting', 'Y' ) INSERT INTO MNU_SUBMENU (MENUPARVAL ,FRM_CODE ,FRM_NAME ,STATUS ) Sample Insert statement to MNU_SUBMENU table to Load Submenus to the Form INSERT INTO MNU_PARENT (MAINMNU ,STATUS ,MENUPARVAL ) Sample Insert statement to Load the Parent Menu: In this for instance we have created Tables as we need to Load the Menu Items Dynamically: To add MenuItems to the MenuStrip we use the ToolStripMenuItem class. A MenuStrip is nothing without MenuItems. Here we will be working with Child Forms, so set it IsMdiContainer property to true.Īfter executing the piece of code in the Form load event, our Form looks something like this,įigure 1: Placing the MenuStrip control in the Formģ. To add the MenuStrip control to the Form, we use the Add property To add a MenuStrip Dynamically at run-time we create an instance of the MenuStrip Class. The MenuStrip gives an easier approach to represent and group application commands. Rename the Form1 to FrmParent, which is the parent window as we will be dealing with multiple forms. Create a new project and give it a name DynamicMenuStripDBDriven.Ģ.
#Menustrip vb net how to
This article tries to explain how to create a Database Driven MenuStrip and add Items to the MenuStrip using ToolStripMenuItem class in C# Windows Forms application. To use Menustrip in the form, we have to drag the Menustrip from the toolbox and drop it to the form.Dynamic MenuStrip in Windows Forms Introduction Microsoft Visual Studio groups the MenuStrip Control in the toolbox under the Menus & Toolbars group. Adding Menu and Menu Items to MenuStrip Control Then we will provide some simple handler for it. In this example, we will create the above shown C# Windows Form with Menu and Menu items in the C# MenuStrip Control. For Example, in the above picture, we can see one such sub-Menu with two Menu items in it. If a Menu item is linked with one more Menu, then the resulting Menu is called as Sub-Menu. Menu items are hooked up to a command handler, and when the user clicks the Menu item, the corresponding handler gets called. Say for example in the above picture, the Exit and Just for Demo are Menu items. In the above example form we add only File Menu to it. The below screenshot shows our example:Ī Menu bar dot net calls it as a Menu strip can hold multiple Menus.

Similarly, the ToolStrip control allows us to add one or more Tool Bar buttons in it. A menu strip allows us to add Menu and Menu allows us to add Menu Items. In this Example, we will explore the C# MenuStrip and ContextMenuStrip Controls.Īll the strip controls accommodate some other UI elements in it. Dot net has three great strip controls, namely MenuStrip, StatusStrip and ToolStrip. Adding Handler Functions for Menu ItemsĪ Strip is a nearly thin slice of an object.
