Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

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
0
josephliu48
Asked:
josephliu48
1 Solution
 
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now