Combobox 将DropDownList组合框的Text属性设置为无效值不会';你不提出例外吗?

Combobox 将DropDownList组合框的Text属性设置为无效值不会';你不提出例外吗?,combobox,Combobox,我正在寻找解决Winforms应用程序问题的方法,该应用程序使用ComboBox控件。具体来说,组合框(Style=DropDownList)绑定到一个数据源,当用户浏览一些其他数据时,组合框属性的“Text”属性被设置,用户可以选择一些其他值 当我将“Text”属性设置为的值不在可用项列表中时,问题就开始了。似乎什么也没发生。以下面的简单示例为例: public partial class Form1 : Form { public Form1() { Initialize

我正在寻找解决Winforms应用程序问题的方法,该应用程序使用ComboBox控件。具体来说,组合框(Style=DropDownList)绑定到一个数据源,当用户浏览一些其他数据时,组合框属性的“Text”属性被设置,用户可以选择一些其他值

当我将“Text”属性设置为的值不在可用项列表中时,问题就开始了。似乎什么也没发生。以下面的简单示例为例:

public partial class Form1 : Form
{
  public Form1()
  {
      InitializeComponent();

      myComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
      //myComboBox1.Items.AddRange(new[] { "One", "Two", "Three" });

      List<KeyValuePair<Int32, String>> values = new List<KeyValuePair<Int32, String>>();
      values.Add(new KeyValuePair<Int32, String>(1, "One"));
      values.Add(new KeyValuePair<Int32, String>(2, "Two"));
      values.Add(new KeyValuePair<Int32, String>(3, "Three"));

      myComboBox1.DataSource = values;
      myComboBox1.ValueMember = "Key";
      myComboBox1.DisplayMember = "Value";

      button1.Click += (s, e) => { myComboBox1.Text = "Four"; };
      button2.Click += (s, e) => { myComboBox1.SelectedIndex -= 1; };
   }
}

public class MyComboBox : System.Windows.Forms.ComboBox
{
   public override string Text
   {
      get { return base.Text; }
      set { MessageBox.Show(value); base.Text = value; }
   }
}
现在,虽然我理解设置“SelectedIndex=-1”对于“IsNull”情况更好,但事实仍然是myDataRow.Blah可能不是有效值。此外,应用程序是编写的(并且是活动的),因此更改越少越好

因此,我的直接想法是“让我们重写文本属性设置器并检查值是否在列表中”。事实证明,这并不像看上去那么简单。问题在于,在各种场景中,“Text”属性被设置为所有类型的事物。例如,它是在分配DataSource属性时设置的,或者在SelectedIndex设置为-1时设置的。此外,它被设置为所选项的字符串表示形式-因此,如果您碰巧有一个绑定到KeyValue对列表的ComboBox控件,那么您将“Text”属性设置为类似“[Key,Value]”的内容。如果它绑定到DataTable/DataView,则会得到DataRow的字符串表示形式,这更难检测

在这一点上,我认为可能有另一种方法来实现所需的结果(即检测文本属性设置为某个无效值,而该设置不起任何作用)


有什么想法吗?

经过思考,这是一个合理的解决方案吗

/// <summary>
/// Gets or sets the text associated with this control.
/// </summary>
public override string Text
{
   get { return base.Text; }
   set
   {
      base.Text = value;

      if ((value != null) && (base.Text != value))
         if (value == "")
            this.SelectedIndex = -1;
         else
            throw new ArgumentException(String.Format("Cannot set Text property of {0} to \"{1}\".", this.Name, value));
   }
}
//
///获取或设置与此控件关联的文本。
/// 
公共重写字符串文本
{
获取{return base.Text;}
设置
{
base.Text=值;
if((value!=null)&&(base.Text!=value))
如果(值==“”)
this.SelectedIndex=-1;
其他的
抛出新的ArgumentException(String.Format(“无法将{0}的文本属性设置为\“{1}\”,this.Name,value));
}
}
/// <summary>
/// Gets or sets the text associated with this control.
/// </summary>
public override string Text
{
   get { return base.Text; }
   set
   {
      base.Text = value;

      if ((value != null) && (base.Text != value))
         if (value == "")
            this.SelectedIndex = -1;
         else
            throw new ArgumentException(String.Format("Cannot set Text property of {0} to \"{1}\".", this.Name, value));
   }
}