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

ComboBox

I'm used to setting the Text property and Value property of a ComboBox. I
do not see this option for C# 2.0. Am I missing something?

Basically, I'm trying to loop thru an ADO.NET reader object and populating
the combobox with the "Text" and "Identity" column for a database.
Therefore, I need the ID so I can update the correct row.

Mar 22 '06 #1
4 4873
ComboBox never had this... not even in 1.1. The
ComboBox.ObjectCollection contains objects, not a specialized
ComboBoxItem that has Text and a Value. I always found this annoying,
since ListView had ListViewItem that had its text and a Tag field in
which you could store a key.

My impression was that you were supposed to solve this problem using
data bindings, but data bindings were a horrid mess in 1.1 (at least I
thought so).

Mar 22 '06 #2
Thanks -- I'm coming from a VB6 background. For some reason I assumed .NET
had it as the Data Binding to a Combobox seemed to have a selection for
setting this... guess it was an object or something.

In the meantime, I solved this by creating a simple class to hold my two
values. I didn't know if this was the normal way to handle this. Seemed
like a lot of work to me for something that should be relatively simple.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
ComboBox never had this... not even in 1.1. The
ComboBox.ObjectCollection contains objects, not a specialized
ComboBoxItem that has Text and a Value. I always found this annoying,
since ListView had ListViewItem that had its text and a Tag field in
which you could store a key.

My impression was that you were supposed to solve this problem using
data bindings, but data bindings were a horrid mess in 1.1 (at least I
thought so).

Mar 23 '06 #3
One thing that you can do is create an object that contains a property
returning text that should show in the ComboBox. Then you set the
ComboBox's DisplayMember to the name of that property, and populate the
ComboBox with these objects, rather than with strings.

I tried this a while back and it caused problems when clearing the
ComboBox of items and repopulating it (which I had to do in some
circumstances). I can't recall the precise details, but it didn't work
as cleanly as it could have.

Nonetheless, it's an option, particularly if you don't need to
repopulate the ComboBox.

Mar 23 '06 #4
On Wed, 22 Mar 2006 17:04:47 -0600, "dm1608" <dm****@spam.net> wrote:
I'm used to setting the Text property and Value property of a ComboBox. I
do not see this option for C# 2.0. Am I missing something?

Basically, I'm trying to loop thru an ADO.NET reader object and populating
the combobox with the "Text" and "Identity" column for a database.
Therefore, I need the ID so I can update the correct row.


Bruce is right there is no text and identity property in the .NET ComboBox.

One way around this is to create a class that you load with the reader. In fact
you can load a class that *IS* the object you are working with. All you need to
do is override the ToString() method of the class so it returns the text you
want the user to see. Then you can use the data contained by the class as you
want.

Here's an example:

// the display class can be used in any project needing to use combo boxes.

using System;
using System.Collections.Generic;
using System.Text;

namespace TestWindow
{
class DisplayObject
{
private string _displayText;
private int _id;

public DisplayObject()
{
}

public DisplayObject(int id, string displayText)
{
_displayText = displayText;
_id = id;
}

public string Text
{
get
{
return _displayText;
}
set
{
_displayText = value;
}
}

public int ID
{
get
{
return _id;
}
set
{
_id = value;
}
}

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

// in the dialog containing the ComboBox...

private void TestDialog_Load(object sender, EventArgs e)
{
DisplayObject disp = new DisplayObject(1, "Test1");
comboBox1.Items.Add(disp);
disp = new DisplayObject(2, "Test2");
comboBox1.Items.Add(disp);
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DisplayObject disp = (DisplayObject)comboBox1.SelectedItem;
int id = disp.ID;
string text = disp.Text;
}

HTH

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 23 '06 #5

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

Similar topics

13
by: Mr. B | last post by:
Here's the situation... You've a combobox with Items already added. Say they look like this (or even lines of text): 10-00-232 10-00-256 10-01-006 10-01-213 10-02-200
7
by: Nicolae Fieraru | last post by:
Hi All, I am trying to change the rowsource of a combobox when I click on it. I played with many events, associated with the form and the combobox, but still haven't figured out what is the way...
8
by: Zlatko Matię | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
1
by: anonymous | last post by:
I've been trying to put a them, please help me out. Here's the major parts of my code: public Form1() { DataSet myDataSet = new DataSet("myDataSet"); DataTable testTable = new...
3
by: TT (Tom Tempelaere) | last post by:
Hay there, I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one (I hope .NET 2.0 supplies one). I based it on this article:...
2
by: Don | last post by:
I've looked high and low for some code that will allow me to have a combobox with a flat borderstyle. I found a few examples, but nothing that was really usable for me. I had the following...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
1
by: polocar | last post by:
Ciao a tutti, leggendo qua e lą per il forum ho scoperto che non sono l'unico ad avere questo problema. Se si inserisce un controllo ComboBox in un form di C#, non č possibile impostare la sua...
5
by: Rich | last post by:
Hello, I have a search application to search data in tables in a database (3 sql server tables). I populate 2 comboboxes with with data from each table. One combobox will contain unique...
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:
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
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?
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,...

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.