dcsimg
Results 1 to 4 of 4

Thread: which DropDownItem was clicked

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    44

    which DropDownItem was clicked

    Hi
    I'M still using VB 2005 and want to ask:
    I'Ve a ContextMenuStrip on my Form1. first coloumn of items were added at design time:
    "My IPhone 3"
    "My IPhone 4"
    "My IPhone 4"

    second coloumn of items are added at run time:

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    MyIPhone3ToolStripMenuItem.DropDownItems.Add("january")
    MyIPhone3ToolStripMenuItem.DropDownItems.Add("February")
    MyIPhone3ToolStripMenuItem.DropDownItems.Add("Marc")

    End Sub

    How can I know which item on the second coloumn was clicked

    thank you
    Attached Images Attached Images  
    Last edited by yosilb; Aug 6th, 2011 at 12:38 AM.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: which DropDownItem was clicked

    This was done in VB2010, and I don't know if it is the same in 2005.

    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Debug.WriteLine(ToolStripComboBox1.SelectedIndex)
            Debug.WriteLine(ToolStripComboBox1.SelectedItem)
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'add month names to the context menus combobox
            ToolStripComboBox1.Items.AddRange(Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames)
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    44

    Re: which DropDownItem was clicked

    thanks

    but I have menu types items on the second column and not combobox type items

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,522

    Re: which DropDownItem was clicked

    In the following example OneToolStripMenuItem is a top level menu item created in the IDE.

    ToolStripMenuItem2 is added as a child to OneToolStripMenuItem and shares the same Click event with OneToolStripMenuItem.

    So in the shared click event (see below) we can tell who was clicked by name and then act as needed.

    Code:
    Dim ToolStripMenuItem2 As System.Windows.Forms.ToolStripMenuItem
    ToolStripMenuItem2 = New System.Windows.Forms.ToolStripMenuItem
    ToolStripMenuItem2.Name = "mnuWhatEver"
    ToolStripMenuItem2.Text = "Whatever"
    OneToolStripMenuItem.DropDownItems.Add(ToolStripMenuItem2)
    AddHandler ToolStripMenuItem2.Click, AddressOf OneToolStripMenuItem_Click
    Code:
    Private Sub OneToolStripMenuItem_Click( _ 
    	ByVal sender As System.Object, _ 
    	ByVal e As System.EventArgs) Handles OneToolStripMenuItem.Click
    	
        Console.WriteLine("[{0}]", CType(sender, ToolStripMenuItem).Name)
    
        Select Case CType(sender, ToolStripMenuItem).Name
            Case "mnuWhatEver"
                Console.WriteLine("Whatever made at run time")
            Case "OneToolStripMenuItem"
                Console.WriteLine("OneToolStripMenuItem - made at design time")
            Case Else
                Console.WriteLine("???")
        End Select
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width