473,386 Members | 1,610 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 confusion

Hi,

I have a combo box in a Windows Forms app (c# VS2003, .Net 1.1) where I
am adding items at runtime using the Items.Add method.

I need to be able to also set the Value for each item in the same way.

I'm sure it must be possible, as I could do this in the Win32 API, but I
can't work out how to do it in .net!
Any help reatly appreciated.
Thankyou.
Paul Cheetham
May 4 '06 #1
4 1569

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:u3**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have a combo box in a Windows Forms app (c# VS2003, .Net 1.1) where I am
adding items at runtime using the Items.Add method.

I need to be able to also set the Value for each item in the same way.

I'm sure it must be possible, as I could do this in the Win32 API, but I
can't work out how to do it in .net!
Any help reatly appreciated.
Thankyou.
Paul Cheetham


You can either use an existing IList or IListSource instance, or create one
of your own :)

Note: DataTable implements IList :)

HTH,
Mythran

May 4 '06 #2
RR
I have the exact issue. I'm developing a windows applicaion in VS2005 where I
need to add a value and a display text at runtime to a combo box. I'm not
using a dataview or a datagrid. Was there a solution?
Thanks!

"Mythran" wrote:

"Paul Cheetham" <PA******@dsl.pipex.com> wrote in message
news:u3**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have a combo box in a Windows Forms app (c# VS2003, .Net 1.1) where I am
adding items at runtime using the Items.Add method.

I need to be able to also set the Value for each item in the same way.

I'm sure it must be possible, as I could do this in the Win32 API, but I
can't work out how to do it in .net!
Any help reatly appreciated.
Thankyou.
Paul Cheetham


You can either use an existing IList or IListSource instance, or create one
of your own :)

Note: DataTable implements IList :)

HTH,
Mythran

May 23 '06 #3
here is the solution
paste this in .cs file and ensure there is no other Main function in
project
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace MyListControlSample
{
public class ListBoxSample3 : Form
{
private TextBox textBox1 = new TextBox();
private ComboBox cmb = new ComboBox();

[STAThread]
static void Main()
{
Application.Run(new ListBoxSample3());
}

public ListBoxSample3()
{

this.ClientSize = new Size(292, 181);
this.Text = "ListBox Sample3";

cmb.Location = new Point(24, 16);
cmb.Name = "cmb";
cmb.Size = new Size(232, 130);

textBox1.Location = new Point(24, 160);
textBox1.Name = "textBox1";
textBox1.Size = new Size(240, 24);
this.Controls.AddRange(new Control[] { cmb, textBox1 });

// Populates the combo box using DataSource.
// DisplayMember is used to display just the long name of
each state.
ArrayList USStates = new ArrayList();
USStates.Add(new USState("Alabama", "AL"));
USStates.Add(new USState("Washington", "WA"));
USStates.Add(new USState("West Virginia", "WV"));
USStates.Add(new USState("Wisconsin", "WI"));
USStates.Add(new USState("Wyoming", "WY"));

cmb.SelectedIndexChanged += new
EventHandler(cmb_SelectedIndexChanged);
cmb.DataSource = USStates;
cmb.DisplayMember = "LongName";
cmb.ValueMember = "ShortName";

}

void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmb.SelectedIndex != -1)
textBox1.Text = cmb.SelectedValue.ToString();
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// ListBoxSample3
//
this.ClientSize = new System.Drawing.Size(658, 347);
this.Name = "ComboSample3";
this.ResumeLayout(false);

}
}

public class USState
{
private string myShortName;
private string myLongName;

public USState(string strLongName, string strShortName)
{

this.myShortName = strShortName;
this.myLongName = strLongName;
}

public string ShortName
{
get
{
return myShortName;
}
}

public string LongName
{

get
{
return myLongName;
}
}

public override string ToString()
{
return this.ShortName + " - " + this.LongName;
}
}

}
I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 23 '06 #4
"RR" <RR@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...
I have the exact issue. I'm developing a windows applicaion in VS2005 where
I
need to add a value and a display text at runtime to a combo box. I'm not
using a dataview or a datagrid. Was there a solution?


I think the solution is that the ComboBox items are objects (NOT strings),
so you can encapsulate your value in an object whose ToString method gives
the display text you want.

Chris Jobson
May 23 '06 #5

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

Similar topics

0
by: eye5600 | last post by:
In the form designer, a ComboBox on a TabPage becomes unselectable via the mouse. (Any attempt to interact with the ComboBox via mouse selects the TabPage) I can select something else, and tab to...
2
by: shumaker | last post by:
I have a combobox that is very much like the one found in the RSS project here: http://msdn.microsoft.com/vstudio/express/visualCSharp/learning/ My projectNameComboBox basically is filled with a...
2
by: murdock | last post by:
I am having an issue with graphical corruption upon re-selecting a combobox which upon 'selected index change' event should list file names in that folder in a separate combobox. Here is the code:...
4
by: Jerad Rose | last post by:
I'm baffled by this -- is there not a typed object used for ComboBox Items? Best I can tell, all of the methods for ComboBox that accept an Item are of type Object. Why in the world is a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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:
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.