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
josephliu48Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ignacio Soler GarciaSolution Architech & Technical LeadCommented:
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
0
wguerramCommented:
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


0
josephliu48Author Commented:
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
0
moduloCommented:
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.