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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

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

Posted 05 March 2008 - 08:21 PM

This a couple of things that need to be changed to get your ListBox working the way you want it. To your names class reverse the order of the arguments being passed to your constructor.

Like this:
			public names(string strnickName, string strrealName)



Put the following code in your Load Event.
			ArrayList names1 = new ArrayList();
			names1.Add(new names("Mary", "Sis"));
			names1.Add(new names("Joe", "Bubba"));
			names1.Add(new names("Lynn", "Mom"));
			names1.Add(new names("Fred", "Dad"));
			names1.Add(new names("Sandy", "dog"));

			listBox1.DataSource = names1;
			listBox1.DisplayMember = "nickName1";


As far as the rest of the code you are asking about. You are going to have to explain what you are trying to do. Currently, you are trying to assign values to the ListBox and you can't do this when it is tied to a data source.
Was This Post Helpful? 0
  • +
  • -

#17 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 06 March 2008 - 06:24 AM

Ok.

I'm sorry for not being more clear. :blush: All this searching and trying different things that don't work is
a little confusing.

Thank you all for trying to help.

Here is exactly what I want to do, with whatever code that will work, whether I already have it or not:

1. In addition to adding items to the listbox, assign a value other than index to each of those items.
2. Display (wherever--as long as it works) the selected items. (Using whatever works, to check that each item is associated with a different value)
I want to be able to access either one. If I put (something?) in the code, the value will be displayed, and if I put (something else?) in
the code, the item itself will be displayed. (In a label, messagebox, textbox or whatever).

These two things are really all I need to do. The rest is just things I wondered about or tried to do.


Is that better? :D

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

Was This Post Helpful? 0
  • +
  • -

#18 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

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

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

Posted 06 March 2008 - 07:01 AM

Crap. :P I still can't understand what you're trying to do. :blink:

I'm cooking something up, try it out and see if that's what you want it to do.

Here it is:

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (textBox1.Text != "")
			{
				this.listBox1.Items.Add(this.textBox1.Text);
			}
			else 
			{
				MessageBox.Show("Please write something to add to the listbox.");
			}

		}
	}
}


What it does is copy the text the user writes in the textbox into the listbox, automatically assigning an index value. Is this what you want? Edit: Now the user can't copy anything into the listbox if textbox is left blank.

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

Was This Post Helpful? 0
  • +
  • -

#19 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 06 March 2008 - 07:36 AM

No...I wasn't looking for that exactly...although it is good to get different ideas, and try different approaches to the problem.
After rereading it, I see what you did...let the user add their own items to the listbox. Neat.
:look: I do appreciate you trying to help me. :^:



jayman9,
Your code (post #16) works. :bananaman: I think that is what I wanted to do, and using the suggestion from n8wxs, I think I've finally
got it!!!
:D


			//display selected items in a label and in a messagebox, one at a time
				foreach (int index in listBox1.SelectedIndices)
				{
					MessageBox.Show(listBox1.Items[index].ToString());
					label1.Text += listBox1.Items[index].ToString() + Environment.NewLine;
				}




Thank you so much, all of you who've been helping me.
:)

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

Was This Post Helpful? 0
  • +
  • -

#20 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

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

Posted 06 March 2008 - 11:39 AM

I'm glad we were able to help guide you to a working solution.

We are here to help anytime you need it.
Was This Post Helpful? 0
  • +
  • -

#21 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 06 March 2008 - 09:02 PM

you are trying to assign values to the ListBox and you can't do this when it is tied to a data source.

?
...but it works...I think.

Do you mean that there is another way to assign values to the ListBox?

Can you do this without a data source?
Was This Post Helpful? 0
  • +
  • -

#22 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 12 April 2008 - 10:34 AM

View PostOliveOyl3471, on 6 Mar, 2008 - 09:02 PM, said:

you are trying to assign values to the ListBox and you can't do this when it is tied to a data source.

?
...but it works...I think.

Do you mean that there is another way to assign values to the ListBox?

Can you do this without a data source?


I found out the answer.
No...you can't.

But you can do it without the 'get' and 'set'.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2