Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:39:01+00:00 2026-06-18T14:39:01+00:00

I would like to prevent the dropdown list from closing when the user checks

  • 0

I would like to prevent the dropdown list from closing when the user checks or unchecks a checkbox in a checked combobox.

I have copied some Microsoft code to created a checked combobox. As it didn’t work out-of-the-box, I did some customizing.

Here’s my code:

    Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class CheckedCombobox
    Inherits ComboBox
    Public Event ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs)

    <Browsable(False)> _
    Public Overloads ReadOnly Property Items() As ComboBox.ObjectCollection
        Get
            Return MyBase.Items
        End Get
    End Property

    Private WithEvents _ItemCollection As New ObservableCollection(Of String)
    Public Property ItemCollection As ObservableCollection(Of String)
        Get
            Return _ItemCollection
        End Get
        Set(value As ObservableCollection(Of String))
            _ItemCollection = value
        End Set
    End Property

    Private _ItemDictionary As New Dictionary(Of String, Boolean)
    Public ReadOnly Property ItemDictionary As Dictionary(Of String, Boolean)
        Get
            Return _ItemDictionary
        End Get
    End Property
    Public ReadOnly Property CheckedItemCollection As List(Of String)
        Get
            Return New List(Of String)(From item In ItemDictionary Where item.Value = True Select item.Key)
        End Get
    End Property
    Public ReadOnly Property UnCheckedItemCollection As List(Of String)
        Get
            Return New List(Of String)(From item In ItemDictionary Where item.Value = False Select item.Key)
        End Get
    End Property

    Public Sub setCheckState(ByVal key As String, ByVal checkstate As Boolean)
        _ItemDictionary(key) = checkstate
    End Sub

    Public Function getCheckState(ByVal key As String)
        Return (_ItemDictionary(key))
    End Function

    Public Sub New()
        Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
    End Sub

    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        For Each item In ItemCollection
            _ItemDictionary.Add(item, False)
        Next
    End Sub

    Private Sub ItemsChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles _ItemCollection.CollectionChanged

        Select Case e.Action

            Case Specialized.NotifyCollectionChangedAction.Add
                If e.NewStartingIndex = ItemDictionary.Count Then
                    _ItemDictionary.Add(e.NewItems(0), False)
                    MyBase.Items.Add(e.NewItems(0))
                End If

            Case Specialized.NotifyCollectionChangedAction.Remove

                _ItemDictionary.Remove(MyBase.Items(e.OldStartingIndex))
                MyBase.Items.RemoveAt(e.OldStartingIndex)

            Case Specialized.NotifyCollectionChangedAction.Move

                Dim _item As Object = MyBase.Items(e.OldStartingIndex)
                MyBase.Items.RemoveAt(e.OldStartingIndex)
                MyBase.Items.Insert(e.NewStartingIndex, _item)

            Case Specialized.NotifyCollectionChangedAction.Replace

                Throw New Exception("Not implemented yet!")

            Case Specialized.NotifyCollectionChangedAction.Reset

                Dim _checkeditems As New List(Of String)(CheckedItemCollection)

                MyBase.Items.Clear()
                MyBase.Items.AddRange(_ItemCollection.ToArray)

                _ItemDictionary.Clear()
                For Each item In _ItemCollection
                    _ItemDictionary.Add(item, _checkeditems.Contains(item))
                Next

        End Select
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
        e.DrawBackground()
        Dim p As Point = e.Bounds.Location

        If e.Index >= 0 Then
            p.Offset(1, 1)
            If getCheckState(MyBase.Items(e.Index)) Then
                CheckBoxRenderer.DrawCheckBox(e.Graphics, p, VisualStyles.CheckBoxState.CheckedNormal)
            Else
                CheckBoxRenderer.DrawCheckBox(e.Graphics, p, VisualStyles.CheckBoxState.UncheckedNormal)
            End If

            p.Offset(12, 0)
            e.Graphics.DrawString(MyBase.GetItemText(Me.Items(e.Index)), e.Font, New SolidBrush(e.ForeColor), p.X, p.Y)
        End If
        If e.State = DrawItemState.Selected Then
            e.DrawFocusRectangle()
        End If
        MyBase.OnDrawItem(e)
    End Sub

    Private Sub checkedChanged(ByVal index As Integer)
        Dim checked As Boolean = _ItemDictionary(MyBase.Items.Item(index))
        If checked Then
            _ItemDictionary(MyBase.Items.Item(index)) = False
            RaiseEvent ItemCheck(Me, New ItemCheckEventArgs(index, CheckState.Unchecked, CheckState.Checked))
        Else
            _ItemDictionary(MyBase.Items.Item(index)) = True
            RaiseEvent ItemCheck(Me, New ItemCheckEventArgs(index, CheckState.Checked, CheckState.Unchecked))
        End If
        Me.Invalidate()
    End Sub

    Private n As nWindow = Nothing
    Private Const WM_CTLCOLORLISTBOX As Integer = &H134
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = WM_CTLCOLORLISTBOX Then
            If n Is Nothing Then
                n = New nWindow(Me)
                n.AssignHandle(m.LParam)
                AddHandler n.checkedChanged, AddressOf checkedChanged
            End If
        End If
    End Sub

    Private Sub CheckedCombobox_Click(sender As Object, e As System.EventArgs) Handles Me.SelectedIndexChanged
        Debugger.Break()
    End Sub
End Class

Public Class nWindow
    Inherits NativeWindow

    Private Const WM_LBUTTONDOWN As Integer = &H201

    Private _combobox As CheckedCombobox

    Public Event checkedChanged(ByVal index As Integer)

    Public Sub New(ByVal cb As CheckedCombobox)
        _combobox = cb
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_LBUTTONDOWN Then
            Dim itemHeight As Integer = _combobox.ItemHeight
            If New Point(m.LParam.ToInt32).Y \ itemHeight <= _combobox.Items.Count - 1 And New Point(m.LParam.ToInt32).Y \ itemHeight >= 0 Then
                If New Point(m.LParam.ToInt32).X >= 1 And New Point(m.LParam.ToInt32).X <= 11 Then
                    RaiseEvent checkedChanged(_combobox.SelectedIndex)
                End If
            End If
        End If
        MyBase.WndProc(m)
    End Sub

End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:39:02+00:00Added an answer on June 18, 2026 at 2:39 pm

    The code below seems to work correctly. It does what I set out to do.

    (Note: I have only shown the changed routines)

     Private Sub checkedChanged(ByVal index As Integer)
        Dim checked As Boolean = _ItemDictionary(MyBase.Items.Item(index))
        If checked Then
            _ItemDictionary(MyBase.Items.Item(index)) = False
            RaiseEvent ItemCheck(Me, New ItemCheckEventArgs(index, CheckState.Unchecked, CheckState.Checked))
        Else
            _ItemDictionary(MyBase.Items.Item(index)) = True
            RaiseEvent ItemCheck(Me, New ItemCheckEventArgs(index, CheckState.Checked, CheckState.Unchecked))
        End If
        Me.SelectedIndex = -1
    End Sub
    
    
    
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_LBUTTONDOWN Then
            Dim itemHeight As Integer = _combobox.ItemHeight
            If New Point(m.LParam.ToInt32).Y \ itemHeight <= _combobox.Items.Count - 1 And New Point(m.LParam.ToInt32).Y \ itemHeight >= 0 Then
                If New Point(m.LParam.ToInt32).X >= 1 And New Point(m.LParam.ToInt32).X <= 11 Then
                    RaiseEvent checkedChanged(_combobox.SelectedIndex)
                    Return
                End If
            End If
        End If
        MyBase.WndProc(m)
    End Sub
    

    I am not hundred percent sure about not calling MyBase.WndProc(), but I did not notice any side-effects yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to prevent a user from either: Copy and pasting from the
I have an ActiveRecord object and I would like to prevent it from being
I would like to prevent a script from repeating when a user refreshes a
I am building a long list of terms I would like to prevent from
I have a dropdown list, what I would like to do is assign values
I would like to prevent user from submitting dialog form twice. Here is my
I have an ajax form I would like to prevent from preforming an action
I would like to prevent a property from being exposed via my WCF web
If you have some blocks of code that you would like to prevent execution
I find the question in some sites like ow would you prevent someone from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.