textbox and listbox

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

    textbox and listbox

    Hello,
    I'm going around in circles with this problem and I hope someone can help me.

    I have a listbox that is populated from a SQL Server database, with about 600 items, all strings.
    I have a textbox on the same form, where I want to display which item is selected.

    In the 'OnSelectedInde xChanged' event, I need code that has the textbox update to whatever value is currently selected in the listbox.

    If I use the code:

    TextBox1.Text = ListBox1.Select edItem.toString

    it diplays the incorrect item in the textbox.

    If I use a datarow view with the following code:
    Dim drv As DataRowView = lBoxCP.Selected Item
    txtCPMain.Text = drv("STR_CUTTIN G_PERMIT")
    It works quite well, but after clicking several different items, the text box starts showing incorrect items.

    SelectionMode is set to One.

    Any suggestions?
    Thanks,
    Amber


  • Ken Tucker [MVP]

    #2
    Re: textbox and listbox

    Hi,

    Try binding the textbox and the listbox to the same datasource. The
    currency manager will keep them in sync.

    Ken
    --------------
    "amber" <anonymous@disc ussions.microso ft.com> wrote in message
    news:C758D239-5604-4EF6-AA57-9F7D772D5E43@mi crosoft.com...[color=blue]
    > Hello,
    > I'm going around in circles with this problem and I hope someone can help[/color]
    me.[color=blue]
    >
    > I have a listbox that is populated from a SQL Server database, with about[/color]
    600 items, all strings.[color=blue]
    > I have a textbox on the same form, where I want to display which item is[/color]
    selected.[color=blue]
    >
    > In the 'OnSelectedInde xChanged' event, I need code that has the textbox[/color]
    update to whatever value is currently selected in the listbox.[color=blue]
    >
    > If I use the code:
    >
    > TextBox1.Text = ListBox1.Select edItem.toString
    >
    > it diplays the incorrect item in the textbox.
    >
    > If I use a datarow view with the following code:
    > Dim drv As DataRowView = lBoxCP.Selected Item
    > txtCPMain.Text = drv("STR_CUTTIN G_PERMIT")
    > It works quite well, but after clicking several different items, the text[/color]
    box starts showing incorrect items.[color=blue]
    >
    > SelectionMode is set to One.
    >
    > Any suggestions?
    > Thanks,
    > Amber
    >
    >[/color]


    Comment

    • Ken Tucker [MVP]

      #3
      Re: textbox and listbox

      Hi,

      Try binding the textbox and the listbox to the same datasource. The
      currency manager will keep them in sync.

      Ken
      --------------
      "amber" <anonymous@disc ussions.microso ft.com> wrote in message
      news:C758D239-5604-4EF6-AA57-9F7D772D5E43@mi crosoft.com...[color=blue]
      > Hello,
      > I'm going around in circles with this problem and I hope someone can help[/color]
      me.[color=blue]
      >
      > I have a listbox that is populated from a SQL Server database, with about[/color]
      600 items, all strings.[color=blue]
      > I have a textbox on the same form, where I want to display which item is[/color]
      selected.[color=blue]
      >
      > In the 'OnSelectedInde xChanged' event, I need code that has the textbox[/color]
      update to whatever value is currently selected in the listbox.[color=blue]
      >
      > If I use the code:
      >
      > TextBox1.Text = ListBox1.Select edItem.toString
      >
      > it diplays the incorrect item in the textbox.
      >
      > If I use a datarow view with the following code:
      > Dim drv As DataRowView = lBoxCP.Selected Item
      > txtCPMain.Text = drv("STR_CUTTIN G_PERMIT")
      > It works quite well, but after clicking several different items, the text[/color]
      box starts showing incorrect items.[color=blue]
      >
      > SelectionMode is set to One.
      >
      > Any suggestions?
      > Thanks,
      > Amber
      >
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: textbox and listbox

        Hi Amber,

        Are you sure you use the protected event 'OnSelectedInde xChanged' because
        than I think you have to show some code to give us an idea what you are
        doing.

        Cor


        Comment

        • Cor Ligthert

          #5
          Re: textbox and listbox

          Hi Amber,

          Are you sure you use the protected event 'OnSelectedInde xChanged' because
          than I think you have to show some code to give us an idea what you are
          doing.

          Cor


          Comment

          • amber

            #6
            Re: textbox and listbox

            Thanks Ken

            It is bound to the same dataset, isn't it? Here's my code

            The list box is 1st populated by this code

            Private Sub get_all_CPs(

            lBoxCP.DataSour ce = Nothin
            lBoxCP.Items.Cl ear(
            lBoxCP.Refresh(

            DsCP1.Clear(
            SqlDA_CP.Fill(D sCP1

            Dim dvCP As DataVie
            dvCP = New DataVie

            With dvC
            .Table = DsCP1.Tables("T DT_CUTTING_PERM IT"
            .Sort = "STR_CUTTING_PE RMIT
            End Wit

            lBoxCP.DataSour ce = dvC
            lBoxCP.DisplayM ember = "STR_CUTTING_PE RMIT
            lBoxCP.Refresh(

            End Su

            And here's the listbox event code

            Private Sub lBoxCP_Selected IndexChanged(By Val sender As System.Object, ByVal e As System.EventArg s) Handle
            lBoxCP.Selected IndexChange

            Dim drv As DataRowView = lBoxCP.Selected Ite
            txtCPMain.Text = drv("STR_CUTTIN G_PERMIT"

            End Su

            Thanks
            Ambe

            Comment

            • amber

              #7
              Re: textbox and listbox

              Thanks Ken

              It is bound to the same dataset, isn't it? Here's my code

              The list box is 1st populated by this code

              Private Sub get_all_CPs(

              lBoxCP.DataSour ce = Nothin
              lBoxCP.Items.Cl ear(
              lBoxCP.Refresh(

              DsCP1.Clear(
              SqlDA_CP.Fill(D sCP1

              Dim dvCP As DataVie
              dvCP = New DataVie

              With dvC
              .Table = DsCP1.Tables("T DT_CUTTING_PERM IT"
              .Sort = "STR_CUTTING_PE RMIT
              End Wit

              lBoxCP.DataSour ce = dvC
              lBoxCP.DisplayM ember = "STR_CUTTING_PE RMIT
              lBoxCP.Refresh(

              End Su

              And here's the listbox event code

              Private Sub lBoxCP_Selected IndexChanged(By Val sender As System.Object, ByVal e As System.EventArg s) Handle
              lBoxCP.Selected IndexChange

              Dim drv As DataRowView = lBoxCP.Selected Ite
              txtCPMain.Text = drv("STR_CUTTIN G_PERMIT"

              End Su

              Thanks
              Ambe

              Comment

              • Cor Ligthert

                #8
                Re: textbox and listbox

                Hi Amber,

                That is what I thought it is the

                SelectedIndexCh anged not OnSelectedIndex Changed, which exist also

                However when you add that one row I did you can delete that complete event
                in my opinion
                \\\
                lBoxCP.DataSour ce = dvCP
                lBoxCP.DisplayM ember = "STR_CUTTING_PE RMIT"
                txtCPMain.Text. DataBindings.Ad d(Text",dvCP,"S RT_CUTTING_PERM IT")
                lBoxCP.Refresh( )
                ///

                I do not know if you fill the dataset more and than you can set by the clear
                of the dataset
                txtCPMain.Text. Databindings.cl ear

                I hope this helps?

                Cor


                Comment

                • Cor Ligthert

                  #9
                  Re: textbox and listbox

                  Hi Amber,

                  That is what I thought it is the

                  SelectedIndexCh anged not OnSelectedIndex Changed, which exist also

                  However when you add that one row I did you can delete that complete event
                  in my opinion
                  \\\
                  lBoxCP.DataSour ce = dvCP
                  lBoxCP.DisplayM ember = "STR_CUTTING_PE RMIT"
                  txtCPMain.Text. DataBindings.Ad d(Text",dvCP,"S RT_CUTTING_PERM IT")
                  lBoxCP.Refresh( )
                  ///

                  I do not know if you fill the dataset more and than you can set by the clear
                  of the dataset
                  txtCPMain.Text. Databindings.cl ear

                  I hope this helps?

                  Cor


                  Comment

                  • Cor Ligthert

                    #10
                    Re: textbox and listbox

                    >I do not know if you fill the dataset more and than you can set by the
                    clear

                    typos

                    I do not know if you fill the dataset more than once and than you can set by
                    the clear


                    Comment

                    • Cor Ligthert

                      #11
                      Re: textbox and listbox

                      >I do not know if you fill the dataset more and than you can set by the
                      clear

                      typos

                      I do not know if you fill the dataset more than once and than you can set by
                      the clear


                      Comment

                      • Ken Tucker [MVP]

                        #12
                        Re: textbox and listbox

                        Hi,

                        Instead of setting the value in the selected index changed event
                        try this.

                        Private Sub get_all_CPs()

                        lBoxCP.DataSour ce = Nothing
                        lBoxCP.Items.Cl ear()
                        lBoxCP.Refresh( )

                        DsCP1.Clear()
                        SqlDA_CP.Fill(D sCP1)

                        Dim dvCP As DataView
                        dvCP = New DataView

                        With dvCP
                        .Table = DsCP1.Tables("T DT_CUTTING_PERM IT")
                        .Sort = "STR_CUTTING_PE RMIT"
                        End With

                        lBoxCP.DataSour ce = dvCP
                        lBoxCP.DisplayM ember = "STR_CUTTING_PE RMIT"
                        lBoxCP.Refresh( )

                        txtCPMain.DataB indings.Add("Te xt", dvCP, "STR_CUTTING_PE RMIT")


                        End Sub

                        Ken
                        --------------
                        "amber" <anonymous@disc ussions.microso ft.com> wrote in message
                        news:0C6216A5-D3EA-4FF7-9667-5DB3056FDE67@mi crosoft.com...[color=blue]
                        > Thanks Ken,
                        >
                        > It is bound to the same dataset, isn't it? Here's my code:
                        >
                        > The list box is 1st populated by this code:
                        >
                        > Private Sub get_all_CPs()
                        >
                        > lBoxCP.DataSour ce = Nothing
                        > lBoxCP.Items.Cl ear()
                        > lBoxCP.Refresh( )
                        >
                        > DsCP1.Clear()
                        > SqlDA_CP.Fill(D sCP1)
                        >
                        > Dim dvCP As DataView
                        > dvCP = New DataView
                        >
                        > With dvCP
                        > .Table = DsCP1.Tables("T DT_CUTTING_PERM IT")
                        > .Sort = "STR_CUTTING_PE RMIT"
                        > End With
                        >
                        > lBoxCP.DataSour ce = dvCP
                        > lBoxCP.DisplayM ember = "STR_CUTTING_PE RMIT"
                        > lBoxCP.Refresh( )
                        >
                        > End Sub
                        >
                        > And here's the listbox event code:
                        >
                        > Private Sub lBoxCP_Selected IndexChanged(By Val sender As System.Object,[/color]
                        ByVal e As System.EventArg s) Handles[color=blue]
                        > lBoxCP.Selected IndexChanged
                        >
                        > Dim drv As DataRowView = lBoxCP.Selected Item
                        > txtCPMain.Text = drv("STR_CUTTIN G_PERMIT")
                        >
                        > End Sub
                        >
                        > Thanks,
                        > Amber
                        >[/color]


                        Comment

                        • Ken Tucker [MVP]

                          #13
                          Re: textbox and listbox

                          Hi,

                          Instead of setting the value in the selected index changed event
                          try this.

                          Private Sub get_all_CPs()

                          lBoxCP.DataSour ce = Nothing
                          lBoxCP.Items.Cl ear()
                          lBoxCP.Refresh( )

                          DsCP1.Clear()
                          SqlDA_CP.Fill(D sCP1)

                          Dim dvCP As DataView
                          dvCP = New DataView

                          With dvCP
                          .Table = DsCP1.Tables("T DT_CUTTING_PERM IT")
                          .Sort = "STR_CUTTING_PE RMIT"
                          End With

                          lBoxCP.DataSour ce = dvCP
                          lBoxCP.DisplayM ember = "STR_CUTTING_PE RMIT"
                          lBoxCP.Refresh( )

                          txtCPMain.DataB indings.Add("Te xt", dvCP, "STR_CUTTING_PE RMIT")


                          End Sub

                          Ken
                          --------------
                          "amber" <anonymous@disc ussions.microso ft.com> wrote in message
                          news:0C6216A5-D3EA-4FF7-9667-5DB3056FDE67@mi crosoft.com...[color=blue]
                          > Thanks Ken,
                          >
                          > It is bound to the same dataset, isn't it? Here's my code:
                          >
                          > The list box is 1st populated by this code:
                          >
                          > Private Sub get_all_CPs()
                          >
                          > lBoxCP.DataSour ce = Nothing
                          > lBoxCP.Items.Cl ear()
                          > lBoxCP.Refresh( )
                          >
                          > DsCP1.Clear()
                          > SqlDA_CP.Fill(D sCP1)
                          >
                          > Dim dvCP As DataView
                          > dvCP = New DataView
                          >
                          > With dvCP
                          > .Table = DsCP1.Tables("T DT_CUTTING_PERM IT")
                          > .Sort = "STR_CUTTING_PE RMIT"
                          > End With
                          >
                          > lBoxCP.DataSour ce = dvCP
                          > lBoxCP.DisplayM ember = "STR_CUTTING_PE RMIT"
                          > lBoxCP.Refresh( )
                          >
                          > End Sub
                          >
                          > And here's the listbox event code:
                          >
                          > Private Sub lBoxCP_Selected IndexChanged(By Val sender As System.Object,[/color]
                          ByVal e As System.EventArg s) Handles[color=blue]
                          > lBoxCP.Selected IndexChanged
                          >
                          > Dim drv As DataRowView = lBoxCP.Selected Item
                          > txtCPMain.Text = drv("STR_CUTTIN G_PERMIT")
                          >
                          > End Sub
                          >
                          > Thanks,
                          > Amber
                          >[/color]


                          Comment

                          Working...