【问题标题】:Should I use a Winforms combobox's SelectedItem, SelectedText, or SelectedValue?我应该使用 Winforms 组合框的 SelectedItem、SelectedText 还是 SelectedValue?
【发布时间】:2012-05-05 12:35:31
【问题描述】:

我想将组合框中的值作为参数传递给 SQL 语句。 Winforms 组合框为我提供了几个用于检索值的选项,即 SelectedItem、SelectedText 和 SelectedValue。在这种情况下使用哪一个最好/最安全?

【问题讨论】:

  • DropDownStyle 属性的值是多少?

标签: winforms combobox selecteditem selectedvalue selectedtext


【解决方案1】:
if (comboBox1.DropDownStyle == DropDownStyle.DropDown || 
    comboBox1.DropDownStyle == DropDownStyle.Simple)
{
    return comboBox1.Text;
}

Text 可能是最好用的。这将从 ComboBox 中获取当前选定的文本作为字符串。

if (comboBox1.DropDownStyle == DropDownStyle.DropDownList)
{
    return comboBox1.GetItemText(comboBox1.SelectedItem);
}

对于这种样式,您无法从ComboBox 获取文本。这将返回来自当前 SelectedIndex 的项目的字符串。

【讨论】:

    【解决方案2】:

    SelectedValue 可能是最好用的一个
    SelectedText 将为您提供可编辑部分的选定文本,Selected Item 将为您返回对象,并且 selected index 将为您返回索引。通常对于应用程序 SelectedValue 被提取和使用。 查看Combobox from MSDN

    SelectedIndex   Gets or sets the index specifying the currently selected item.                (Overrides ListControl.SelectedIndex.)
    SelectedItem    Gets or sets currently selected item in the ComboBox.
    SelectedText    Gets or sets the text that is selected in the editable portion of a ComboBox.
    SelectedValue   Gets or sets the value of the member property specified by the ValueMember property. (Inherited from ListControl.)
    

    【讨论】:

    • 我终于开始测试这个了;当我尝试 SelectedValue 时,它​​被炸毁了。但是,SelectedItem 有效: //String Center = comboBoxCenters.SelectedValue.ToString();
    【解决方案3】:

    SelectedItem 似乎是一个安全的选择。

    我有这个代码:

    NRBQConsts.currentSiteNum = listBoxSitesWithFetchedData.SelectedValue.ToString();
    

    ...与 NRE 一起崩溃。

    改成这样后:

    NRBQConsts.currentSiteNum = listBoxSitesWithFetchedData.SelectedItem.ToString();
    

    ...它工作正常。

    【讨论】:

      【解决方案4】:

      这取决于 3 件事 1. Mode 2. DropDownStyle 3. 必需值

      在 ComboBox.SelectedIndexChanged 上

      • 未绑定模式

        一个。 DropDownStyle = 下拉

        • SelectedItem 将返回 = SelectedText
        • SelectedValue 将返回 = ""
        • SelectedText 将返回 = SelectedText

          b. DropDownStyle = DropDownList

          • SelectedItem 将返回 = SelectedText
          • SelectedValue 将返回 = ""
          • SelectedText 将返回 = ""
      • 使用数据绑定模式(意味着您正在从某些数据源(即 SQL Server 表)填充您的 ComboBox) 您将选择表的一列作为 DisplayMember,并选择相同或另一列作为 ValueMember。

        一个。 DropDownStyle = 下拉

        • SelectedItem 将返回 = System.Data.DataRowView(提示)
        • SelectedValue 将返回 = ValuMemeber 的值
        • SelectedText 将返回 = SelectedText(DisplayMember 的值)

          b. DropDownStyle = DropDownList

          • .SelectedItem 将返回 = System.Data.DataRowView(提示)
          • .SelectedValue 将返回 = ValueMember 的值
          • .SelectedText 将返回 = ""

      注意:你也可以使用 .Text 来返回 = Text of ComboBox

      结论:

      1. 非绑定模式

        • .SelectedItem 是最好的选择
      2. 数据绑定模式

        一个。 ValueMember 是必需的

        • .SelectedValue 是最好的选择

          b. DisplayMember 是必需的

          • .Text 是最好的选择

      【讨论】:

      • 感谢您的详细解释。这真的很有帮助。
      猜你喜欢
      • 2011-02-22
      • 2021-10-15
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2017-08-20
      相关资源
      最近更新 更多