Sure:
MyComboBox.SelectedIndexChanged += delegate(object sender, EventArgs eventArgs) {
MyTextBox.Text = ((MyComboBox)sender).SelectedItem.ToString();
};
Chances are, you're using C# v.3 or later, then you can use simpler lambda syntax:
MyComboBox.SelectedIndexChanged += (sender, eventArgs) => {
MyTextBox.Text = ((MyComboBox)sender).SelectedItem.ToString();
}
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