Results 1 to 11 of 11

Thread: Context menu help [resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Resolved Context menu help [resolved]

    Okay I have a couple (well 50 or so) of text boxes on my form which all share the same context menu.
    Is there any way to find out which text box the user has right clicked on.
    VB Code:
    1. Private Sub MenuItem8_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
    2.         MsgBox("you have just right clicked " & ClickedTextBox)
    3.     End Sub

    To clarify what I'm trying to do, each text box contains a name of a person, when you right click on that person I want to be able to send that person a message, but to do this I need to know what text box has been clicked.
    Last edited by Oliver1; Feb 28th, 2006 at 09:50 AM.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Context menu help

    i cant remember if you use sender to find out which textbox or if there's a property listed in e but one of the two will tell ya which textbox called the menu
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Context menu help

    should be the sender parameter that will hold what control that caused the event to fire.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  4. #4
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Context menu help

    you could do the following...


    VB Code:
    1. Private Sub MenuItem8_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
    2.         If TypeOf sender Is TextBox Then
    3.             MsgBox("you have just right clicked " & CType(sender, TextBox).Name)
    4.         End If
    5. End Sub

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  5. #5
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Context menu help

    I'm not sure how to do it (still thinking), but in both the suggestions Sender will not do it. Sender is not the text box, but the MenuItem that is being clicked.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Context menu help

    Ok here is a way to do it. May not be the best, but it works.

    You are going to need to create a mousedown event for the textboxes. In the mouse down event save the sender in a variable and then call that variable in menu click.

    Example
    VB Code:
    1. Private test As String
    2.  
    3.     Private Sub ContextMenuStrip1_Open(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    4.         MessageBox.Show("TextBox clicked was " & test)
    5.     End Sub
    6. ' notice I only need one procedure for two textboxes because I set the handles to both of them.
    7.     Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown, TextBox2.MouseDown
    8.         If e.Button = Windows.Forms.MouseButtons.Right Then
    9.             test = sender.ToString
    10.         End If
    11.     End Sub
    12. End Class
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  7. #7
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Context menu help

    here i got it for you and i through this together really quick....

    VB Code:
    1. Private Sub TestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TestToolStripMenuItem.Click
    2.         If TypeOf sender Is ToolStripMenuItem Then
    3.             If TypeOf CType(sender, ToolStripMenuItem).Owner Is ContextMenuStrip Then
    4.                 Debug.WriteLine(CType(CType(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl.Name)
    5.             End If
    6.         End If
    7.     End Sub

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Context menu help

    Sorry for the delay, got dragged of on other projects.
    Have just had a quick look at vbdotnetboy solution and I don't understand and can't get it to work.
    What is a ToolStripMenuItem assumed it was a menuitem, but menuitem doesn't have an owner.
    Also assumed ContextMenuStrip was a Contextmenu but not sure it is.

    Will have a look at mpdeglau soloution now, which looks a lot more understandable, but less elegant (sorry).

    Anyway thanks all for you help so far and any more help would be very much appreciated.

  9. #9
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Context menu help

    Quote Originally Posted by Oliver1
    Sorry for the delay, got dragged of on other projects.
    Have just had a quick look at vbdotnetboy solution and I don't understand and can't get it to work.
    What is a ToolStripMenuItem assumed it was a menuitem, but menuitem doesn't have an owner.
    Also assumed ContextMenuStrip was a Contextmenu but not sure it is.

    Will have a look at mpdeglau soloution now, which looks a lot more understandable, but less elegant (sorry).

    Anyway thanks all for you help so far and any more help would be very much appreciated.

    i'm very, very sorry... i wrote that using vb2005 with the 2.0 framework, and you're obviously not using that.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Context menu help

    No thanks alot for your help, just figured it out

    CType(CType(sender, MenuItem).Parent, ContextMenu).SourceControl.Name

    which is pretty much the same as you had.
    Thanks again, never would have figured it out on my own.

  11. #11
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Context menu help [resolved]

    glad i could help... i was just getting ready to post that to you too

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

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