How to create custom menu with CheckedListBox and 'TextBox for filter items'

Mansour_Dalir 1,756 Reputation points
2024-07-06T04:03:43.93+00:00

hi. I tried with this code, but the size of the menu and the list becomes bigger in Saudi style. And the scroll list does not work. And that the size of the width of the text box is not adjusted

Public Class Form2
    Dim menu As New ContextMenuStrip()
    Dim WithEvents txtFilter As New TextBox
    Dim WithEvents chkListBoxSelect As New CheckedListBox
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        menu.AutoSize = False
        menu.ShowImageMargin = False
        menu.Width = 200
        menu.Height = 200
        txtFilter.Size = New Size(menu.Width, 12)
        txtFilter.Width = menu.Width
        chkListBoxSelect.ScrollAlwaysVisible = True
        chkListBoxSelect.Size = New Size(menu.Width, 100)
        chkListBoxSelect.Items.AddRange({"Item 1", "Item 2"})

        menu.Items.Add(New ToolStripControlHost(txtFilter))
        menu.Items.Add(New ToolStripControlHost(chkListBoxSelect))
        Me.ContextMenuStrip = menu
    End Sub

    Private Sub txtFilter_TextChanged(sender As Object, e As EventArgs) Handles txtFilter.TextChanged
        chkListBoxSelect.Items.Add("Not Fix Size Menu And ListBox")
    End Sub
End Class

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,675 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 29,416 Reputation points Microsoft Vendor
    2024-07-08T06:53:14.78+00:00

    Hi @Mansour_Dalir ,

    You can create a new class inherits ContextMenuStrip that encapsulates the functionality.

    Public Class AdvancedContextMenu
        Inherits ContextMenuStrip
    
        Private WithEvents txtFilter As New TextBox
        Private WithEvents chkListBoxSelect As New CheckedListBox
    
        Public Sub New()
            MyBase.New()
            InitializeComponents()
        End Sub
    
        Private Sub InitializeComponents()
            Me.AutoSize = True
            Me.ShowImageMargin = False
    
            chkListBoxSelect.MaximumSize = New Size(0, 200)
            chkListBoxSelect.Items.AddRange({"Item 1 xx", "Item 2", "Item 3", "Item 4 xxxxx", "Item 5"})
            txtFilter.MinimumSize = New Size(200, 0)
            txtFilter.Width = 200
    
            Me.Items.Add(New ToolStripControlHost(txtFilter) With {.AutoSize = False})
            Me.Items.Add(New ToolStripControlHost(chkListBoxSelect) With {.AutoSize = False})
        End Sub
    
        Private Sub txtFilter_TextChanged(sender As Object, e As EventArgs) Handles txtFilter.TextChanged
            chkListBoxSelect.Items.Add("Not Fix Size Menu And ListBox")
            chkListBoxSelect.Width = MaxW()
        End Sub
    
        Private Function MaxW() As Integer
            Dim w As Integer = 0
            For Each item As ToolStripItem In Me.Items
                If item.Bounds.Width > w Then w += item.Bounds.Width
            Next
            Return w
        End Function
    
        Public Function ValidateTextCellValue(value As String) As Boolean
            ' Add your validation logic
            For Each item In chkListBoxSelect.CheckedItems
                If value = item.ToString() Then
                    Return True
                End If
            Next
            Return False
        End Function
    End Class
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful