Bound ListBox Values

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

    Bound ListBox Values

    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 count As Integer = 0 To DescriptionList Box.Items.Count - 1

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


    DescriptionList Box.SetSelected (count, 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?
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by SenileOwl
    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 count As Integer = 0 To DescriptionList Box.Items.Count - 1

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


    DescriptionList Box.SetSelected (count, 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?
    just use the index and save it into a variable e.g

    [CODE=vb]dim a
    a= descritpionlist box.items.item( 3) [/CODE]

    This will make a the 4th item of the listbox without having to select it.
    But it will depend on the version of VB you're using, so if this doesnt work, it'll be good if you post the version.
    HTH

    Comment

    • SenileOwl
      New Member
      • Feb 2008
      • 27

      #3
      Sorry about that. The version I'm working with is VB .NET 2005. Should I have posted this in the .NET forum?

      When I try using DescriptionList Box.Items.Item( count) it gives me a conversion datarowview to String error, because it is a bound listbox.


      Originally posted by kadghar
      just use the index and save it into a variable e.g

      [CODE=vb]dim a
      a= descritpionlist box.items.item( 3) [/CODE]

      This will make a the 4th item of the listbox without having to select it.
      But it will depend on the version of VB you're using, so if this doesnt work, it'll be good if you post the version.
      HTH

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by SenileOwl
        Sorry about that. The version I'm working with is VB .NET 2005. Should I have posted this in the .NET forum?

        When I try using DescriptionList Box.Items.Item( count) it gives me a conversion datarowview to String error, because it is a bound listbox.
        I dont have vb.net here, so i cant check exactly what's the problem, but can you add the method .ToString?? something like:
        DescriptionList Box.Items.Item( count).ToString

        may be it helps

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          Originally posted by kadghar
          I dont have vb.net here, so i cant check exactly what's the problem, but can you add the method .ToString?? something like:
          DescriptionList Box.Items.Item( count).ToString

          may be it helps
          oh, and by the way... shouldnt it be:

          DescriptionList Box.Items.Item( DescriptionList Box.Items.Count ).

          Comment

          • SenileOwl
            New Member
            • Feb 2008
            • 27

            #6
            I'm not sure what you mean by the last message. Could you explain it to me a little bit more. I'm confused as to whether you are referring to my variable called count or the method that counts the total number of items in a listbox.


            As for your first reply, if I add .ToString to the line, it doesn't give me an error, but it returns the line "System.Data.Da taRowView" for every item in the list box.




            Originally posted by kadghar
            oh, and by the way... shouldnt it be:

            DescriptionList Box.Items.Item( DescriptionList Box.Items.Count ).

            Comment

            • kadghar
              Recognized Expert Top Contributor
              • Apr 2007
              • 1302

              #7
              Originally posted by SenileOwl
              I'm not sure what you mean by the last message. Could you explain it to me a little bit more. I'm confused as to whether you are referring to my variable called count or the method that counts the total number of items in a listbox.


              As for your first reply, if I add .ToString to the line, it doesn't give me an error, but it returns the line "System.Data.Da taRowView" for every item in the list box.
              Really nothing, it was my mistake, i supposed you wanted to show the last index, but then i realized you called the variable "count" (which is not actually a very good name, since there's a command called that way, but's ok)

              As i told you, i dont know the right syntax for .net 05, and since im in the office i havn't got it here. but have you tried removing .item?? just something like

              .items(count).t ostring

              that's how its done in other versions.

              HTH

              Comment

              • SenileOwl
                New Member
                • Feb 2008
                • 27

                #8
                You are right. That wasn't a very good variable. I'm sure I will get the hang of this some day. But in answer to your question. Yes I have tried it without the .Item. I get the exact same thing "System.Data.Da taRowView".


                Originally posted by kadghar
                Really nothing, it was my mistake, i supposed you wanted to show the last index, but then i realized you called the variable "count" (which is not actually a very good name, since there's a command called that way, but's ok)

                As i told you, i dont know the right syntax for .net 05, and since im in the office i havn't got it here. but have you tried removing .item?? just something like

                .items(count).t ostring

                that's how its done in other versions.

                HTH

                Comment

                • kadghar
                  Recognized Expert Top Contributor
                  • Apr 2007
                  • 1302

                  #9
                  Originally posted by SenileOwl
                  You are right. That wasn't a very good variable. I'm sure I will get the hang of this some day. But in answer to your question. Yes I have tried it without the .Item. I get the exact same thing "System.Data.Da taRowView".
                  oh, i hate so much they change the syntax. what about (just guessing) :

                  .items(count).i tem.value.tostr ing

                  ¿?

                  Comment

                  • SenileOwl
                    New Member
                    • Feb 2008
                    • 27

                    #10
                    No such luck
                    This was the error I received

                    Public member 'value' on type 'DataRowView' not found.

                    I have to assume there is a way, but I'm running out of ideas to try out. I very much appreciate you trying to help. It makes me feel like I'm not alone out there in the world.

                    Originally posted by kadghar
                    oh, i hate so much they change the syntax. what about (just guessing) :

                    .items(count).i tem.value.tostr ing

                    ¿?

                    Comment

                    • SenileOwl
                      New Member
                      • Feb 2008
                      • 27

                      #11
                      I tried the .NET forum and here is the answer I received, which worked. Yeah!!

                      Dim anewtry As DataRowView
                      anewtry = DescriptionList Box.Items(count ing)
                      Dim blahblah As String
                      blahblah = DescriptionList Box.ValueMember
                      MsgBox(anewtry( blahblah))


                      Originally posted by SenileOwl
                      No such luck
                      This was the error I received

                      Public member 'value' on type 'DataRowView' not found.

                      I have to assume there is a way, but I'm running out of ideas to try out. I very much appreciate you trying to help. It makes me feel like I'm not alone out there in the world.

                      Comment

                      • kadghar
                        Recognized Expert Top Contributor
                        • Apr 2007
                        • 1302

                        #12
                        Originally posted by SenileOwl
                        I tried the .NET forum and here is the answer I received, which worked. Yeah!!
                        Great!!
                        Thanks

                        Comment

                        • OliveOyl3471
                          New Member
                          • Jul 2007
                          • 8

                          #13
                          Hi.
                          I need to know the same thing, except in C# not VB. How do you set the value, not the item itself, and not the index, of an item in a listbox? I think I know how to display the value once it is set. I just don't know how to make an item equal to a numerical value other than an index.

                          Thanks:)

                          Here is my code so far//with some failed attempts and notes to myself included:

                          Code:
                          private void Form1_Load(object sender, EventArgs e)
                                  {
                                      //I need to figure out how to assign a number value to an item in a list
                                      //box
                                      listBox1.Items.Add("Hello");
                                      listBox1.Items.Add("Goodbye");
                                      listBox1.Items.Add("Mom");
                                      listBox1.Items.Add("Dad");
                                      listBox1.Items.Add("Sandy");
                                  }
                          
                                  private void label1_Click(object sender, EventArgs e)
                                  {
                                      //don't want code here because don't want to do anything when click on label
                                  }
                          
                                  private void button1_Click(object sender, EventArgs e)
                                  {
                                      //this is what happens when button is clicked
                                      int test = listBox1.SelectedIndices.Count;
                                      if (test == 0)//make sure they selected something from the listBox
                                      {
                                          MessageBox.Show("Please select at least one item", "Select something", MessageBoxButtons.OK, MessageBoxIcon.Information);
                          
                                      }
                                      else if (test >= 3)
                                      {
                                          MessageBox.Show("Please select no more than two from the list", "Try again");
                           
                                      }
                                    //  label1.Text = listBox1.SelectedIndices.Count.ToString();//the number of items selected will appear in the label
                          
                                      //label1.Text = listBox1.SelectedItems.ToString(); //...selectedobjectcollection, not what we want to appear in the label
                                     // label1.Text = listBox1.SelectedValue.ToString();//creates an exception
                          
                                   
                                   /*   int listCount = listBox1.Items.Count;//gets the number of items in the collection, starts with 1 and x starts with 0
                                      for (int x = 0; x < listCount; x++)//for loop to check the array
                                      {
                                          if (listBox1.SelectedIndices.Contains(x))//see if an item is selected, each iteration
                                          {
                                              MessageBox.Show(listBox1.Items[x].ToString());//put selected items in messagebox, one at a time
                                          }

                          Comment

                          • SenileOwl
                            New Member
                            • Feb 2008
                            • 27

                            #14
                            I don't think this is quite what you wanted, but this is the code for C# that pulls the value of the item in a listbox and not the item itself, or its index or its text.

                            DataRowView dr = (DataRowView)De scriptionListBo x.Items[0];
                            String valueColumn = DescriptionList Box.ValueMember ;
                            MessageBox.Show (dr[valueColumn].ToString());

                            Are you trying to assign a value to each item in the list box?

                            Comment

                            • OliveOyl3471
                              New Member
                              • Jul 2007
                              • 8

                              #15
                              Originally posted by SenileOwl
                              I don't think this is quite what you wanted, but this is the code for C# that pulls the value of the item in a listbox and not the item itself, or its index or its text.

                              DataRowView dr = (DataRowView)De scriptionListBo x.Items[0];
                              String valueColumn = DescriptionList Box.ValueMember ;
                              MessageBox.Show (dr[valueColumn].ToString());

                              Are you trying to assign a value to each item in the list box?
                              Yes...that is what I want to do! Finally...someo ne gets it right. :D

                              I tried your suggestion, and changed the name of the listbox to listBox1. The program runs, but gives me an error at

                              DataRowView dr = (DataRowView)li stBox1.Items[0];
                              It says "when casting from a number, the value must be a number less than infinity"...wha t does that mean?

                              I added a try/catch block here and another place. The program runs, but not like it's supposed to. It still has errors, but I don't know how to fix them.

                              I have another question. Why would I want a row and
                              column if I have a listbox? I'm new to C#, have only had a few
                              lessons so I really don't know what I'm doing with it yet.
                              (Is the answer simply..."that is how you do it"?)

                              To get the value assigned to each list item:
                              Might this possibly be correct? It doesn't give me an error, but
                              it also doesn't display correctly.
                              When I mouse over "ValueMembe r" it says "Gets or sets the
                              property to use as the actual value for the items
                              in the System.Windows. Forms.ListContr ol."


                              Code:
                                      private void Form1_Load(object sender, EventArgs e)
                                      {
                                          //populate listbox
                                          listBox1.Items.Add("Mary"); 
                                          listBox1.ValueMember = "Ma".ToString();
                                          listBox1.Items.Add("Joe");
                                          listBox1.ValueMember = "J".ToString();
                                          listBox1.Items.Add("Mom");
                                          listBox1.ValueMember = "M".ToString();
                                          listBox1.Items.Add("Dad");
                                          listBox1.ValueMember = "D".ToString();
                                          listBox1.Items.Add("Sandy");
                                          listBox1.ValueMember = "S".ToString();
                                          
                                      }
                              I think this is what I want...right?

                              Comment

                              Working...