Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,482 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,730 people online right now. Registration is fast and FREE... Join Now!




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

2 Pages V  1 2 >  
Reply to this topicStart new topic

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

OliveOyl3471
3 Mar, 2008 - 06:53 PM
Post #1

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,631



Thanked: 18 times
Dream Kudos: 150
My Contributions
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.

CODE


        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! biggrin.gif

This post has been edited by OliveOyl3471: 3 Mar, 2008 - 07:01 PM
User is offlineProfile CardPM
+Quote Post

papuccino1
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
3 Mar, 2008 - 07:01 PM
Post #2

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


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


User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
3 Mar, 2008 - 07:17 PM
Post #3

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,631



Thanked: 18 times
Dream Kudos: 150
My Contributions
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. smile.gif

CODE



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

User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
3 Mar, 2008 - 08:09 PM
Post #4

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,631



Thanked: 18 times
Dream Kudos: 150
My Contributions
I also tried this, which doesn't work either.

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();



This post has been edited by OliveOyl3471: 3 Mar, 2008 - 08:13 PM
User is offlineProfile CardPM
+Quote Post

killnine
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
3 Mar, 2008 - 08:39 PM
Post #5

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 107



Thanked: 3 times
My Contributions
QUOTE(OliveOyl3471 @ 3 Mar, 2008 - 09:09 PM) *

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

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();






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

CODE

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



I dont know. I dont understand really what this would be for.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
3 Mar, 2008 - 08:39 PM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,993



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
This code


csharp

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


csharp

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. smile.gif
User is online!Profile CardPM
+Quote Post

OliveOyl3471
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
4 Mar, 2008 - 08:09 AM
Post #7

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,631



Thanked: 18 times
Dream Kudos: 150
My Contributions
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?

CODE

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

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: 4 Mar, 2008 - 09:14 AM
User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
4 Mar, 2008 - 05:18 PM
Post #8

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,631



Thanked: 18 times
Dream Kudos: 150
My Contributions
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."


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? If so, how do I display it? Even just a little hint would be helpful!
Thanks again! biggrin.gif
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
4 Mar, 2008 - 06:11 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
User is online!Profile CardPM
+Quote Post

OliveOyl3471
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
4 Mar, 2008 - 07:24 PM
Post #10

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,631



Thanked: 18 times
Dream Kudos: 150
My Contributions
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:)
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
4 Mar, 2008 - 08:27 PM
Post #11

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
If you have any problems, then post here and we will help guide you further.
User is online!Profile CardPM
+Quote Post

OliveOyl3471
RE: Get/set Value (not Index, Not Item) Of Item In A Listbox?
5 Mar, 2008 - 11:45 AM
Post #12

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,631



Thanked: 18 times
Dream Kudos: 150
My Contributions


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

smile.gif

This post has been edited by OliveOyl3471: 6 Mar, 2008 - 06:15 AM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:00PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month