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
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
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.Mouse EventArgs) Handles ComboBox1.MouseDown
comboIndex = ComboBox1.SelectedIndex
End Sub
Private Sub ComboBox1_SelectedIndexCha nged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexCha nged
If comboLocked Then
ComboBox1.SelectedIndex = comboIndex
End If
End Sub
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.Mouse
comboIndex = ComboBox1.SelectedIndex
End Sub
Private Sub ComboBox1_SelectedIndexCha
If comboLocked Then
ComboBox1.SelectedIndex = comboIndex
End If
End Sub
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.Mouse EventArgs) Handles MyBase.MouseDown
m_intSelectedValue = CType(sender, ComboBox).SelectedIndex
End Sub
Private Sub ComboBoxEx_SelectedIndexCh anged(ByVa l sender As Object, ByVal e As System.EventArgs) Handles MyBase.SelectedIndexChange d
If m_ReadOnly Then
If m_intSelectedValue <> -1 Then
CType(sender, ComboBox).SelectedIndex = m_intSelectedValue
m_intSelectedValue = -1
End If
End If
End Sub
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
m_intSelectedValue = CType(sender, ComboBox).SelectedIndex
End Sub
Private Sub ComboBoxEx_SelectedIndexCh
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private comboLocked As Boolean
Private comboIndex As Integer = 1
Private Sub ComboBox1_SelectedIndexCha
If comboLocked Then
ComboBox1.SelectedIndex = comboIndex
Else
comboIndex = ComboBox1.SelectedIndex
End If
End Sub
SoMoS