473,395 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 DescriptionListBox.SelectedValue. 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 DescriptionListBox.Items.Count - 1

If DescriptionListBox.Items.Item(count).value = Me.DataSet.Table.Rows(i).Item(1) then


DescriptionListBox.SetSelected(count, True)

end if
Next

I guess my question is what can I use in place of ListBox.SelectedValue, to capture the Value of a bound listbox's item that is not selected?
Feb 29 '08 #1
17 2645
kadghar
1,295 Expert 1GB
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 DescriptionListBox.SelectedValue. 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 DescriptionListBox.Items.Count - 1

If DescriptionListBox.Items.Item(count).value = Me.DataSet.Table.Rows(i).Item(1) then


DescriptionListBox.SetSelected(count, True)

end if
Next

I guess my question is what can I use in place of ListBox.SelectedValue, 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

Expand|Select|Wrap|Line Numbers
  1. dim a 
  2. a= descritpionlistbox.items.item(3) 
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
Feb 29 '08 #2
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 DescriptionListBox.Items.Item(count) it gives me a conversion datarowview to String error, because it is a bound listbox.


just use the index and save it into a variable e.g

Expand|Select|Wrap|Line Numbers
  1. dim a 
  2. a= descritpionlistbox.items.item(3) 
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
Feb 29 '08 #3
kadghar
1,295 Expert 1GB
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 DescriptionListBox.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:
DescriptionListBox.Items.Item(count).ToString

may be it helps
Feb 29 '08 #4
kadghar
1,295 Expert 1GB
I dont have vb.net here, so i cant check exactly what's the problem, but can you add the method .ToString?? something like:
DescriptionListBox.Items.Item(count).ToString

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

DescriptionListBox.Items.Item(DescriptionListBox.I tems.Count).
Feb 29 '08 #5
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.DataRowView" for every item in the list box.




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

DescriptionListBox.Items.Item(DescriptionListBox.I tems.Count).
Feb 29 '08 #6
kadghar
1,295 Expert 1GB
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.DataRowView" 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).tostring

that's how its done in other versions.

HTH
Feb 29 '08 #7
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.DataRowView".


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).tostring

that's how its done in other versions.

HTH
Feb 29 '08 #8
kadghar
1,295 Expert 1GB
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.DataRowView".
oh, i hate so much they change the syntax. what about (just guessing) :

.items(count).item.value.tostring

¿?
Feb 29 '08 #9
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.

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

.items(count).item.value.tostring

¿?
Mar 1 '08 #10
I tried the .NET forum and here is the answer I received, which worked. Yeah!!

Dim anewtry As DataRowView
anewtry = DescriptionListBox.Items(counting)
Dim blahblah As String
blahblah = DescriptionListBox.ValueMember
MsgBox(anewtry(blahblah))


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.
Mar 3 '08 #11
kadghar
1,295 Expert 1GB
I tried the .NET forum and here is the answer I received, which worked. Yeah!!
Great!!
Thanks
Mar 3 '08 #12
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void Form1_Load(object sender, EventArgs e)
  3.         {
  4.             //I need to figure out how to assign a number value to an item in a list
  5.             //box
  6.             listBox1.Items.Add("Hello");
  7.             listBox1.Items.Add("Goodbye");
  8.             listBox1.Items.Add("Mom");
  9.             listBox1.Items.Add("Dad");
  10.             listBox1.Items.Add("Sandy");
  11.         }
  12.  
  13.         private void label1_Click(object sender, EventArgs e)
  14.         {
  15.             //don't want code here because don't want to do anything when click on label
  16.         }
  17.  
  18.         private void button1_Click(object sender, EventArgs e)
  19.         {
  20.             //this is what happens when button is clicked
  21.             int test = listBox1.SelectedIndices.Count;
  22.             if (test == 0)//make sure they selected something from the listBox
  23.             {
  24.                 MessageBox.Show("Please select at least one item", "Select something", MessageBoxButtons.OK, MessageBoxIcon.Information);
  25.  
  26.             }
  27.             else if (test >= 3)
  28.             {
  29.                 MessageBox.Show("Please select no more than two from the list", "Try again");
  30.  
  31.             }
  32.           //  label1.Text = listBox1.SelectedIndices.Count.ToString();//the number of items selected will appear in the label
  33.  
  34.             //label1.Text = listBox1.SelectedItems.ToString(); //...selectedobjectcollection, not what we want to appear in the label
  35.            // label1.Text = listBox1.SelectedValue.ToString();//creates an exception
  36.  
  37.  
  38.          /*   int listCount = listBox1.Items.Count;//gets the number of items in the collection, starts with 1 and x starts with 0
  39.             for (int x = 0; x < listCount; x++)//for loop to check the array
  40.             {
  41.                 if (listBox1.SelectedIndices.Contains(x))//see if an item is selected, each iteration
  42.                 {
  43.                     MessageBox.Show(listBox1.Items[x].ToString());//put selected items in messagebox, one at a time
  44.                 }
  45.  
Mar 4 '08 #13
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)DescriptionListBox.Items[0];
String valueColumn = DescriptionListBox.ValueMember;
MessageBox.Show(dr[valueColumn].ToString());

Are you trying to assign a value to each item in the list box?
Mar 4 '08 #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)DescriptionListBox.Items[0];
String valueColumn = DescriptionListBox.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...someone 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)listBox1.Items[0];
It says "when casting from a number, the value must be a number less than infinity"...what 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 "ValueMember" it says "Gets or sets the
property to use as the actual value for the items
in the System.Windows.Forms.ListControl."


Expand|Select|Wrap|Line Numbers
  1.  
  2.         private void Form1_Load(object sender, EventArgs e)
  3.         {
  4.             //populate listbox
  5.             listBox1.Items.Add("Mary"); 
  6.             listBox1.ValueMember = "Ma".ToString();
  7.             listBox1.Items.Add("Joe");
  8.             listBox1.ValueMember = "J".ToString();
  9.             listBox1.Items.Add("Mom");
  10.             listBox1.ValueMember = "M".ToString();
  11.             listBox1.Items.Add("Dad");
  12.             listBox1.ValueMember = "D".ToString();
  13.             listBox1.Items.Add("Sandy");
  14.             listBox1.ValueMember = "S".ToString();
  15.  
  16.         }
  17.  
  18.  
I think this is what I want...right?
Mar 5 '08 #15
I've never actually programmed in C#, I received that code from another person, altered it as best I could to VB and ran with it. So I'm not sure I'm quite the person to ask. I will try and answer your questions, but I have also just barely started programming 3 months ago, and I'm still mostly confused by the whole thing. I would recommend creating a new post in the .NET forum. There are a lot of people who program in C# there.


First Question. the infinity error. The listbox that I used this program for was bound to a table. Therefore, most the of the normal ways of looking at my listbox did not work, because instead of my items being just text, they were whole rows in a table. I think your listbox is just text and not bound. As a result, the whole datarowview thing might not work, because your items are not datarowview things, they are just strings. (maybe) Earlier in this Thread we talked about some ways to get values out. Most of the ideas did not work for me, because my listbox was bound. They might work for you, if you can convert them to C#.


Second Question, I think the column and row thing is really just how a person looks at it. the Rows came, because my listbox was bound to a table, and each item in my listbox came from a different row in my table. The column part, was just what SammyB called his variable, I named mine blahblah. However, looking at the values as part of a column makes a lot of sense.


Third Question, my guess is that when you are setting the value member, you are not setting it individually for each listbox item. When I set the value member for my listbox, I set it as a certain column in my table. Therefore the program went through and matched up the the values in two different columns in my program. It made one value, the display member that users could see, and made the other value, the hidden value that only I saw. The point was, I only set my valuemember once for my whole listbox, and it applied to the whole thing. If you want to apply the value to only one item, you are going to need to specify that one item somehow.

These are my best guesses to your questions, but I can't really give you any code to help you follow. Try posting in the .NET forum, they might be able to give you the code you are looking for.




Yes...that is what I want to do! Finally...someone 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)listBox1.Items[0];
It says "when casting from a number, the value must be a number less than infinity"...what 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 "ValueMember" it says "Gets or sets the
property to use as the actual value for the items
in the System.Windows.Forms.ListControl."


Expand|Select|Wrap|Line Numbers
  1.  
  2.         private void Form1_Load(object sender, EventArgs e)
  3.         {
  4.             //populate listbox
  5.             listBox1.Items.Add("Mary"); 
  6.             listBox1.ValueMember = "Ma".ToString();
  7.             listBox1.Items.Add("Joe");
  8.             listBox1.ValueMember = "J".ToString();
  9.             listBox1.Items.Add("Mom");
  10.             listBox1.ValueMember = "M".ToString();
  11.             listBox1.Items.Add("Dad");
  12.             listBox1.ValueMember = "D".ToString();
  13.             listBox1.Items.Add("Sandy");
  14.             listBox1.ValueMember = "S".ToString();
  15.  
  16.         }
  17.  
  18.  
I think this is what I want...right?
Mar 5 '08 #16
Thanks for the advice. I think you are right.

I've just started using C#. The class is supposed to be advanced C++ but the instructor prefers C#, so that is what we're using. I've taken classes in VB, C++, HTML, and have done a little bit of QBasic, a little SQL, and a little JavaScript and DHTML. All these languages do get kind of mixed up when you try to learn them all.

I think C# is kind of a combination of VB and C++.
It would be easier if they just had ONE language and stuck with it. According to my instructor, that is the purpose of making the languages so similar. Eventually they want to have just one language.

Thanks again for trying to help me, and clearing up the row/column question. I'll post the answer when I figure it out.

:)
Mar 5 '08 #17
This works, although I am sure it is not the only way to do this.

Expand|Select|Wrap|Line Numbers
  1.  
  2.         public class names
  3.         {
  4.             private string realName;
  5.             public string nickName;
  6.  
  7.             public names(string strnickName, string strrealName)
  8.             {
  9.  
  10.                 this.realName = strrealName;
  11.                 this.nickName = strnickName;
  12.             }
  13.  
  14.             public string realName1
  15.             {
  16.                 get
  17.                 {
  18.                     return realName;
  19.                 }
  20.             }
  21.  
  22.             public string nickName1
  23.             {
  24.  
  25.                 get
  26.                 {
  27.                     return nickName;
  28.                 }
  29.             }
  30.  
  31.             public override string ToString()
  32.             {
  33.                 return this.realName;
  34.             }
  35.         }
  36.         public void Form1_Load(object sender, EventArgs e)
  37.         {
  38.             //populate listbox 
  39.  
  40.             ArrayList names1 = new ArrayList();
  41.             names1.Add(new names("Mary", "Sis"));
  42.             names1.Add(new names("Joe", "Bubba"));
  43.             names1.Add(new names("Lynn", "Mom"));
  44.             names1.Add(new names("Fred", "Dad"));
  45.             names1.Add(new names("Sandy", "dog"));
  46.  
  47.             listBox1.DataSource = names1;
  48.             listBox1.DisplayMember = "nickName1";
  49.         }
  50.  
  51.         private void button1_Click(object sender, EventArgs e)
  52.             //display values of  selected items
  53.  
  54.                 foreach (int index in listBox1.SelectedIndices)
  55.                 {
  56.                     MessageBox.Show(listBox1.Items[index].ToString());
  57.                     label1.Text += listBox1.Items[index].ToString() + Environment.NewLine;
  58.  
  59.  
  60.  
  61.  
Mar 6 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
1
by: VB Programmer | last post by:
I have a listbox which is data bound to a query that incorporates several tables. I want to be able to update the listbox with new items (from another "master" listbox), remove existing items,...
2
by: Daryll SHatz | last post by:
What object is returned by a bound (to a DataSet) ListBox when it is iterated? Listbox.DataSource = DataSet1.MyTable ListBox.DisplayMember = "Col1" ListBox.ValueMember = "Col2" Dim s as...
0
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
3
by: Richard Albrecht | last post by:
I have been trying to figure out for days on how to read values from a Bound ListBox. The listBox gets the values from an Access Table. I can read values fine for Non-Bound ListBoxes, But the...
0
by: Tony Botelho | last post by:
I am fairly new to VB.NET and I am having some difficulty working with a bound listbox. On a simple form, I open a connection to my database and load a dataser. I then bind a listbox to the the...
3
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores...
0
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number...
4
by: SenileOwl | last post by:
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.