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

Data in ComboBoxes

How can data be stored in comboboxes similar to html pulldowns? I know I can
access the data of the combobox either by the items text or by its index.
However, I would like to have a pulldown representing something like:

Text: 1 minute; Value: 1
Text: 5 minutes; Value: 5
Text: 1 hour; Value: 60

Thanks. Dave.
Nov 16 '05 #1
4 1406
You can do something like this if you use data binding. Simply setup your
comboBox as follows:

// fill your comboBox with data from a database
// SQL - select myValueMember, myDisplayMember from someTable
// *** code to fill dataset ***

comboBox1.DisplayMember = "myDisplayMember";
comboBox1.ValueMember = "myValueMember";
comboBox1.DataSource = ds.Tables[0];

In the SelectedIndexChanged event of the ComboBox you can then do:

object myValueMember = comboBox1.SelectedValue;
// or if you know the data type
int myValueMember = (int)comboBox1.SelectedValue;

Since ComboBoxes can be bound to any component that implements IListSource
or IList your imagination is the limit. The MSDN has a good example listed
under the ListControl.DataSource property (System.Windows.Forms).

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
How can data be stored in comboboxes similar to html pulldowns? I know I can access the data of the combobox either by the items text or by its index.
However, I would like to have a pulldown representing something like:

Text: 1 minute; Value: 1
Text: 5 minutes; Value: 5
Text: 1 hour; Value: 60

Thanks. Dave.

Nov 16 '05 #2
The way I've handled this sort of thing is to create a Class (or Struct)
with a ToString() method that gives you the text you want. If you store
objects of this class in the ComboBox, it will display the text you want,
but when you get the selected item, you can cast it to your Class and access
member variables containing the underlying data. It's a cool feature of
DotNet that the item type is object and the control uses the ToString()
method to figure out how to display it.

Generally speaking, you probably want to write some code that determines how
to display a particular value (number of minutes). The only place to put
this is inside of a class, so this works out fine. Off the top of my head,
you could use something like this:

public class MyTimeSpan
{
private int m_iMinutes;
public MyTimeSpan(int minutes)
{
m_iMinutes = Math.Max(minutes, 0);
}
public string ToString()
{
string sTime = "";
int iMinutes = m_iMinutes;

int iWeeks = iMinutes \ 10080;
sTime += GetFormattedString(iWeeks, "week", "weeks");
iMinutes -= iWeeks * 10080;

int iDays = iMinutes \ 1440;
sTime += GetFormattedString(iDays, "day", "days");
iMinutes -= iDays * 1440;

int iHours = iMinutes \ 60;
sTime += GetFormattedString(iHours, "hour", "hours");
iMinutes -= iHours * 60;

sTime += GetFormatttedString(iMinutes, "minute", "minutes");
return sTime;
}
private string GetFormattedString(int count, string singular, string
plural)
{
if( iCount <= 0 )
return "";
return string.Format("{0} {1}", iCount, ( iCount == 1 )? singular :
plural);
}
}

"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
How can data be stored in comboboxes similar to html pulldowns? I know I
can
access the data of the combobox either by the items text or by its index.
However, I would like to have a pulldown representing something like:

Text: 1 minute; Value: 1
Text: 5 minutes; Value: 5
Text: 1 hour; Value: 60

Thanks. Dave.

Nov 16 '05 #3
Thank you Tom. Your post cleared it up. I also found this link useful:
http://msdn.microsoft.com/library/de...embertopic.asp

"Tom Clement" wrote:
The way I've handled this sort of thing is to create a Class (or Struct)
with a ToString() method that gives you the text you want. If you store
objects of this class in the ComboBox, it will display the text you want,
but when you get the selected item, you can cast it to your Class and access
member variables containing the underlying data. It's a cool feature of
DotNet that the item type is object and the control uses the ToString()
method to figure out how to display it.

Generally speaking, you probably want to write some code that determines how
to display a particular value (number of minutes). The only place to put
this is inside of a class, so this works out fine. Off the top of my head,
you could use something like this:

public class MyTimeSpan
{
private int m_iMinutes;
public MyTimeSpan(int minutes)
{
m_iMinutes = Math.Max(minutes, 0);
}
public string ToString()
{
string sTime = "";
int iMinutes = m_iMinutes;

int iWeeks = iMinutes \ 10080;
sTime += GetFormattedString(iWeeks, "week", "weeks");
iMinutes -= iWeeks * 10080;

int iDays = iMinutes \ 1440;
sTime += GetFormattedString(iDays, "day", "days");
iMinutes -= iDays * 1440;

int iHours = iMinutes \ 60;
sTime += GetFormattedString(iHours, "hour", "hours");
iMinutes -= iHours * 60;

sTime += GetFormatttedString(iMinutes, "minute", "minutes");
return sTime;
}
private string GetFormattedString(int count, string singular, string
plural)
{
if( iCount <= 0 )
return "";
return string.Format("{0} {1}", iCount, ( iCount == 1 )? singular :
plural);
}
}

"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
How can data be stored in comboboxes similar to html pulldowns? I know I
can
access the data of the combobox either by the items text or by its index.
However, I would like to have a pulldown representing something like:

Text: 1 minute; Value: 1
Text: 5 minutes; Value: 5
Text: 1 hour; Value: 60

Thanks. Dave.


Nov 16 '05 #4
Glad to help David.
"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
Thank you Tom. Your post cleared it up. I also found this link useful:
http://msdn.microsoft.com/library/de...embertopic.asp

"Tom Clement" wrote:
The way I've handled this sort of thing is to create a Class (or Struct)
with a ToString() method that gives you the text you want. If you store
objects of this class in the ComboBox, it will display the text you want,
but when you get the selected item, you can cast it to your Class and
access
member variables containing the underlying data. It's a cool feature of
DotNet that the item type is object and the control uses the ToString()
method to figure out how to display it.

Generally speaking, you probably want to write some code that determines
how
to display a particular value (number of minutes). The only place to put
this is inside of a class, so this works out fine. Off the top of my
head,
you could use something like this:

public class MyTimeSpan
{
private int m_iMinutes;
public MyTimeSpan(int minutes)
{
m_iMinutes = Math.Max(minutes, 0);
}
public string ToString()
{
string sTime = "";
int iMinutes = m_iMinutes;

int iWeeks = iMinutes \ 10080;
sTime += GetFormattedString(iWeeks, "week", "weeks");
iMinutes -= iWeeks * 10080;

int iDays = iMinutes \ 1440;
sTime += GetFormattedString(iDays, "day", "days");
iMinutes -= iDays * 1440;

int iHours = iMinutes \ 60;
sTime += GetFormattedString(iHours, "hour", "hours");
iMinutes -= iHours * 60;

sTime += GetFormatttedString(iMinutes, "minute", "minutes");
return sTime;
}
private string GetFormattedString(int count, string singular, string
plural)
{
if( iCount <= 0 )
return "";
return string.Format("{0} {1}", iCount, ( iCount == 1 )? singular
:
plural);
}
}

"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
> How can data be stored in comboboxes similar to html pulldowns? I know
> I
> can
> access the data of the combobox either by the items text or by its
> index.
> However, I would like to have a pulldown representing something like:
>
> Text: 1 minute; Value: 1
> Text: 5 minutes; Value: 5
> Text: 1 hour; Value: 60
>
> Thanks. Dave.


Nov 16 '05 #5

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

Similar topics

4
by: Kathy | last post by:
In my custom menu one menu item opens a form to add new Makes to TblMakes and another menu item opens a form to add new Models to TblModels. I have a form that has a combobox for Makes and a...
2
by: edworboys | last post by:
I have designed a data entry form with a number of fields and a sub form. The first field (Country) is a combo box and the user selects a country. This, in turn reduces the number of options in the...
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
2
by: Matt | last post by:
Hi all, me again! :) I've now got an issue with combo boxes. Basically, I have a number of items that I want a user to pick from a single list. It's basically along the lines of: Fruit 1: ...
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: 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...
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.