473,385 Members | 2,005 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,385 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 1225
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.