Link to home
Start Free TrialLog in
Avatar of josephliu48
josephliu48

asked on

How to mimic the locked property of combobox in visual basic .net?

How to mimic the locked property of combobox in visual basic .net?
Microsoft site suggests that cacel the selection in Mousedown event, but i can't figure out how to it.
Anyone's help will be apppreciated.
Joe
Avatar of Ignacio Soler Garcia
Ignacio Soler Garcia
Flag of Spain image

Try this:

Private comboLocked As Boolean
Private comboIndex As Integer = 1

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If comboLocked Then
            ComboBox1.SelectedIndex = comboIndex
        Else
            comboIndex = ComboBox1.SelectedIndex
        End If
    End Sub

SoMoS
Avatar of wguerram
wguerram

An addition to SoMoS code:

Private comboLocked As Boolean
Private comboIndex As Integer

'Capture the selected index in the mousedown
Private Sub ComboBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ComboBox1.MouseDown
         comboIndex = ComboBox1.SelectedIndex
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If comboLocked Then
            ComboBox1.SelectedIndex = comboIndex
        End If
    End Sub


Avatar of josephliu48

ASKER

This is a workable solution, but I think Microsoft's note is better, simply don't allow user to select the
item, just like in vb6, user even can't move the mouse over the items.
But I don't know how to do that in MouseDown event.

    Private m_ReadOnly As Boolean

    'TODO stimulate Canceling the selection in the MouseDown Event.
    Private m_intSelectedValue As Integer = -1

    Public Property MyReadOnly() As Boolean
        Get
            Return m_ReadOnly
        End Get
        Set(ByVal Value As Boolean)
            m_ReadOnly = Value
            If m_ReadOnly Then
                Me.DropDownStyle = ComboBoxStyle.DropDownList
                'Me.Enabled = False

            Else
                Me.DropDownStyle = ComboBoxStyle.DropDown
                'Me.Enabled = True

            End If
        End Set
    End Property

    Private Sub ComboBoxEx_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        m_intSelectedValue = CType(sender, ComboBox).SelectedIndex
    End Sub

    Private Sub ComboBoxEx_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SelectedIndexChanged
        If m_ReadOnly Then
            If m_intSelectedValue <> -1 Then
                CType(sender, ComboBox).SelectedIndex = m_intSelectedValue
                m_intSelectedValue = -1
            End If
        End If
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial