473,757 Members | 8,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# - Adding values to combo box items.

16 New Member
Hey guys. I have a quick question... its very simple but i cant find a way to do it..
I need to add items to a combobox in a windows form... i need to have a name, and a value to go with it so they're associated with one another.
whats the easiest way i can do this? i thought of creating a new class to handle it, but im sure c# has a way of doing that already....

thanks for your help.
Jul 13 '08 #1
1 6514
todashah
26 New Member
Hi ! I have assume that u want to assign roll no to name of student which are
store in combo-box whenever user select name of student. Write following code
to do this.

First declare int[] j; in class defination of form.

then write following coding in page load event of form.
j = new int[comboBox1.Items .Count];
for (int i = 0; i < j.Length; i++)
{
j[i] = 0;
}

Write down following coding in selected index changed event of combo-box.

int id = comboBox1.Selec tedIndex;
if (id == 0 && j[id]==0)
{
comboBox1.Items .Insert(id, comboBox1.Selec tedItem + "-001");
comboBox1.Items .RemoveAt(++id) ;
j[--id] = 1;
}
else if (id == 1 && j[id] == 0)
{
comboBox1.Items .Insert(id, comboBox1.Selec tedItem + "-002");
comboBox1.Items .RemoveAt(++id) ;
j[--id] = 1;
}

Note: I have written else if block for first two items of combo-box. Now when u select these items from combo-box then number is associated with name.
Make appropriate changes as per your requirement.

Bye...Enjoy Coding
Jul 13 '08 #2

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

Similar topics

4
24751
by: Gav | last post by:
I am using VS 2005 and am trying to add items to a combo box using C#. I know how to add simple text items but I am trying to add a value and some text ie. Value Text A First Text B Second Text I've seen examples of ListItem but I can't seem to get this to work, can anybody offer any advice.
6
4448
by: Sebi | last post by:
Hello all, I'm thinking about overwriting the ListItem, so it can contain x additional values (not only one). Has anybody ever tried this? Has someone got an example (C#)? Can DropDownList handle overwritten Items, or will additional Data be lost? Thanks for any advice.
6
2671
by: vb | last post by:
Hi, I am new to .Net. I am using a Combo Box in my windows forms. I am adding the items by creating the instances and adding the same to the list. My questions/doubts are: 1. If I have 25 to 30 options in each combo box and like that if we have 6 or 7 combo boxes in a form, isnt it a problem in creating that number of instances everytime the form is opened/activated 2. How do I dispose the Combo Box items.
5
25842
by: James P. | last post by:
Hello, For my new Windows application, all I want is to create an initial form to demo to the user to show them how it looks like with some data on it. So I figure the fastest way is to create some comboBox-es to show some data on them manually entered in the code without connecting to the database yet. It should be simple but somehow I can't make it work. First, I drag a comboBox from the ToolBox to the form. Then,
4
2948
by: tino | last post by:
I have the (usual) problem that, in a combo or listbox, I want to keep a text and a related key value. For instance the country description and the country code. In order to solve this I uss the following approach: With the following class I create an item #Region "Create SubClass Item" Public Class KeyItem Inherits ListViewItem
1
4478
by: stinehelferw | last post by:
I need a right-click menu on a form list control with combobox controls. I used View->Toolbars->Customize to create a toolbar. I added ComboBox through commands tab. I set properties to Popup making it a shortcut menu. I have no problem linking it to the list control. The menu comes up when I right-click the list control.
0
1964
by: mollyf | last post by:
I've got a combo box that I want to bind a collection to. I got the error "Complex Data Binding accepts as a data source either an IList or an IListSource" so I tried using the BindingSource, which works except when I want to have a blank line as the first item in the combo box--then all that is displayed is the type of object. Here is the code for filling the collection: Public Function GetCollection(ByRef cndb As SqlConnection,...
3
1838
by: =?Utf-8?B?Y2RtdW5veg==?= | last post by:
I have created a sub routine to programmatically add an indefinite number of combo boxes to a form. The combo boxes are bound in code to a view for displaying the available options. This routine works good until you add the second combo box. At that point it changes the first combo box's selected item. A third changes the second and first and so on. I've tried setting the combo box name and tag to a unique name, but that doesn't help. ...
0
1255
by: bz | last post by:
Hi, I have a datagrid with two columns Region and City Both have combo-boxes. City column is unbound What I want is, when use select a region, to fill the combo for City column with appropriate cities for that region I did this with a code like this private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
2
5071
by: mygirl22 | last post by:
Hi, I used this code to created 2 combo boxes General and Specific...and Only show Specific (Combo B) when Combo A is chosen..... What i need now is to know how to assign specific values to the items in combo b (Specific).??? If i chose a sode, Fanta, i want the total price to be $20.00 (this price will show up in the finial price box....Please Help!! drinkComboBox.Items.Clear() 'Clears the old items out of the drinkComboBox ...
0
9906
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
9885
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
9737
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
8737
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
6562
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
5172
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
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
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
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.