vb.net listbox problem

45 pts.
Tags:
ComboBox
ListBox
VB.NET
vb.net related questoin. i have 3 items in listbox. At one of list item i have applide method that on selectedvaluechanged open a combobox but when i click on the blank space in listbox other than listbox items, the combobox appeared still. means the selectedvaluechanged is called again. how to stop this functon call when i click the blank space?

Software/Hardware used:
visual studio 2005

Answer Wiki

Thanks. We'll let you know when a new response is added.

I gave it a try, and in fact both handlers SelectedIndexChanged and SelectedValueChanged fire when you click inside the list box, even when the selected item has not changed or when you click on an empty position, and I didn’t find a way to change that behavior.

When in MultiSimple selection mode it works a little different, and I think you could write some code to limit the selected items number, but it would be more complex.

One workaround would be using a variable to store the previous selected item index, and ignoring the event when the selected index is the same as the previous selection (when you click on an empty position, the event fires, but the selection doesn’t change).

Something like this:

<pre> Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
If mySelectedIndex <> ListBox1.SelectedIndex Then
‘ do what you have to do
mySelectedIndex = ListBox1.SelectedIndex
End If
End Sub</pre>

—————-

Thank u CARLOSDL
i have found the way how to solve my problem.

this problem can be handled by doing so

<pre>Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.SelectedIndexChanged
If (ListBox1.IndexFromPoint(e.X, e.Y) = ListBox.NoMatches) Then
ListBox1.SetSelected(0, False)
Else
If ListBox1.SelectedIndex = 0 Then
// do here what you want
Else
// do here what you want
End If
End If

End Sub </pre>

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following