Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dynamically add ToolStripMenuItem ...

Dynamically add ToolStripMenuItem ...

Scheduled Pinned Locked Moved C#
csharp
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IamHuM
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • I IamHuM

      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

      S Offline
      S Offline
      sam
      wrote on last edited by
      #2

      does it mean popup menu ?if yes then yes you can sameer

      I 1 Reply Last reply
      0
      • S sam

        does it mean popup menu ?if yes then yes you can sameer

        I Offline
        I Offline
        IamHuM
        wrote on last edited by
        #3

        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...?

        S 1 Reply Last reply
        0
        • I IamHuM

          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...?

          S Offline
          S Offline
          sam
          wrote on last edited by
          #4

          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

          I 1 Reply Last reply
          0
          • S sam

            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

            I Offline
            I Offline
            IamHuM
            wrote on last edited by
            #5

            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

            S 1 Reply Last reply
            0
            • I IamHuM

              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

              S Offline
              S Offline
              sam
              wrote on last edited by
              #6

              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

              I 1 Reply Last reply
              0
              • S sam

                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

                I Offline
                I Offline
                IamHuM
                wrote on last edited by
                #7

                With what u told i think it is not posible to add different clicke events dynamicaly & differet procedures to handle that events... Thanks for your replies, Vinay

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups