Bound ListBox Values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SenileOwl
    New Member
    • Feb 2008
    • 27

    Bound ListBox Values

    I posted this in the Visual Basic Forum, but I've been wondering if .NET might be a better place for it. I'm working in Visual Basic Microsoft Visual Studio .NET 2005.

    I'm very new to the whole programming thing and I'm having a little trouble with the syntax of my code.

    My code contains a bound listbox where the display member and value member are two different values. I want my program to cycle through the listbox and compare the value member of each item in the list box with a fixed value. My problem is I can't seem to find the syntax to grab the value of the item in the list box. I can't use DescriptionList Box.SelectedVal ue. Because the item is not selected. This is an example of some code I've tried, but it is not working.

    For counting As Integer = 0 To DescriptionList Box.Items.Count - 1

    If DescriptionList Box.Items.Item( counting).value = Me.DataSet.Tabl e.Rows(i).Item( 1) then


    DescriptionList Box.SetSelected (counting, True)

    end if
    Next

    I guess my question is what can I use in place of ListBox.Selecte dValue, to capture the Value of a bound listbox's item that is not selected?
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #2
    Just use the Items property. DescriptionList Box.Items(1) gives you the second item in the list. You can also use a for each loop. HTH --Sam
    [code=vb]
    Dim s As String
    For Each s In DescriptionList Box.Items
    MessageBox.Show (s)
    Next s
    [/code]

    Comment

    • SenileOwl
      New Member
      • Feb 2008
      • 27

      #3
      No such luck. My ListBox is bound to a table. Therefore the program reads items as a system.data.dat arowview. If I try and put a .ToString on it or in someway convert it to a string, the program gives me "System.Data.Da taRowView" as my listbox item.

      Originally posted by SammyB
      Just use the Items property. DescriptionList Box.Items(1) gives you the second item in the list. You can also use a for each loop. HTH --Sam
      [code=vb]
      Dim s As String
      For Each s In DescriptionList Box.Items
      MessageBox.Show (s)
      Next s
      [/code]

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        Ah, I'm more senile than you. Not exactly sure how to do it in VB, but in C#, it is
        Code:
        DataRowView dr = (DataRowView)DescriptionListBox.Items[0];
        String valueColumn = DescriptionListBox.ValueMember;
        MessageBox.Show(dr[valueColumn].ToString());
        Hopefully, that will get you started. --Sam

        Comment

        • SenileOwl
          New Member
          • Feb 2008
          • 27

          #5
          You are amazing. It worked. Here is the code I used for VB
          [code=vbnet]
          Dim anewtry As DataRowView
          anewtry = DescriptionList Box.Items(count ing)
          Dim blahblah As String
          blahblah = DescriptionList Box.ValueMember
          MsgBox(anewtry( blahblah))
          [/code]

          Thanks a ton.

          Comment

          Working...