setting DataSource fires 'SlectedIndexChanged' event too!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TF

    #1

    setting DataSource fires 'SlectedIndexChanged' event too!

    hi,
    i am using a ListBox control on a windows form using VB.Net. when i
    set the DataSource property of the control to an ArrayList it fires
    following events in this order:
    SelectedIndexCh anged
    SelectedIndexCh anged
    DataSourceChang ed
    SelectedIndexCh anged

    is there any way to check in SelectedIndexCh anged event handler that
    if event is fired while setting DataSource property or user has
    actually changed the item? SelectedIndex property doesn't work 'cause
    it is already set to '0'.

    BTW, why SelectedIndexCh anged is fired 3-times?

    Any help...
    Thanks
    TF
  • John Cottrell

    #2
    setting DataSource fires 'SlectedIndexCh anged' event too!

    I have experienced this same problem in the past and have
    resolved the issue by using a no-so-elegant but workable
    solution using a module level boolean variable.

    For example, declare a variable in your form as such...

    Private mboolSettingDat aSource As Boolean = False

    Then prior to setting the DataSource property set the
    variable to True then after setting the DataSource
    property set the variable back to False.

    Then at the begining of the SelectedIndexCh anged event
    place a code snipet like...

    If mboolSettingDat aSource Then
    Exit Sub
    End If

    FWIW: I believe the reason the SelectedIndexCh anged event
    fires multiple times is because internally the DataSource
    property actually changes the SelectedIndex porperty
    (probably once for each element in the array).

    I hope this helps!
    [color=blue]
    >-----Original Message-----
    >hi,
    >i am using a ListBox control on a windows form using[/color]
    VB.Net. when i[color=blue]
    >set the DataSource property of the control to an[/color]
    ArrayList it fires[color=blue]
    >following events in this order:
    >SelectedIndexC hanged
    >SelectedIndexC hanged
    >DataSourceChan ged
    >SelectedIndexC hanged
    >
    >is there any way to check in SelectedIndexCh anged event[/color]
    handler that[color=blue]
    >if event is fired while setting DataSource property or[/color]
    user has[color=blue]
    >actually changed the item? SelectedIndex property doesn't[/color]
    work 'cause[color=blue]
    >it is already set to '0'.
    >
    >BTW, why SelectedIndexCh anged is fired 3-times?
    >
    >Any help...
    >Thanks
    >TF
    >.
    >[/color]

    Comment

    • TF

      #3
      Re: setting DataSource fires 'SlectedIndexCh anged' event too!

      yes, i did same sort of thing but i was wondering if i am missing something.

      Thanks for reply

      Comment

      Working...