dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Context Menu With Textbox & Button.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    190

    Resolved [RESOLVED] Context Menu With Textbox & Button.

    Evening all,

    I'm having a few problems with aToolStripTextbox and a ToolStripButton on a context menu. Essentially what i want to do is right click on a form, context menu appears with a couple of choices at the top then a textbox followed by a button. When the button is clicked the textbox text is read and for this example printed in the console...

    I'm pretty sure where i've gone wrong is that the textbox is 'read' when the menu is created and not when the button is clicked. Any idea how i'd solve this or if there's a better way to achieve it?

    Thanks

    Dave

    Context Menu Code :
    Code:
                Dim cms = New ContextMenuStrip
                Dim cmsTB = New ToolStripTextBox
                Dim cmsBTN = New ToolStripButton
    
                ' First Menu Item
                Dim Item1 = cms.Items.Add("1st menu item")
                Item1.Tag = 1 
                AddHandler Item1.Click, AddressOf MenuChoice
                
                ' Second Menu Item
                Dim Item2 = cms.Items.Add("2nd menu item")
                Item2.Tag = 2 
                AddHandler Item2.Click, AddressOf MenuChoice
    
                ' Third Menu Item
                Dim Item3 = cms.Items.Add(cmsTB)
                cmsTB.BorderStyle = BorderStyle.FixedSingle
    
                ' Fourth Menu Item
                Dim Item4 = cms.Items.Add(cmsBTN)
                cmsBTN.Text = "Add Day Note"
                cmsBTN.Tag = cmsTB.Text
                AddHandler cmsBTN.Click, AddressOf MenuButtonClick
    
                cms.Show(Me.TestDGV, Me.TestDGV.PointToClient(Windows.Forms.Cursor.Position))
    Event Handlers Code:

    Code:
    Private Sub MenuButtonClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim item = CType(sender, ToolStripButton)
            Console.WriteLine(CStr(item.Tag))
    End Sub
    
    Private Sub MenuChoice(ByVal sender As Object, ByVal e As EventArgs)
            Dim item = CType(sender, ToolStripMenuItem)
            Dim Selection As Integer = CInt(item.Tag)
            Dim Save As New cAvailabilty
            Dim ChangeToCellColour As Color
            Dim CellColourNum As Integer = 0
    
            If Selection = 1 Then Console.WriteLine("First item clicked")
            If Selection = 2 Then Console.WriteLine("Second item clicked")
    End Sub
    Last edited by QuattroDave; Apr 13th, 2018 at 03:06 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,831

    Re: Context Menu With Textbox & Button.

    You can host a usercontrol in a menu of any kind. Search for ToolStripControlHost

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,831

    Re: Context Menu With Textbox & Button.

    There's an example in this example project i have on MSDN

    https://code.msdn.microsoft.com/quizControl-65965231

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,773

    Re: Context Menu With Textbox & Button.

    Correct...at the moment you set the .Tag, it gets what ever is in the textbox... which is an empty string... so when you read the .Tag in the click event, that's what is there... an empty string. If you want the contents of the textbox, you need to read the textbox. How to do that? Couple ways... One way is to use the .Tag, but store a reference to the textbox itself in there (the .Tag is an object property, so you can store anything you want in it.)
    Code:
    'Instead of 
    cmsBTN.Tag = cmsTB.Text
    'use
    cmsBTN.Tag = cmsTB 'Instead
    and then...
    Code:
    Private Sub MenuButtonClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim item = CType(sender, ToolStripButton)
            Dim itemTB = CType(item, ToolStripTextBox)
            Console.WriteLine(itemTB.Text)
    End Sub
    Or, you simpy dim cmsTB at a higher level so that it's available where you need it to be, such as at the form level.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    190

    Re: Context Menu With Textbox & Button.

    Quote Originally Posted by techgnome View Post
    One way is to use the .Tag, but store a reference to the textbox itself in there (the .Tag is an object property, so you can store anything you want in it.)
    Code:
    'Instead of 
    cmsBTN.Tag = cmsTB.Text
    'use
    cmsBTN.Tag = cmsTB 'Instead
    and then...
    Code:
    Private Sub MenuButtonClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim item = CType(sender, ToolStripButton)
            Dim itemTB = CType(item, ToolStripTextBox)
            Console.WriteLine(itemTB.Text)
    End Sub

    -tg
    Perfect, thank you!

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