Listbox SelectedIndexChanged Timing

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

I have a listbox and a textbox. When I choose the value from the listbox, I
would like to see the value placed in the textbox. When I click a row in the
listbox, the SelectedIndexChanged event fires fine and places the value in
the textbox. The problem is that this event fires when I fill the listbox
with a dataset, and I get an error "Argument prompt cannot be converted to
type string." Even if I use the following code:

If Not lstChooseForms Is Nothing Then
' Do nothing
Else
' Fill the text box with the value.
End If

The above code does not prevent the event from firing. Do I have to get into
storing my own flags to make sure this code not run when the dataset fills
the listbox?



Derek
 
You could also call RemoveHandler before filling the dataset and AddHandler
after filling the dataset to avoid the handler being hit during the fill.

============================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools
 
Do you have an example of this, and is there another event I could use? It
seems that there is no click event.

Derek
 
1) Add your handler via the VB dropdown.

2) Go to the handler code and remove the Handles xxxxxx clause at the end of
the sub signature so you just have a sub signature.

3) At the bottom of form load, call AddHandler to hook th event

4) Where you fill your dataset, call RemoveHandler and Addhandler again.

Here are some code snppets:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs)
'your code
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'.......
AddHandler ListBox1.SelectedIndexChanged, AddressOf
ListBox1_SelectedIndexChanged
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RemoveHandler ListBox1.SelectedIndexChanged, AddressOf
ListBox1_SelectedIndexChanged

'file your dataset however you do it...
'....

AddHandler ListBox1.SelectedIndexChanged, AddressOf
ListBox1_SelectedIndexChanged
End Sub


=====================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools
 
Back
Top