473,657 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.Generic;
using System.Windows. Forms;

class testForm : Form
{
testForm()
{
ComboBox cm = new ComboBox() { DisplayMember = "DisplayMember" ,
ValueMember = "Code" };
List<Productpro dList = new List<Product>() ;
prodList.Add(ne w Product() { Code = "001", Name = "Wine", Cb =
cm });
prodList.Add(ne w Product() { Code = "002", Name = "Beer", Cb =
cm });
cm.DataSource = prodList;
cm.DataBindings .Add("SelectedV alue", 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 2199
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.DataSou rce, 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.DataSou rce, 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.Collecti ons.Generic;
using System.Windows. Forms;

class testForm : Form
{
testForm()
{
ComboBox cm = new ComboBox()
{
ValueMember = "Code"
};
Lis<Productprod List = new List<Product>() ;
prodList.Add(ne w Product() { Code = "001", Name = "Wine", Cb =
cm });
prodList.Add(ne w Product() { Code = "002", Name = "Beer", Cb =
cm });
cm.DataSource = prodList;
cm.DataBindings .Add("SelectedV alue", 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
2235
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 properties (attributes) set. Actually the message box (alert) that is used to review the content of the SELECT statement, shows that the only attributes set are size = 1 and id = theComboBox. The other two statements, refering to name and style, are...
12
2321
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 graph. Right now, the log name is hard coded. The logs are archived each day in the form of say ddmmyy.log and I would like to put a drop down list in the script that would allow my script to run off the "log" picked or default to the current "hard...
4
2625
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
3394
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 Private Sub txtFindComposer_Change() Requery Me!.SetFocus
8
2494
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
2817
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 example like this.... '<link rel="icon" href="/favicon.ico" type="image/ico"> '<img src="/intl/en_ca/images/logo.gif" width=276 height=110 alt="Google"> and some more like powerbuilder or powerpoint...some used css, etc regards,
10
1821
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 "select name from newcustomer union all select name from newsupplier" combobox1.rowsource = recordset1 combobox1.listfield = "name"
1
2616
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 select one customer. This can be done in several ways: 1. Fill AutoComplete Custom source, and allow user to select from this.
18
6043
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 that new row should not added and presses up arrow. DataGridView does not show this unfinished row anymore.
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
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...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
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
4168
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
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
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.