Get/set value (not index, not item) of item in a listbox?

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 30101 Views - Last Post: 12 April 2008 - 10:34 AM Rate Topic: -----

#1 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Get/set value (not index, not item) of item in a listbox?

Posted 03 March 2008 - 07:53 PM

Hi.
I'd like to know how to set the value of an item in a listbox in C#. I don't want the index, I know how to get that. I don't want the item itself displayed. I just want to assign a numerical value to each item in the listbox and then display the item in a label or messagebox, using the numerical value to access the item. Possibly, it doesn't have to be a numerical value, but could be another variable? I am not sure. (I hope I have explained this correctly).

Yes, this is homework, but my instructor said we can use any resource we want, to get the answer. Apparently he doesn't care how we get the answer, just so we get it. Thanks:)

Here's my code so far, with notes to myself and failed attempts included.


		private void Form1_Load(object sender, EventArgs e)
		{
		   //populate listbox
			listBox1.Items.Add("Mary");
			listBox1.Items.Add("Joe");
			listBox1.Items.Add("Mom");
			listBox1.Items.Add("Dad");
			listBox1.Items.Add("Sandy");
		}

		private void button1_Click(object sender, EventArgs e)
		{
		   
			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");

			}

			/* HERE's WHERE THE PROBLEM IS, AND SOME THINGS I'VE TRIED:

			label1.Text = listBox1.SelectedIndices.Count.ToString();//the number of items selected will appear in the label

			label1.Text = listBox1.SelectedItems.ToString(); //displays "...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++)
			   {
				   if (listBox1.SelectedIndices.Contains(x))
				   {
					   MessageBox.Show(listBox1.Items[x].ToString());//put selected items in messagebox, one at a time--this works
				MessageBox.Show(listBox1.SelectedItems.GetEnumerator().ToString());//tried to put selected items in messagebox, one at a time--doesn't work
				   }
			   
			label1.Text = listBox1.Items(x).item.value.tostring();//this is not right, won't work
			*/
		}




Your help is greatly appreciated! :D

This post has been edited by OliveOyl3471: 03 March 2008 - 08:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Get/set value (not index, not item) of item in a listbox?

#2 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Get/set value (not index, not item) of item in a listbox?

Posted 03 March 2008 - 08:01 PM

Have you tried
for (int i=0; i<=n-1; i++)
		   {listBox1.item[i]=your function();
		   }


Was This Post Helpful? 0
  • +
  • -

#3 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 03 March 2008 - 08:17 PM

I couldn't get that to work, either. I modified your suggestion to fit my existing code, but this doesn't work. It changes all entries to "value" instead of assigning a value to each name. You had "your function" where I have "value" but I didn't know what function to put there. Do you have an example of a function I could put there instead? Maybe that would work. :)



			 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++)
			{

		   {listBox1.Items[x]="value";


Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 03 March 2008 - 09:09 PM

I also tried this, which doesn't work either.


			 int listCount = listBox1.Items.Count;
			for (int x = 0; x < listCount; x++)
			{
for(int newNumber=0; newNumber<=x; newNumber++)
{
	string y = listBox1.GetItemText(x).ToString();
	  newNumber=int.Parse(y);
   
		   label1.Text = newNumber.ToString();



This post has been edited by OliveOyl3471: 03 March 2008 - 09:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 03 March 2008 - 09:39 PM

View PostOliveOyl3471, on 3 Mar, 2008 - 09:09 PM, said:

I also tried this, which doesn't work either.


			 int listCount = listBox1.Items.Count;
			for (int x = 0; x < listCount; x++)
			{
for(int newNumber=0; newNumber<=x; newNumber++)
{
	string y = listBox1.GetItemText(x).ToString();
	  newNumber=int.Parse(y);
   
		   label1.Text = newNumber.ToString();






Maybe your instructor is looking for enumeration of the items in your combo box?

enum boxItems
{
	Mary,
	Joe,
	Mom,
	Dad,
	Sandy
} 




I dont know. I dont understand really what this would be for.
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 03 March 2008 - 09:39 PM

This code


int listCount = listBox1.Items.Count;
for (int x = 0; x < listCount; x++)
{
      for(int newNumber=0; newNumber<=x; newNumber++)
     {
           string y = listBox1.GetItemText(x).ToString();
           newNumber=int.Parse(y);  
           label1.Text = newNumber.ToString();
      }
}



Will not show all of the selected items because you're overwriting the value on each iteration. You're going to want to try something like


int listCount = listBox1.Items.Count;
for (int x = 0; x < listCount; x++)
{
      for(int newNumber=0; newNumber<=x; newNumber++)
     {
           string y = listBox1.GetItemText(x).ToString();
           newNumber=int.Parse(y);  
           label1.Text += newNumber.ToString() + Environment.NewLine();
      }
}




That should list each selected item on it's own line in your label control. :)
Was This Post Helpful? 0
  • +
  • -

#7 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 04 March 2008 - 09:09 AM

Thank you. I will try that, to display all the names in the label. (edit-I tried it, but I had to remove the () from Environment.NewLine(); It displays all items in the label, selected or not, but only the index numbers are displayed. So I guess that's not what I'm supposed to do, either)

I need to know how to get/set the value of the list item, not the index or the item itself, but a value?

Would you use the listbox.SelectedValue property, and then to get it into the label use this?

		 string y = listBox1.SelectedValue.ToString();



If so, how do you use the SelectedValue property to set the value of each item?


This is probably a lot easier than it seems, but I just can't figure it out, so far.
:blink:

killnine, thanks for trying to help, but...it's a listbox. can you do enumeration with a listbox? I don't know what it would be used for either.

This post has been edited by OliveOyl3471: 04 March 2008 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#8 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 04 March 2008 - 06:18 PM

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."



		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? If so, how do I display it? Even just a little hint would be helpful!
Thanks again! :D
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Get/set value (not index, not item) of item in a listbox?

Posted 04 March 2008 - 07:11 PM

You are getting there, however your implementation is currently not going to work. You need to add the value member and the display name at the same time to the Listbox.

Here is the MSDN for the ListBox.ValueMember, it includes an example using a class and an ArrayList.
Was This Post Helpful? 0
  • +
  • -

#10 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 04 March 2008 - 08:24 PM

Thank you Jayman.
I've been working on this most of the day, so I need to quit for today. I will try again tomorrow, using the suggestions
from the site you linked. Hopefully I can get the answer tomorrow!
OliveOyl:)
Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Get/set value (not index, not item) of item in a listbox?

Posted 04 March 2008 - 09:27 PM

If you have any problems, then post here and we will help guide you further.
Was This Post Helpful? 0
  • +
  • -

#12 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 05 March 2008 - 12:45 PM

(edit---I erased the code from this post so that anyone reading it won't become confused)

:)

This post has been edited by OliveOyl3471: 06 March 2008 - 07:15 AM

Was This Post Helpful? 0
  • +
  • -

#13 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Get/set value (not index, not item) of item in a listbox?

Posted 05 March 2008 - 03:14 PM

:blink:
:crazy:
Sorry for the confusion. I've erased the code that was here, to keep others who might read this from being confused as well.

This post has been edited by OliveOyl3471: 06 March 2008 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#14 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Get/set value (not index, not item) of item in a listbox?

Posted 05 March 2008 - 04:09 PM

The listbox.SelectedIndices collection contains the set of indicies the
user selected. You only need to iterate through them to display the
selected item(s):

			int listCount = listBox1.Items.Count;//gets the number of items in the collection, starts with 1 and x starts with 0

			foreach (int index in listBox1.SelectedIndices)
			{
				MessageBox.Show(listBox1.Items[index].ToString());	 //put selected items in messagebox, one at a time--this works
			}


Was This Post Helpful? 0
  • +
  • -

#15 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Get/set value (not index, not item) of item in a listbox?

Posted 05 March 2008 - 05:27 PM

Hello again.

Would you mind stating your question again, real concisely. I'd be more than happy to help you out as I'm learning as well. :)

I've re-read your posts and I'm not sure what it is you want the program to do. Please tell me! :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2