473,882 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How To Add (Item and Value) Into ComboBox ?

6 New Member
Hi Everyone,

Can you guys assist me on how to write code to manually add item into a combobox. I am using VS2005 program with C# language for Window Apps.

Text | Value
===============
Jan M01
Feb M02

My current code are stated below:
comboBox1.Items .Add("Jan");
comboBox1.Items .Add("Feb");

This way only able to add the TEXT, not the VALUE. I need to access the VALUE as well, for calculation purpose. I cannot display VALUE to the user because it's not meaningful.

If it's done correctly, I will able to retrieve value from the code below:
comboBox1.Selec tedValue
(if user select Jan, it will return M01)

My program is develop for Win App and WM, so I cannot use ListItem which only for available for ASP.NET.

Appreciate help from you guys.

Thanks,
Regards,
Iverson.
Aug 22 '07 #1
5 44852
dip_developer
648 Recognized Expert Contributor
Hi Everyone,

Can you guys assist me on how to write code to manually add item into a combobox. I am using VS2005 program with C# language for Window Apps.

Text | Value
===============
Jan M01
Feb M02

My current code are stated below:
comboBox1.Items .Add("Jan");
comboBox1.Items .Add("Feb");

This way only able to add the TEXT, not the VALUE. I need to access the VALUE as well, for calculation purpose. I cannot display VALUE to the user because it's not meaningful.

If it's done correctly, I will able to retrieve value from the code below:
comboBox1.Selec tedValue
(if user select Jan, it will return M01)

My program is develop for Win App and WM, so I cannot use ListItem which only for available for ASP.NET.

Appreciate help from you guys.

Thanks,
Regards,
Iverson.
I found this solution in a forum.......hop e it will help you........

The Add method expects an Object. So one way to do this is to create your own MyListBoxItem class. This class should expose at least two properties: a Value property and a Description property (you can choose other names, but these names are in synch with other .NET objects).

Once you have the new class, you should add instances of this class to the Items collection. If you provide a constructor that accepts a Value and a Description, you can do something like this:

Expand|Select|Wrap|Line Numbers
  1. comboBox1.Items.Add(new MyListboxItem(147, masteryacademy"));
  2.  
Finally, you should tell the combo box what properties to use on the object you have added for the Value and Display members:

Expand|Select|Wrap|Line Numbers
  1.  comboBox1.DisplayMember = "Description";
  2. comboBox1.ValueMember = "Value";
  3.  
Remember that your combobox now holds a collection of MyListboxItem objects. So, if you get the SelectedItem, you get a generic Object that you need to cast to a MyListboxItem object before you can retrieve its Value and Description properties.The code will look like

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim SelectedItem As MyListboxItem = CType(combobox1.SelectedItem, MyListboxItem)
  3. Dim SelectedItemDescription As String = SelectedItem.Description ' returns George. 
  4.  
Aug 22 '07 #2
masteryacademy
6 New Member
Hi dip_developer,

Thank you for the prompt response. Appreciate that.
I still not able to get the SelectedValue. It still return NullReferenceEx ception.

Maybe I didn't code it correctly when you ask me to create my own MyListBoxItem class. Can you show me the code how to do that?

Thanks,
Regards,
Iverson.
Aug 23 '07 #3
dip_developer
648 Recognized Expert Contributor
Hi dip_developer,

Thank you for the prompt response. Appreciate that.
I still not able to get the SelectedValue. It still return NullReferenceEx ception.

Maybe I didn't code it correctly when you ask me to create my own MyListBoxItem class. Can you show me the code how to do that?

Thanks,
Regards,
Iverson.
I think giving you the full code will not do any justice......Pl ease read about class/object/property/constructor .........the OOP concept.......h ope you understand..... ...I am giving you the basic class (with property)descri ption code ............... ............... ......
the class descrption code will look like...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class MyListBoxItem 
  3. Public mVal As Integer
  4. Public mDesc As String
  5. Public Property myValue() As Integer
  6. Get
  7. Return mVal 
  8. End Get
  9. Set(ByVal Value As String)
  10. mVal = Value
  11. End Set
  12. End Property
  13. Public Property myDescription() As String
  14. Get
  15. Return mDesc 
  16. End Get
  17. Set(ByVal Value As String)
  18. mDesc = Value
  19. End Set
  20. End Property
  21. End Class
  22.  
Aug 23 '07 #4
masteryacademy
6 New Member
Hi dip_developer,

Thank you for answer. It's help alot.

Cheers,
Iverson.
Aug 23 '07 #5
clintmannar
1 New Member
A simple way is to use Dictionary Object

Dictionary comboSource = new Dictionary();
comboSource.Add ("1", "Sunday");
comboSource.Add ("2", "Monday");

comboBox1.DataS ource = new BindingSource(c omboSource, null);
comboBox1.Displ ayMember = "Value";
comboBox1.Value Member = "Key";


Full Source...Adding Text and Value to ComboBox

Clint
Oct 31 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9510
by: nickyw | last post by:
Hi, Can anyone help, I want to get a reference to a combobox item based on the item's value rather than the text (basically what I am asking is, is there a function like 'FindStringExact' for values) , does anyone know how to do this? Thanks, Nicky
2
10273
by: farseer | last post by:
Hi, I have a combobox who's data source i set to an array of objects (call it MyObject). these objects have get properties: key, value, descr. i set ValueMember to "key", DisplayMember to "descr", and datasource to the array of MyObjects. How can i set the selected value of that combobox based on the ValueMember OR how can i find the index of an item in the combobox BY ValueMember?
4
6708
by: Michael Turner | last post by:
Hi I am having a problem with a combobox the values are generated from a recordset, I need to beable to change the selected item to the value specified in a configuration file, I have stored the value as a varible. I am using the code below but it isn't working any ideas am I using the right method. combobox1.selecteditem = getcolvalue(0) Any help appreciated.
2
2865
by: Nate | last post by:
I have used the feedback on this issue to remedy my comboboxes showing the first item on the list when a new record is added to the binding context --- Me.BindingContext(dsOrders, "tblOrders").AddNew() Me.ComboBox1.SelectedIndex = -1 but... how do we deal with the same issue during record navigation? When I move forward or backward onto a null field the combo defaults to the first item on the list again?
4
13470
by: Drum2001 | last post by:
I have a combobox that is populated from a button on the form. One pushes this button and the following happens: !! = Me.ItemName So the combobox no shows whatever was in Me.ItemName. Here is my question... How do I autoselect this item? I know the following code will select
3
94293
by: sklett | last post by:
I'm changing from a DataGrid to a DataGridView and have run across a problem. The items that are bound to the DataGrid have an int Property that represents a primary key of a lookup table in my database. For example: table JobTypes 1 | Manager 2 | Controller 3 | Supervisor table Employee
4
6668
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 common/standard .NET control accepting an Object as a parameter type? In Web Forms, there is a ListItem object that can be passed in to add/retrieve objects from a DropDownItems collection. I searched Google groups, and all the solutions I'm finding...
6
5210
by: Smokey Grindle | last post by:
Say I have a combo box with the following simple object Public class MyObject public ID as integer public Name as string public overrides sub ToString() as string return name end sub end Class
7
3589
by: Brad Pears | last post by:
I have something strange going on - pretty sure it used to work before - and now it does not... Why does the following code not clear a combo box? Me.cboLocation.Text = String.Empty OR Me.cboLocation.Text = ""
1
5247
by: sbandalli | last post by:
Hello, I have a Datagridview which has a combobox,and 2 textbox, The combobox is bound to a Datasource(Database Sql Server and the table name is Category) ,and Datagridview is not bounded to any datasource. When the user selects an item in the combobox,and enters the item(text) in the 2 text box , I want all the 3(combobox selected item, and the entered text in the 2 textbox) of them to store it a a table name called...
0
9777
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,...
0
11108
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10725
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...
1
10830
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
10403
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...
1
7956
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
5781
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
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.