![]() Here we add the namespace System.Reflection to our project that contains classes that allow us to obtain information about the application dynamically at run-time. To get the "FRM_CODE", in other words the Form name: ![]() then we close all of the child Forms with simple below codeįrmShow.WindowState = FormWindowState.Maximized 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 register the events with the ChildClick. The piece of code above 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 a Sub Menu to the Transactions Menu, we use the "ToolStripMenuItem" Class I have included this piece of Code to Load the SubMenu. In the code above, we read the Settings of the Configuration File and loaded 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 following code: Menu Items are added to the MenuStrip using the Items Collection and we can also add properties for 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 some. The ToolStripMenuItem shows differently depending on whether it is a Top-level Menu Item in To start, 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 the "MNU_SUBMENU" table is 1, since this will be the submenu for the "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 the "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 instance we have created Tables since 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 its "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, since we will be dealing with multiple forms. Create a new project and give it the name "DynamicMenuStripDBDriven".Ģ. This article tries to explain how to create a Database Driven MenuStrip and add Items to the MenuStrip using the ToolStripMenuItem class in a C# Windows Forms application.ġ.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |