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

ListItem not available in win forms

RR
Hello,

I'm simply trying to add items with values to a combox at runtime in a
windows application. The code below does not work in windows development.

comboBox.Items.Add(new ListItem("text", value));

How can I do this in win forms?
Thanks!
May 23 '06 #1
3 11625

The concept you are missing, is that in winforms.. you put Objects into the
combobox.

(I know this, because I didn't get it at first either)

You add objects... like

combobox.Items.Add(new Emp("Smith","John");

You can override the ToString() method..
Or you set which property you want to show up as text.
...

When you pull the selected item, you cast it back to the type of object you
put in there.

Emp ep = myComboBox.SelectedItem as Emp;
if (null!=ep)
{
// do stuff with ep
}


"RR" <RR@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
Hello,

I'm simply trying to add items with values to a combox at runtime in a
windows application. The code below does not work in windows development.

comboBox.Items.Add(new ListItem("text", value));

How can I do this in win forms?
Thanks!

May 23 '06 #2
RR
really!

No wonder why I've had such poor progress with this. Is there a simple way
to create the object with just a display string and an int value for the
combo box? I'm not sure I understand your code comments below.

Thanks for yolur help.

"sloan" wrote:

The concept you are missing, is that in winforms.. you put Objects into the
combobox.

(I know this, because I didn't get it at first either)

You add objects... like

combobox.Items.Add(new Emp("Smith","John");

You can override the ToString() method..
Or you set which property you want to show up as text.
...

When you pull the selected item, you cast it back to the type of object you
put in there.

Emp ep = myComboBox.SelectedItem as Emp;
if (null!=ep)
{
// do stuff with ep
}


"RR" <RR@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
Hello,

I'm simply trying to add items with values to a combox at runtime in a
windows application. The code below does not work in windows development.

comboBox.Items.Add(new ListItem("text", value));

How can I do this in win forms?
Thanks!


May 23 '06 #3
You can put anything in a combobox, be it a string, int, float or a custom
class. The combobox will call the ToString method of the object to find out
what text to display. In your case you'ld want a custom class looking
something like this:

public class ListItem
{
public int Value;
public string Text;
public ListItem(string text, int value)
{
Value = value;
Text = text;
}
public override string ToString()
{
return Text;
}
}

You should probably use properties instead of public fields though, but you
get the idea.

/claes

"RR" <RR@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
really!

No wonder why I've had such poor progress with this. Is there a simple way
to create the object with just a display string and an int value for the
combo box? I'm not sure I understand your code comments below.

Thanks for yolur help.

"sloan" wrote:

The concept you are missing, is that in winforms.. you put Objects into
the
combobox.

(I know this, because I didn't get it at first either)

You add objects... like

combobox.Items.Add(new Emp("Smith","John");

You can override the ToString() method..
Or you set which property you want to show up as text.
...

When you pull the selected item, you cast it back to the type of object
you
put in there.

Emp ep = myComboBox.SelectedItem as Emp;
if (null!=ep)
{
// do stuff with ep
}


"RR" <RR@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
> Hello,
>
> I'm simply trying to add items with values to a combox at runtime in a
> windows application. The code below does not work in windows
> development.
>
> comboBox.Items.Add(new ListItem("text", value));
>
> How can I do this in win forms?
> Thanks!


May 24 '06 #4

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

Similar topics

0
by: Dr. Paul Caesar - CoullByte (UK) Limited | last post by:
Just been reading an interesting article about Windows Installer and it has got be thinking. The article is at...
7
by: Steve | last post by:
When I bring up my database (access 97) I can't select the "DESIGN" or "NEW" in my Forms. Table and Query seem to be ok. Is there a way to fix it? Thank Steve
7
by: Amil | last post by:
Hi all, I am using VS 2005 Beta 2. Which assembly (namespace?) should I use for the ListItem class? I don't have the Help files installed on my notebook for space reasons; I use MSDN2 online...
1
by: Zack Sessions | last post by:
I am trying to implement a ListBox control in VB.NET. I am using the sample code right from the online help, but I am stuck on simply declaring a list item variable. The datatype ListItem simply...
2
by: Todd Sparks | last post by:
Is there any way that I can get a list of all of the forms that are in my project? I don't just need a list of open forms or anything like that, I need to know all of the forms. I am writing my...
0
by: Sergio E. | last post by:
Hello, I have a problem with masterpages and forms security. I made a new Web site, in which I have my page login.aspx as the homepage , a master page with only a sitemappath object in it, the...
2
devonknows
by: devonknows | last post by:
Is there anyway to list all the forms that in the application itself? I know if you do Dim oFrm as Form For Each oFrm in Forms msgbox oFrm.Name Next oFrm It returns a list of all the open...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
6
by: Ryno Bower | last post by:
Hello. I want to integrate roles and permissions for a specific operator after they log in. Is there a way to handle this. If so please ldt me know and ill give more details. Please answer. Thanks....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.