Concerning ListBoxes
-
Hi, i am using a listBox that contains some items. I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired. I wish to know if there is a way to make sure that the event is only raised when only an item is double clicked. thanks in advance
-
Hi, i am using a listBox that contains some items. I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired. I wish to know if there is a way to make sure that the event is only raised when only an item is double clicked. thanks in advance
quiteSmart wrote:
I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired.
You can programmatically distinguish between the two cases. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
quiteSmart wrote:
I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired.
You can programmatically distinguish between the two cases. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
CPallini wrote: You can programmatically distinguish between the two cases. How to do that?
-
CPallini wrote: You can programmatically distinguish between the two cases. How to do that?
use the
ListBox.SelectedIndex Property
as stated in MSDN:Property Value
A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.hence, if you got a
-1
then no item is selected. hope that helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
use the
ListBox.SelectedIndex Property
as stated in MSDN:Property Value
A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.hence, if you got a
-1
then no item is selected. hope that helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
CPallini wrote:
Property Value A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.
I have already tried this. It workd fine with one exception when u select an item and then double click in the empty space it gives u the returned index as the index of the selected item so in short this wont work perfectly. do u have another suggestion?
-
CPallini wrote:
Property Value A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.
I have already tried this. It workd fine with one exception when u select an item and then double click in the empty space it gives u the returned index as the index of the selected item so in short this wont work perfectly. do u have another suggestion?
DoubleClick event occurs when the control is double-clicked. This is a normal behaviour. Regards, Bhupi Bhai.
-
CPallini wrote:
Property Value A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.
I have already tried this. It workd fine with one exception when u select an item and then double click in the empty space it gives u the returned index as the index of the selected item so in short this wont work perfectly. do u have another suggestion?
quiteSmart wrote:
do u have another suggestion?
Of course... You can retrieve the Rectangle of the selected item (if there is one) and then check if it contains the point where DoubleClick occurred. It follows an ugly example, using VB.NET (was handy, 'cause I had a VB project easyly available... Conversion to C# is straightforward):
Sub ListBox1DoubleClick(sender As Object, e As System.EventArgs)
Dim index As Integer
index = listBox1.SelectedIndex
Dim ev As System.Windows.Forms.MouseEventArgs
ev = e
If index <> -1 Then
Dim rc As Rectangle
rc =listBox1.GetItemRectangle(index)
If rc.Contains(ev.X, ev.Y) Then
MsgBox("Hello")
End If
End If
End SubHope that helps. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hi, i am using a listBox that contains some items. I tried using the double click event of the list box to make some action based on the item that is double clicked in the listBox. But i discovered that also if i double click on the empty part of the listBox,(not on an item) the event is still fired. I wish to know if there is a way to make sure that the event is only raised when only an item is double clicked. thanks in advance
in your click handler, try using ListBox.IndexFromPoint() and test for ==ListBox.NoMatches (which equals -1 I guess) :)
Luc Pattyn