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

Newbe Question. Adding items to Combo Box

Gav
I am using VS 2005 and am trying to add items to a combo box using C#. I
know how to add simple text items but I am trying to add a value and some
text ie.

Value Text
A First Text
B Second Text

I've seen examples of ListItem but I can't seem to get this to work, can
anybody offer any advice.

thanks

Gavin
Nov 17 '05 #1
4 24733
On Thu, 7 Jul 2005 13:19:13 +0100, "Gav"
<ga************@nospam.portakabin.com> wrote:
I am using VS 2005 and am trying to add items to a combo box using C#. I
know how to add simple text items but I am trying to add a value and some
text ie.

Value Text
A First Text
B Second Text

I've seen examples of ListItem but I can't seem to get this to work, can
anybody offer any advice.

thanks

Gavin


Convert the value to a string, concatenate it with the text that you
want and add the whole thing to your combo box.

rossum

The ultimate truth is that there is no ultimate truth
Nov 17 '05 #2
Gav

"rossum" <ro******@coldmail.com> wrote in message
news:nl********************************@4ax.com...
On Thu, 7 Jul 2005 13:19:13 +0100, "Gav"
<ga************@nospam.portakabin.com> wrote:
I am using VS 2005 and am trying to add items to a combo box using C#. I
know how to add simple text items but I am trying to add a value and some
text ie.

Value Text
A First Text
B Second Text

I've seen examples of ListItem but I can't seem to get this to work, can
anybody offer any advice.

thanks

Gavin

Convert the value to a string, concatenate it with the text that you
want and add the whole thing to your combo box.

rossum


But that will make the value visable, that is not what I want to do. I
thought there was a way to have a value and a bit of text seperate. ie value
is what I use in the program to determine what was chosen and text is
what is visable to the user.

Gav

The ultimate truth is that there is no ultimate truth

Nov 17 '05 #3
Gav,

One way you can do this is to create a class for the combo item. For
example,

public class MyComboBoxItem
{
private string _name;
private int _value;

public MyComboBoxItem(string name, int value)
{
_name = name;
_value = value;
}

public override string ToString()
{
return _name;
}
}

Now, add this to your combo box. For example,

comboBox1.Items.Add(new MyComboBoxItem("Test", 134));

The item will display in the combo box based on how you implemented
ToString(). In this case, the name is displayed. But, when the object is
selected, you have this object, which may contain as much data as you need.

In many cases you will already have a data class that has all the data in it
you need, but the ToString() method does not display what you want. If you
are not using the ToString() method, and it makes sense, add an override as
shown above to that class, and just stick your data class in the ComboBox.
Or, you can create a class that hold your data class with just the method to
override ToString(), and a method to get at your data class, like a Value
property.

Hope this helps...

Frisky

"Gav" <ga************@nospam.portakabin.com> wrote in message
news:da**********@newsreaderg1.core.theplanet.net. ..

"rossum" <ro******@coldmail.com> wrote in message
news:nl********************************@4ax.com...
On Thu, 7 Jul 2005 13:19:13 +0100, "Gav"
<ga************@nospam.portakabin.com> wrote:
I am using VS 2005 and am trying to add items to a combo box using C#. I
know how to add simple text items but I am trying to add a value and some
text ie.

Value Text
A First Text
B Second Text

I've seen examples of ListItem but I can't seem to get this to work, can
anybody offer any advice.

thanks

Gavin


Convert the value to a string, concatenate it with the text that you
want and add the whole thing to your combo box.

rossum


But that will make the value visable, that is not what I want to do. I
thought there was a way to have a value and a bit of text seperate. ie
value
is what I use in the program to determine what was chosen and text is
what is visable to the user.

Gav

The ultimate truth is that there is no ultimate truth


Nov 17 '05 #4
Gav
Thanks very much, thats what I was after. I just thought there was maybe
something standard to do it. :o) I'll give it a go tonight... :o)

Gav

"Frisky" <Fr***********@NorthPole.Net> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl...
Gav,

One way you can do this is to create a class for the combo item. For
example,

public class MyComboBoxItem
{
private string _name;
private int _value;

public MyComboBoxItem(string name, int value)
{
_name = name;
_value = value;
}

public override string ToString()
{
return _name;
}
}

Now, add this to your combo box. For example,

comboBox1.Items.Add(new MyComboBoxItem("Test", 134));

The item will display in the combo box based on how you implemented
ToString(). In this case, the name is displayed. But, when the object is
selected, you have this object, which may contain as much data as you
need.

In many cases you will already have a data class that has all the data in
it you need, but the ToString() method does not display what you want. If
you are not using the ToString() method, and it makes sense, add an
override as shown above to that class, and just stick your data class in
the ComboBox. Or, you can create a class that hold your data class with
just the method to override ToString(), and a method to get at your data
class, like a Value property.

Hope this helps...

Frisky

"Gav" <ga************@nospam.portakabin.com> wrote in message
news:da**********@newsreaderg1.core.theplanet.net. ..

"rossum" <ro******@coldmail.com> wrote in message
news:nl********************************@4ax.com...
On Thu, 7 Jul 2005 13:19:13 +0100, "Gav"
<ga************@nospam.portakabin.com> wrote:

I am using VS 2005 and am trying to add items to a combo box using C#. I
know how to add simple text items but I am trying to add a value and
some
text ie.

Value Text
A First Text
B Second Text

I've seen examples of ListItem but I can't seem to get this to work, can
anybody offer any advice.

thanks

Gavin
Convert the value to a string, concatenate it with the text that you
want and add the whole thing to your combo box.

rossum


But that will make the value visable, that is not what I want to do. I
thought there was a way to have a value and a bit of text seperate. ie
value
is what I use in the program to determine what was chosen and text is
what is visable to the user.

Gav

The ultimate truth is that there is no ultimate truth



Nov 17 '05 #5

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

Similar topics

8
by: ttan | last post by:
//I had a class public class PERSON { PERSON(string LName, string FName, string age) { m_last = LName; m_first = FName; m_age = age; }
3
by: Nikolay Petrov | last post by:
I have a Combo box, binded to a dataset With cmbCompany .DataSource = dsSpecContact .DisplayMember = "Companies.CompanyName" .ValueMember = "Companies.CompanyID" End With What I want is to...
6
by: vb | last post by:
Hi, I am new to .Net. I am using a Combo Box in my windows forms. I am adding the items by creating the instances and adding the same to the list. My questions/doubts are: 1. If I have 25 to...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
1
by: stinehelferw | last post by:
I need a right-click menu on a form list control with combobox controls. I used View->Toolbars->Customize to create a toolbar. I added ComboBox through commands tab. I set properties to Popup...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
0
by: mollyf | last post by:
I've got a combo box that I want to bind a collection to. I got the error "Complex Data Binding accepts as a data source either an IList or an IListSource" so I tried using the BindingSource,...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
2
by: 794613 | last post by:
Hello, thank you for reading this question, and possibly (hopefully) pointing me in the right direction. I've got a basic database that I've been working on, and adding items as I see necessary. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.