472,978 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 software developers and data experts.

Show code and name in combobox picklist

I need select from list containing product code and name but show only code
in combo textbox.

Tried code below but dropdown list shows only code.
How to display code AND name in selection list ?

Andrus.
using System.Collections.Generic;
using System.Windows.Forms;

class testForm : Form
{
testForm()
{
ComboBox cm = new ComboBox() { DisplayMember = "DisplayMember",
ValueMember = "Code" };
List<ProductprodList = new List<Product>();
prodList.Add(new Product() { Code = "001", Name = "Wine", Cb =
cm });
prodList.Add(new Product() { Code = "002", Name = "Beer", Cb =
cm });
cm.DataSource = prodList;
cm.DataBindings.Add("SelectedValue", prodList, "Code");
Controls.Add(cm);
}

class Product
{
public ComboBox Cb;
public string Code { get; set; }
public string Name { get; set; }

public string DisplayMember
{
get
{
if (Cb.DroppedDown)
return Code + " " + Name;
else
return Code;
}
}
}

static void Main()
{
Application.Run(new testForm());
}
}

Jun 27 '08 #1
4 2184
On May 28, 8:09*am, "Andrus" <kobrule...@hot.eewrote:
How to display code AND name in selection list ?
You need to override the ToString() for the Product class to display
it the way you want to see it in the combobox.
Jun 27 '08 #2
This is a great learning example, so thanks for posting it. If anyone can
answer my questions, I'd be grateful.

I see that you're creating a combobox in code; my preference would be to
have this in the xaml. What is your reasoning?

When I try to assign to mycombo.DataSource, vs2k8 says comboboxes don't have
DataSource as member. What am I missing?

Jun 27 '08 #3
I see that you're creating a combobox in code; my preference would be to
have this in the xaml. What is your reasoning?

When I try to assign to mycombo.DataSource, vs2k8 says comboboxes don't
have DataSource as member. What am I missing?
This is winforms, not wpf.

Andrus.

Jun 27 '08 #4

Israel,
>You need to override the ToString() for the Product class to display
it the way you want to see it in the combobox.
Thank you.
I tried code below but dropdown list shows only code.
How to force it to show both code and name ?

Andrus.

using System.Collections.Generic;
using System.Windows.Forms;

class testForm : Form
{
testForm()
{
ComboBox cm = new ComboBox()
{
ValueMember = "Code"
};
Lis<ProductprodList = new List<Product>();
prodList.Add(new Product() { Code = "001", Name = "Wine", Cb =
cm });
prodList.Add(new Product() { Code = "002", Name = "Beer", Cb =
cm });
cm.DataSource = prodList;
cm.DataBindings.Add("SelectedValue", prodList, "Code");
Controls.Add(cm);
}

class Product
{
public string Code { get; set; }
public string Name { get; set; }

public override string ToString()
{
return Code + " " + Name;
}
}

static void Main()
{
Application.Run(new testForm());
}
}

Jun 27 '08 #5

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

Similar topics

3
by: Idelso Quintero | last post by:
Hi everyone: Could someone look into what could possibly be wrong in these lines of code? It looks all good to me but for some reason the combo box that is created, is not shown with all the...
12
by: Mike Brashars | last post by:
Hi all, I have been searching for a week and am unable to find and example to "Populate picklist from directory and return file name". I have a php script that reads a log file and plots a...
4
by: news.online.no | last post by:
In a query, I need too be able to show if a parent record has a child record. Using the query in a combo box too select record in a form. Thanks :)
9
by: Bob Alston | last post by:
In 2002, "GrayJay" posted the following code: I did this in a jazz record catalogue to find composers - On a form "frmComposers" Create a text box - txtFindComposer, and add the following sub...
8
by: gumi | last post by:
Hi, I am looking for code for a alarm clock program that pops up a messege to be used as part of my VB.Net class project. Any help is very much appreciated. Thanks
8
by: Supra | last post by:
how do they get an icon to show up in the URL address box, bookmark?" I got combobox1_drawitem working. I posted code recently ago. i do understand when i looked in view page code... and i see...
10
by: muddasirmunir | last post by:
I had two tables, one new customers and the other new suppliers. Now I want to show all the customer and suppliers in one combo box. I am using a recordset by coding recordset1.open...
1
by: Andrus | last post by:
I'm creating Winforms application. User enters first characters of customer name to Combobox in DataGridView or in form. I need to present list of matcing customer names and allow user to...
18
by: Andrus | last post by:
Marc, Thank you very much. I have issue on implementing add row properly using this. User presses down arrow in last row in grid starting adding new row. Then user changes its mind desiding...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.