从组合框中获取文本框中的值. [英] Setting value in textbox when it is taken from combobox..

查看:86
本文介绍了从组合框中获取文本框中的值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库的学生ID填充组合框...

任何人都可以告诉我如何从组合框中选择文本框中的值吗?

谢谢..

确实对您有所帮助

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

推荐答案

确定:

Sure:

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



您可能会使用C#v.3或更高版本,然后可以使用更简单的lambda语法:



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();
} //MyComboBox.SelectedIndexChanged



在这种语法中,您不需要参数的类型,它们是从偶数类型(类型推断功能)推断出来的.

您可以在显示控件之前在某处添加此代码.通常,我通常在表单或用户控件构造函数的最后进行设置.

另请注意:您可以对ComboBox列表项使用任何类型,而不仅限于字符串.如果您需要存储和提取列表中的任何语义感知数据,这将非常方便.列表中这种类型的显示怎么样?它依赖于继承的object.ToString方法的返回.只需重写此方法即可获得列表中对象的所需字符串表示形式.

我以为您正在使用System.Windows.Forms.如果使用WPF,您的代码将几乎相同.
请在您的问题标签中放入您要使用的库.

—SA



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


这篇关于从组合框中获取文本框中的值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆