Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据选定的组合框值更改textbox的值_C#_Winforms_Combobox - Fatal编程技术网

C# 根据选定的组合框值更改textbox的值

C# 根据选定的组合框值更改textbox的值,c#,winforms,combobox,C#,Winforms,Combobox,我已经尝试了下面的代码,以获得组合框的选择值文本框,但它给我以下错误 错误:对象引用未设置为对象的实例 代码 private void frmpaymentsearch_Load(object sender, EventArgs e) { txtcomvalue.Text = "PaymentVoucherCode"; dllby.DisplayMember = "Text"; dllby.ValueMember = "Value"; dllby.Items.Ad

我已经尝试了下面的代码,以获得组合框的选择值文本框,但它给我以下错误

错误:对象引用未设置为对象的实例

代码

private void frmpaymentsearch_Load(object sender, EventArgs e)
{
    txtcomvalue.Text = "PaymentVoucherCode";
    dllby.DisplayMember = "Text";
    dllby.ValueMember = "Value";
    dllby.Items.Add(new { Text = "P.Voucher Code", Value = "PaymentVoucherCode" });
    dllby.Items.Add(new { Text = "Vendor", Value = "VendorName" });
    dllby.SelectedIndex = 0;
}



private void dllby_SelectedIndexChanged(object sender, EventArgs e)
{
    txtcomvalue.Text = dllby.SelectedValue.ToString();
}

private void dllby\u SelectedIndexChanged(对象发送方,事件参数e)
{
类型myType=dllby.SelectedItem.GetType();
IList props=新列表(myType.GetProperties());
foreach(PropertyInfo props in props)
{
如果(属性名称=“值”)
textBox1.Text=prop.GetValue(dllby.SelectedItem,null).ToString();
}
}

你能解释一下为什么
dllby.SelectedValue
失败吗?感谢代码返回{Text=P.凭单代码,Value=PaymentVoucherCode}我需要获取值,我应该使用子字符串吗?@Ayman-你应该避免使用匿名类型(见Jasmin答案)。要获取
您需要使用当前代码执行类似的操作:
dllby.SelectedItem?.GetType().GetProperty(“Value”)?.GetValue(dllby.SelectedItem,null)
(顺便说一句,这需要C#6)如果对
ComboBox
不使用
数据源,则无法找到类型或命名空间“ComboboxItem”。它对
SelectedValue
返回null。作为一般解决方案,您可以依赖扩展方法。它根据
ValueMember
提取item的值,方法如下:
var value=comboBox1.GetItemValue(comboBox1.SelectedItem.ToString()
ComboBox
ListBox
具有用于获取文本的
GetItemText
,但它们缺少
GetItemValue
。链接的帖子共享了一个扩展方法来解决这个问题。
private void dllby_SelectedIndexChanged(object sender, EventArgs e)
{
        Type myType = dllby.SelectedItem.GetType();
        IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

        foreach (PropertyInfo prop in props)
        {
            if(prop.Name=="value")
                textBox1.Text = prop.GetValue(dllby.SelectedItem, null).ToString();
        }
}
 Dictionary comboSource = new Dictionary();
            comboSource.Add("1", "Sunday");
            comboSource.Add("2", "Monday");
            comboSource.Add("3", "Tuesday");
            comboSource.Add("4", "Wednesday");
            comboSource.Add("5", "Thursday");
            comboSource.Add("6", "Friday");
            comboSource.Add("7", "Saturday");
            comboBox1.DataSource = new BindingSource(comboSource, null);
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember = "Key";
private void frmpaymentsearch_Load(object sender, EventArgs e)
    {
        txtcomvalue.Text = "PaymentVoucherCode";
        dllby.DisplayMember = "Text";
        dllby.ValueMember = "Value";
        dllby.Items.Add(new { Text = "P.Voucher Code", Value = "PaymentVoucherCode" });
        dllby.Items.Add(new { Text = "Vendor", Value = "VendorName" });
        dllby.SelectedIndexChanged -= dllby_SelectedIndexChanged; // unsubscribe you event 
        dllby.SelectedIndex = 0;
        dllby.SelectedIndexChanged += dllby_SelectedIndexChanged; // subscribe you event 
    }

    private void dllby_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtcomvalue.Text = dllby.Text.ToString(); // change selectedvalue to Text
    }
private void frmpaymentsearch_Load(object sender, EventArgs e)
{
    txtcomvalue.Text = "PaymentVoucherCode";
    dllby.DisplayMember = "Text";
    dllby.ValueMember = "Value";
    dllby.Items.Add(new ComboboxItem(){ Text = "P.Voucher Code", Value = "PaymentVoucherCode" });
    dllby.Items.Add(new ComboboxItem(){ Text = "Vendor", Value = "VendorName" });

    dllby.SelectedIndex = 0;
}
private void dllby_SelectedIndexChanged(object sender, EventArgs e)
{
    txtcomvalue.Text = (dllby.SelectedItem as ComboboxItem).Value.ToString();
}

public class ComboboxItem
{
    public string Text { get; set; }
    public string Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}