Results 1 to 7 of 7

Thread: Stop SelectedIndexChanged, Selectedvaluechanged ... being called during form load

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    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.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try using the SelectedItemChanged or SelectedChangeCommited events. Some of the others change when you add items also.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Listbox unlike combobox does not have these two events.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Well, in this special situation i used
    bindfrm(Val(sender.selectedvalue.tosting)) that returns 0 till the value is DataRowView.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width