【问题标题】:ComboBox Selected Item or ComboBox text Which you prefer to get the Combo Box Value in WinForm C#?ComboBox Selected Item 或 ComboBox text 您更喜欢在 WinForm C# 中获取 Combo Box 值吗?
【发布时间】:2012-01-27 10:47:24
【问题描述】:

当我看到组合框 Propriety Selected item 和 text Returns Same value in WinForms.

那么两者之间有什么区别,.. 我在哪里使用 ComboBox.SelectedItem ? 以及我在哪里使用 ComboBox.Text?

【问题讨论】:

  • combobox.Text 是显示的文本,seledted item 是当前选中的项目。做 combobox.selecteditem_changed 时要小心,我不确定在处理事件之前是否更新了 combobox.text

标签: mysql winforms


【解决方案1】:

combobox.text 总是一个字符串,combobox.selectedItem 是一个对象

【讨论】:

  • 如果你想将它们全部用作字符串,你可以使用 .Text 这样你就不必将对象解析为字符串(.ToString() 方法)
【解决方案2】:

您可以将任何对象放到 ComboBox 控件中。

  • SelectedItem 将返回此对象。
  • 当 ComboBoxStyle = DropDownList(文本部分不可编辑)时,文本将返回 SelectedItem 的字符串表示形式,结果可能是这样的 - SelectedItem.ToString()。

在此处查找更多信息 - ComboBox Class

在您的情况下,如果您将字符串添加到 ComboBox.Items 中,那么使用 SelectedItem 或 Text 没有区别。

【讨论】:

  • SelectedText 不是这样的,它的combobox.Text
  • Ya combobox.Text 或 ComboBox.SelectedItem 我更喜欢什么?
  • 由您选择;同意月光笔记关于演员表的看法。注意,当文本可编辑时,文本字符串可以不同于 (string)SelectedItem;因为你可以设置 Text 属性。
猜你喜欢
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
相关资源
最近更新 更多