Can I create ToolStripMenuItem using TextBox and Label?

In the WinForms.Net 2.0 application, I want to create a context menu with ToolStripMenuItem, which has both a label and a text field in the element itself. An example of what I'm talking about can be found in Access - when viewing the Access table in the context menu there are options "Filter By Selection", "Filter Exclusion Selection", and then "Filter For: _ _ _ _ _ _". This third option is essentially a label AND a text box in one element. This is what I can’t figure out how to do this.

I had no problem implementing this with two separate ToolStripMenuItems - one for text and then for a child with only a text box. But this is inconvenient and does not look as good as the implementation in Access.

Can someone point me in the right direction? I have problems with the search, since everything I find seems to be related to context menus in the text field itself.

+3
source share
1 answer

Here is the answer:

A practical guide. Wrapping a Windows Forms Control Using ToolStripControlHost ToolStripControlHost
Class

And the short demo that I wrote (remember, it looks awful since I didn't write it at all):

(VB.net, as I prefer, and you did not specify which language you prefer)

Public Class ToolStripEntry
    Inherits ToolStripControlHost

    Public Sub New()
        MyBase.New(New ControlPanel)

    End Sub

    Public ReadOnly Property ControlPanelControl() As ControlPanel
        Get
            Return CType(Me.Control, ControlPanel)
        End Get
    End Property

End Class


Public Class ControlPanel
    Inherits Panel

    Friend WithEvents txt As New TextBox  //with events so you can just use the events
    Friend WithEvents lbl As New Label    //don think you can just do that in c#, but you get the idea

    Public Sub New()

        lbl.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Bottom
        lbl.Text = "Test"
        lbl.TextAlign = ContentAlignment.MiddleLeft
        lbl.Size = New Size(30, Me.Height)          //think of somthing!
        lbl.Location = New Point(0, 0)
        lbl.Parent = Me

        txt.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
        txt.Location = New Point(lbl.Right, 0)
        txt.Width = Me.Width - txt.Left
        txt.Parent = Me

    End Sub

End Class
+2
source

Source: https://habr.com/ru/post/1704248/


All Articles