Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i fill the combobox from the Student id's from the database...

can anyone tell me how to set the value in the textbox when it is selected from the combobox...

thanks..

Really looking farward for u help
Posted

1 solution

Sure:

C#
MyComboBox.SelectedIndexChanged += delegate(object sender, EventArgs eventArgs) {
    MyTextBox.Text = ((MyComboBox)sender).SelectedItem.ToString();
}; //MyComboBox.SelectedIndexChanged


Chances are, you're using C# v.3 or later, then you can use simpler lambda syntax:

C#
MyComboBox.SelectedIndexChanged += (sender, eventArgs) => {
    MyTextBox.Text = ((MyComboBox)sender).SelectedItem.ToString();
} //MyComboBox.SelectedIndexChanged


I this syntax you don't need type for the parameters, they are inferred from the even type (type inference feature).

You can add this code somewhere before your controls are shown; I usually do such setup in the form or User Control constructor, at the end.

Also note: you can use any type for ComboBox list items, not only strings. This is very convenient if you need to store and extract any semantic-aware data in your list. How about presentation of such type in the list? It's rely on the return of inherited object.ToString method. Simply override this method to get desired string presentation of objects in the list.

I assumed you're using System.Windows.Forms. In case of WPF, your code will be almost the same.
Please put the library you want to use in your Question's tags.

—SA
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Feb-11 18:04pm    
Attention! A stupid illiterate coward maniac went out for down-votes!
Very dangerous; this person has not idea of single language or single technology and has strong opinion on all of them.
Ha-ha-ha!
--SA
CS2011 27-Feb-11 3:01am    
has happened wid me too... let me correct this...
Sergey Alexandrovich Kryukov 1-Mar-11 2:57am    
I know... thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900