473,322 Members | 1,610 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,322 software developers and data experts.

New List Items

xHow do I create a new item in a list box that has a different value then the
text? When using the add method the value and the text are the same. I’m
really surprised that listbox1.Items.Add(“value”, “text”); doesn’t work. So
how can I do this? Thanks.
Jul 21 '05 #1
3 1221
You can add any type of object you want to a listbox as an item. That means
you can create a class with a all the data you want, simply override the
ToString() method, and add it to a listbox. For example:

private class TestClass
{
public string name = "shane";
public int value = 282;
public override string ToString()
{
return name;
}
}
.....

TestClass t = new TestClass();
listbox1.Items.Add(t); // listbox1 will show an item named "shane"

Chances are that you have already created a class and you just need to
override the ToString method so that it is displayed properly.

ShaneB
"Tome73" <To****@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
xHow do I create a new item in a list box that has a different value then
the
text? When using the add method the value and the text are the same. I'm
really surprised that listbox1.Items.Add("value", "text"); doesn't work.
So
how can I do this? Thanks.

Jul 21 '05 #2
Tome,

In addition to Shane,

There is a big difference by adding a object to items of the listbox or to
bind a datasource to it.

There are in my opinion two kind of listboxes (better listcontrols), one
which uses the items and the otherone the datasource.

With the datasource you can use the "value" properties of the listbox.

With the items you have to use the objects in the itemarray, which you can
cast back using the DataRowView.

I hope this helps?

Cor

"Tome73" <To****@discussions.microsoft.com>
xHow do I create a new item in a list box that has a different value then
the
text? When using the add method the value and the text are the same. I'm
really surprised that listbox1.Items.Add("value", "text"); doesn't work.
So
how can I do this? Thanks.

Jul 21 '05 #3
Got it; thanks everyone;

ListItem lstNew = new ListItem();
lstNew.Text = "mytest";
lstNew.Value = "myvalue";
lbleft.Items.Add(lstNew);
"Tome73" wrote:
xHow do I create a new item in a list box that has a different value then the
text? When using the add method the value and the text are the same. I’m
really surprised that listbox1.Items.Add(“value”, “text”); doesn’t work. So
how can I do this? Thanks.

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon | last post by:
Hi, I'm hoping you could show me examples of how a functional/declarative language could be used to consicely describe resticted subsets of elements. I'm looking for a 'specification' style...
21
by: M. Clift | last post by:
Hi All, Could someone help me out with this? items = ('a', 'b', 'c', 'd') items + 1 = ( 'b', 'c', 'd', 'a') items + 2 = ( 'c', 'd', 'a', 'b') items + 3 = ( 'd', 'a', 'b', 'c') trans = 1
1
by: Oliver Eichler | last post by:
Hi what will be the fastest solution to following problem class a: def __init__(self): self.varA = 0 self.varB = 0 s.o. ...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
3
by: Kriston-Vizi Janos | last post by:
Dear Mr. Kern, and Members, Thank you very much for the fast answer, my question became over-simplified. My source code is appended below. It uses two text files (L.txt and GC.txt) as input...
6
by: ahart | last post by:
I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child...
13
by: Joel Koltner | last post by:
Is there an easy way to get a list comprehension to produce a flat list of, say, for each input argument? E.g., I'd like to do something like: for x in range(4) ] ....and receive
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.