Dynamically add ToolStripMenuItem ...
-
Hi... I want to add 'ToolStripMenuItem' dynamically... In my application i am getting port names with this statement foreach (string s in SerialPort.GetPortNames()) { } now for each port name 's' i want to add a ToolStripMenuItem & a click event for each ToolStripMenuItem (i.e. for all available ports on PC). That click event will open a form with a textbox. Is it possible to do these things dynamically in c#. Mainly dynamically adding click events for ToolStripMenuItem & open a form for each ToolStripMenuItem click event. Regards, Vinay
-
Hi... I want to add 'ToolStripMenuItem' dynamically... In my application i am getting port names with this statement foreach (string s in SerialPort.GetPortNames()) { } now for each port name 's' i want to add a ToolStripMenuItem & a click event for each ToolStripMenuItem (i.e. for all available ports on PC). That click event will open a form with a textbox. Is it possible to do these things dynamically in c#. Mainly dynamically adding click events for ToolStripMenuItem & open a form for each ToolStripMenuItem click event. Regards, Vinay
-
Its same as our normal windows. Like our normal internet exploree or any other windows give option in "File" menu...(New,Open,Save,Save as,Print,Exit etc.) But click events are different... Is it possible...?
ya you can do this.i think below code would solve you problem;
private void createMenu() { MenuItem myMenu =new MenuItem("Name",new System.EventHandler(myMenu_Click)); menuItemFile.MenuItems.Add(mymenu); } private void myMenu_Click(object sender, System.EventArgs e) { MessageBox.Show("Hi"); }
by following same procedure you create your desired menu sameer -
ya you can do this.i think below code would solve you problem;
private void createMenu() { MenuItem myMenu =new MenuItem("Name",new System.EventHandler(myMenu_Click)); menuItemFile.MenuItems.Add(mymenu); } private void myMenu_Click(object sender, System.EventArgs e) { MessageBox.Show("Hi"); }
by following same procedure you create your desired menu sameerThanks... But in my application i dont know number of comports present on a PC. If there are 4 com ports then my application should create 4 ToolStripMenuItems & respective 4 click events.Each Click event will display a form with a text box placed on it. Can u give more suggestion for my application... Thanks & Regards, Vinay
-
Thanks... But in my application i dont know number of comports present on a PC. If there are 4 com ports then my application should create 4 ToolStripMenuItems & respective 4 click events.Each Click event will display a form with a text box placed on it. Can u give more suggestion for my application... Thanks & Regards, Vinay
-
Hmmm .. No problem dear.use same function for all just type cast sender with MenuItem and get the text associated with it(
((MenuItem)sender).Text
).By this you'll know which Item you have clicked.i think since now you can handle the rest code