English 中文(简体)
在标签中显示组合框字段
原标题:Display combobox field in label
  • 时间:2012-05-24 13:25:26
  •  标签:
  • .net
  • vb.net

我有一个组合框, 它读取目录, 并在组合框中显示文件名称。 我试图做的是当组合框中选择一个值时, 我希望在标签中显示它 。

我尝试了以下

    Label1.Text = Combobox1.SelectedValue

但它似乎不起作用。

我要在组合框中显示值的编码

With Combobox1
     .DisplayMember = "Name" 
     .ValueMember = "FullName"
     .DataSource = New IO.DirectoryInfo("Path").GetFiles().Select( _ 
       Function(fi) New With {.Name = IO.Path.GetFileNameWithoutExtension( _ 
       fi.FullName), fi.FullName}).ToArray()
End With

在Combobox1_SectiveIndex changed 事件里,我有第一个编码片段。

当我在组合框中选择一个值时, “ fullName” 与文本框1 一起工作, 但我想在标签1 text 中显示“ Name ” 。

最佳回答

您可以使用 :

ComboBox1.SelectedItem.Name

ComboBox1.SelectedItem.FullName

取决于你想要做什么

SectiveTround 是一个匿名对象, 因此如果您在另一个有不同密钥/ Value 的组合框中使用此选项, 属性的名称将会不同 。

问题回答

使用 Sselected Troit. to string () . sselectedValue 是指定给某个项目的价值, 用户看不到。 如果您想用这种方式看它, 它有点像标签。 如果您不明确地设定某个项目的值, 它将是 NULL, 所以这就是为什么您在标签上看不到任何东西 。

sselectedValue 对数据绑定非常有用。 例如, 您会希望用户看到“ John Smith” 的名称, 但是您会想要设置它所要约束的数据库行的主键值。 如果您根据此项目更新数据库, 您可以通过 SselectiveValue 作为参数( John Smith 的 PK 行), 因为它已经设置了 。

请照这个照做 希望这能帮上忙

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    label1.Text=comboBox1.SelectedItem.ToString(); 

                    OR
    label1.Text=comboBox1.SelectedValue.ToString();

}

试试这个,它对我有用, 只要给您想要从这个代码中访问的文本的列名 。

label1.Text = specific.Rows[comboBox1.SelectedIndex]["Column name"].ToString();




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签