Results 1 to 4 of 4

Thread: Get the Name of the Active Control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Get the Name of the Active Control

    Hi i was wondering how to get the name of the active control in Visual basic

    system.reflection..... that doesnt work

    like i use addhandler so if I do that it gets me the name of the adress of the original control

    Also, I'm trying to get a toolstripdropdown menu as a bookmarks thing; how can I get the name of the selected dropdown item?

    thanks guys

    its a big confusing but please ask if i'm not clear I didn't get any help for my other 3 questions

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get the Name of the Active Control

    Why exactly do you need the name of the active control? I would think that you would want the active control itself, which you would get from the form's ActiveControl property. If you really do want its name then you can just get its Name property.

    That said, I think what you're actually asking for is the control that raised the event you're currently handling. In that case you use the 'sender' parameter. Again, you don't need the name because the 'sender' refers to the object itself. That goes for menu items that are all covered by the same Click event handler too. The 'sender' parameter refers to the menu item that was clicked.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    78

    Re: Get the Name of the Active Control

    k i have a dropdown menu in a toolstrip and I want users to add items into it, and when an item is clicked, it will go to whatever the clicked item's text is

    thats my main problem i cant find the script that identifies the control

    in a normal listbox and combobox its selecteditem

    but i'm using a toostripdropdown menu button

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get the Name of the Active Control

    Like I said, you can use a single method to handle the Click event for all the menu items. This is such a common occurrence that there are methods dedicated to it specifically. In that one event handler, the 'sender' parameter refers to the menu item that was clicked.
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Me.ItemsToolStripMenuItem.DropDownItems.Add(Me.TextBox1.Text, _
    3.                                                 Nothing, _
    4.                                                 AddressOf HandleMenuItemClick)
    5. End Sub
    6.  
    7. Private Sub HandleMenuItemClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    8.     Dim item = DirectCast(sender, ToolStripMenuItem)
    9.  
    10.     MessageBox.Show(item.Text)
    11. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width