|
-
Oct 19th, 2002, 12:27 PM
#1
Thread Starter
Frenzied Member
Stop SelectedIndexChanged, Selectedvaluechanged ... being called during form load
Look at the following code:
private sub listboxbind()
ListBox1.DataSource = tb1
ListBox1.DisplayMember = "name"
ListBox1.ValueMember = "ID" ' data type is integer
End Sub
Private Sub ListBox1_SelectedvalueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
MsgBox(sender.selectedvalue.ToString)
End Sub
Private Sub bindfrm(ByVal i As Integer)
'some code here
End Sub
During the form load the Listbox1_selectevalueChanged is called about 7 times, and the messagebox shows 'System.Data.DataRowView' 6 times and the selectedvalue (a number) for the last time. Well this may not seem important at first, but if i change the above like this:
Private Sub ListBox1_SelectedvalueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
MsgBox(sender.selectedvalue.ToString)
bindform(sender.selectedvalue)
End Sub
then after 4 times it stops with an error in the bindfrm sub:
Cast from type 'DataRowView' to type 'Integer' is not valid.
Ok, the reason is obvious, I am calling a sub that needs an integer parameter with a DataRowView.
BUT
The problem is this: how can i stop this event being called during the form load? Each change you make to the listbox for example setting its datasourse, display member... calls this once or twice!!!. This is true for comboboxes too, but there you have an event called 'selectionchangecommitted' that is not called, so I used that in such cases but for the list box there is no such an event.
I hope I have conveyed what I mean.
-
Oct 19th, 2002, 02:51 PM
#2
Fanatic Member
I had similar problems with different events, but there's a way around it - declare a form level boolean variable (or property if you like) called bLoading, initialize it to False, and set it to True whenever you're doing something that you don't want a particular block of event code to be processed for, and set it back to False when you're done. Then in your event code, just put a simple test:
If bLoading = False Then
(do stuff)
End If
The event will still fire, but the overhead of checking one boolean is pretty miniscule. Another way might be to mess with removing the event handler entirely during such an operation, but it seemed to me that was more problematic than it was worth, and it seemed to generate more overhead. Hope this helps.
-
Oct 19th, 2002, 04:14 PM
#3
Try using the SelectedItemChanged or SelectedChangeCommited events. Some of the others change when you add items also.
-
Oct 19th, 2002, 04:24 PM
#4
Thread Starter
Frenzied Member
Listbox unlike combobox does not have these two events.
-
Oct 19th, 2002, 04:24 PM
#5
PowerPoster
I beleive that you can add the event handler later using delegates.
I don't have an example for you, sorry, but instead of having the IDE make the event and binding it for you, you do it after you run the code that you don't want to raise the event.
In C# you do something like this:
control.event += new thedelegate(method);
and you can take the event away by doing almost the same:
control.event -= thedelegate(method);
For VB.Net, I think you use the AddHandler keyword as well as the AddressOf operator.
When I have time, I will try to put an example together for you.
-
Oct 19th, 2002, 04:32 PM
#6
Thread Starter
Frenzied Member
Well, in this special situation i used
bindfrm(Val(sender.selectedvalue.tosting)) that returns 0 till the value is DataRowView.
-
Oct 19th, 2002, 06:25 PM
#7
It might be easier to just fill the listbox (sorry I didn't realize it was a listbox before) in the end of the constructor or in the InitializeComponent sub that the autogenerated code makes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|