Creating a simple menuįirst, create a root window and set its title to 'Menu Demo': root = tk.Tk() It displays menus with the look-and-feel of the target platform that the program runs e.g., Windows, macOS, and Linux. For example, you can find the File menu in most text editors. Typically, you use a menu to group closely related operations. When an application contains a lot of functions, you need to use menus to organize them for easier navigation. then we close all of the child Forms with simple below codeįrmShow.WindowState = FormWindowState.Summary: in this tutorial, you’ll learn how to create a Tkinter menu bar, add menus to the menu bar, and add menu items to each menu. If (type.Name = ())įorm frmShow = ( Form)frmAssembly.CreateInstance(type.ToString()) SqlDataAdapter datransaction = new SqlDataAdapter(Seqtx, conn) ĭataTable dtransaction = new DataTable() Īssembly frmAssembly = Assembly.LoadFile( Application.ExecutablePath) įoreach ( Type type in frmAssembly.GetTypes()) 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. 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 ![]() 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.Ģ. 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. Dynamic MenuStrip in Windows Forms Introduction
0 Comments
Leave a Reply. |