Everytime I select (single-click) on an item in the ListBox, the selected item will be highlighted.
At the same time, "Me.ActiveControl Is ListBox" is true.
How can I 'unselect' a selected item, and set "Me.ActiveControl Is ListBox" is FALSE?
So that "Me.ActiveControl Is Not ListBox"?
I'm trying to execute the following event on a ListBox:
Click on an item in ListBox will select (highlight) an item.
Click on a selected (highlighted) item in ListBox will deselect (unhighlight) an item.
Double click on an item will show a new form.
The following are the codes I wrote:
'Handle click event
Private Sub lstEntry_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstEntry.SelectedIndexChanged
If lstEntry.SelectedItem = strActiveControlText Then
lstEntry.ClearSelected()
strActiveControlText = ""
Me.ActiveControl = Nothing
Else
strActiveControlText = lstEntry.SelectedItem
End If
End Sub
'Handle double click event
Private Sub lstEntry_OnDoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstEntry.DoubleClick
'Create an instance of Edit Entry form
Dim frmEditEntry As New frmEntry("Edit Entry", splitEntryName())
'Show Edit Entry form
frmEditEntry.ShowDialog()
'Retrieve Entry
subRetrieveEntry()
End Sub
My problem is, when an item is already selected, if I try to double click on the selected item, lstEntry_SelectedIndexChanged will be executed before lstEntry_OnDoubleClick is execute.
How can I make it to only execute the lstEntry_OnDoubleClick funtion?
I tried to set ListBox1.SelectionMode = SelectionMode.MultiSimple, but the problem never solved.
Still the same.
When I want to "double-click" on an item which is currently being clicked (highlighted),
the single-click event will be executed first, then only the double-click event is executed.
It seems like it triggers the double-click's first clicking.
Well in point of fact your code is looking at the .SelectedIndexChanged event, which isn't quite the same as .Click, so the behavior is exactly as you describe: when you click the first time, SelectedIndexChanged fires and Me.ActiveControl = Nothing. Then the second click hits, and the DoubleClick event fires. Then, if you double-click again, SelectedIndex does not change, so only DoubleClick fires.
I'm having trouble understanding exactly what kind of behavior you want your app to have.
I understand now why it's throwing that error , try to double click under those items .I'm sure will cause an error because no value was selected . You can overcome this problem as follow :
Just replace this code with the old one .
VB Code:
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick