Listbox problem

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

    Listbox problem

    I have a Listbox binded to as DataSet.
    Also I have sub to hadle SelectedIndexCh anged event.
    The problem is that every time the dataset is filled the
    SelectedIndexCh anged is fired.
    How can I prevent this?


  • Ken Tucker [MVP]

    #2
    Re: Listbox problem

    Hi,

    You need to add a boolean variable to you form. Use it to prevent
    the code from running after the first selected index changed event.

    Dim ds As New DataSet

    Dim bIgnoreEvent As Boolean

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Dim strConn As String

    Dim strSQL As String

    Dim daEmployees As OleDbDataAdapte r

    Dim conn As OleDbConnection

    strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"

    strConn &= "Data Source = Northwind.mdb;"

    conn = New OleDbConnection (strConn)

    daEmployees = New OleDbDataAdapte r("Select * From Employees Order by
    LastName, FirstName", conn)

    daEmployees.Fil l(ds, "Employees" )

    bIgnoreEvent = True

    ListBox1.DataSo urce = ds.Tables("Empl oyees")

    ListBox1.Displa yMember = "LastName"



    End Sub

    Private Sub ListBox1_Select edIndexChanged( ByVal sender As Object, ByVal e As
    System.EventArg s) Handles ListBox1.Select edIndexChanged

    If bIgnoreEvent Then

    bIgnoreEvent = False

    Return

    End If

    'your code goes here

    End Sub



    Ken
    -----------------------
    "Nikolay Petrov" <johntup2@mail. bg> wrote in message
    news:OAfrVjciEH A.2668@TK2MSFTN GP10.phx.gbl...
    I have a Listbox binded to as DataSet.
    Also I have sub to hadle SelectedIndexCh anged event.
    The problem is that every time the dataset is filled the
    SelectedIndexCh anged is fired.
    How can I prevent this?



    Comment

    • Nikolay Petrov

      #3
      Re: Listbox problem

      Tnanks Ken

      "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
      news:OlRwLsciEH A.3320@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Hi,
      >
      > You need to add a boolean variable to you form. Use it to prevent
      > the code from running after the first selected index changed event.
      >
      > Dim ds As New DataSet
      >
      > Dim bIgnoreEvent As Boolean
      >
      > Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      > System.EventArg s) Handles MyBase.Load
      >
      > Dim strConn As String
      >
      > Dim strSQL As String
      >
      > Dim daEmployees As OleDbDataAdapte r
      >
      > Dim conn As OleDbConnection
      >
      > strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"
      >
      > strConn &= "Data Source = Northwind.mdb;"
      >
      > conn = New OleDbConnection (strConn)
      >
      > daEmployees = New OleDbDataAdapte r("Select * From Employees Order by
      > LastName, FirstName", conn)
      >
      > daEmployees.Fil l(ds, "Employees" )
      >
      > bIgnoreEvent = True
      >
      > ListBox1.DataSo urce = ds.Tables("Empl oyees")
      >
      > ListBox1.Displa yMember = "LastName"
      >
      >
      >
      > End Sub
      >
      > Private Sub ListBox1_Select edIndexChanged( ByVal sender As Object, ByVal e[/color]
      As[color=blue]
      > System.EventArg s) Handles ListBox1.Select edIndexChanged
      >
      > If bIgnoreEvent Then
      >
      > bIgnoreEvent = False
      >
      > Return
      >
      > End If
      >
      > 'your code goes here
      >
      > End Sub
      >
      >
      >
      > Ken
      > -----------------------
      > "Nikolay Petrov" <johntup2@mail. bg> wrote in message
      > news:OAfrVjciEH A.2668@TK2MSFTN GP10.phx.gbl...
      > I have a Listbox binded to as DataSet.
      > Also I have sub to hadle SelectedIndexCh anged event.
      > The problem is that every time the dataset is filled the
      > SelectedIndexCh anged is fired.
      > How can I prevent this?
      >
      >
      >[/color]


      Comment

      • Andy Becker

        #4
        Re: Listbox problem

        "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
        news:OlRwLsciEH A.3320@TK2MSFTN GP11.phx.gbl...[color=blue]
        > Hi,
        >
        > You need to add a boolean variable to you form. Use it to prevent
        > the code from running after the first selected index changed event.[/color]

        I might clarify this statement by saying that you may not need to do this if
        you are using typed datasets. In tracing through your code, it seems that
        it is the setting of the DataSource and DisplayMember properties which
        trigger SelectedIndexCh anged. This makes sense to me.

        When using a typed dataset in the designer, those properties are set in
        InitializeCompo nent before there is any data (hopefully!)... and thus the
        event does not fire.

        In this same typed dataset scenario, I found that SelectedValueCh anged fired
        once when the ValueMember property was set in InitializeCompo nent. That one
        does not make sense to me. Especially when adding ListBox1.ValueM ember

        I never got a SelectedIndexCh anged call. A side effect of this is that
        after the form and data are loaded, the ListBox is sitting on the first row,
        and the event never fired. This speaks strongly in favor of your
        bIgnoreEvent technique, if one wishes to know that the first list item was
        indeed selected.

        I found that SelectedValueCh anged was a bit worse... It was called 2 or 3
        times by setting the DataSource property, depending on whether the dataset
        was typed or not. Figure that one out! Maybe it had to do with the late
        binding of the ds.Tables("Empl oyees") syntax.

        But I want to be clear that it is not the filling of the dataset which is
        triggering SelectedIndexCh anged multiple times - at least not in this
        particular case. It is the setting of the properties. And if they are set
        before the dataset is filled, it does not appear to be an issue for
        SelectedIndexCh anged.

        Best Regards,

        Andy


        Comment

        • Nikolay Petrov

          #5
          Re: Listbox problem

          So Andy, any idea how to fix the problem?


          "Andy Becker" <x@x.com> wrote in message
          news:eunGCceiEH A.2812@tk2msftn gp13.phx.gbl...[color=blue]
          > "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
          > news:OlRwLsciEH A.3320@TK2MSFTN GP11.phx.gbl...[color=green]
          > > Hi,
          > >
          > > You need to add a boolean variable to you form. Use it to[/color][/color]
          prevent[color=blue][color=green]
          > > the code from running after the first selected index changed event.[/color]
          >
          > I might clarify this statement by saying that you may not need to do this[/color]
          if[color=blue]
          > you are using typed datasets. In tracing through your code, it seems that
          > it is the setting of the DataSource and DisplayMember properties which
          > trigger SelectedIndexCh anged. This makes sense to me.
          >
          > When using a typed dataset in the designer, those properties are set in
          > InitializeCompo nent before there is any data (hopefully!)... and thus the
          > event does not fire.
          >
          > In this same typed dataset scenario, I found that SelectedValueCh anged[/color]
          fired[color=blue]
          > once when the ValueMember property was set in InitializeCompo nent. That[/color]
          one[color=blue]
          > does not make sense to me. Especially when adding ListBox1.ValueM ember
          >
          > I never got a SelectedIndexCh anged call. A side effect of this is that
          > after the form and data are loaded, the ListBox is sitting on the first[/color]
          row,[color=blue]
          > and the event never fired. This speaks strongly in favor of your
          > bIgnoreEvent technique, if one wishes to know that the first list item was
          > indeed selected.
          >
          > I found that SelectedValueCh anged was a bit worse... It was called 2 or 3
          > times by setting the DataSource property, depending on whether the dataset
          > was typed or not. Figure that one out! Maybe it had to do with the late
          > binding of the ds.Tables("Empl oyees") syntax.
          >
          > But I want to be clear that it is not the filling of the dataset which is
          > triggering SelectedIndexCh anged multiple times - at least not in this
          > particular case. It is the setting of the properties. And if they are[/color]
          set[color=blue]
          > before the dataset is filled, it does not appear to be an issue for
          > SelectedIndexCh anged.
          >
          > Best Regards,
          >
          > Andy
          >
          >[/color]


          Comment

          Working...