473,769 Members | 3,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1422
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.Displ ayMember = "myDisplayMembe r";
comboBox1.Value Member = "myValueMember" ;
comboBox1.DataS ource = ds.Tables[0];

In the SelectedIndexCh anged event of the ComboBox you can then do:

object myValueMember = comboBox1.Selec tedValue;
// 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.Dat aSource property (System.Windows .Forms).

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

"David Meier" <Da********@dis cussions.micros oft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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(minute s, 0);
}
public string ToString()
{
string sTime = "";
int iMinutes = m_iMinutes;

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

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

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

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

"David Meier" <Da********@dis cussions.micros oft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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(minute s, 0);
}
public string ToString()
{
string sTime = "";
int iMinutes = m_iMinutes;

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

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

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

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

"David Meier" <Da********@dis cussions.micros oft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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********@dis cussions.micros oft.com> wrote in message
news:37******** *************** ***********@mic rosoft.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(minute s, 0);
}
public string ToString()
{
string sTime = "";
int iMinutes = m_iMinutes;

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

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

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

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

"David Meier" <Da********@dis cussions.micros oft.com> wrote in message
news:77******** *************** ***********@mic rosoft.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
1969
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 combobox for Models. In both comboboxes I use the NotInList event and code to allow users to enter Makes and Models that are not in the database. When the form is open, I want to limit the users to entering new Makes and Models in the comboboxes and not...
2
2041
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 second field (Prospect Name). The user then uses the subform to enter a document location. The problem is this:
0
2216
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 correctly, posting to the wrong forum, or committing some other sort of faux pas. My team is developing a Windows Forms application using VS 2005 with SQL Server 2005 on the back end and we're having a problem using ComboBoxes data bound to lookup...
2
11457
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: Fruit 2: Fruit 3:
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8862
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.