代码改变世界

[VS2005]Bug or Design?

2005-12-09 13:14  Colin Han  阅读(1269)  评论(4编辑  收藏  举报

在VS2005以前的版本,如果我们调用下面的逻辑

// Form中包含一个ComboBox Control。
public void Form_Load(object sender, EventArgs e)
{
  
this.comboBox1.DropDownStyle = DropDownStyle.DropDown; // 注意不是DropDownList.
  this.comboBox1.Items.Add("Test1");
  
this.comboBox1.Items.Add("Test2");
  
this.comboBox1.Items.Add("Test3");
  this.comboBox1.Items.Add("Test4");
  
this.comboBox1.SelectedIndex = 0;
}


public void button1_Click(object sender, EventArgs e)
{
  
this.comboBox1.Text = "Test"//该项不存在于Items中
  Debug.WriteLine(this.comboBox1.SelectedIndex.ToString());
}

返回的结果是-1.因为没有任何一项被选中。

但是,如果我们在VS2005中运行上面的代码,结果却是0

这是VS2005的Bug还是它的Design。

Bug:
    与以前的版本不兼容;
    数据会丢失;(我没有具体试验,在某些情况下,似乎设进取的Text会丢掉)

Design:
    将SelectedIndex和当前显示的Text分开。(似乎有些牵强,但好像也有些合理性)

博客园里微软的人很多,有没有能够出面澄清一下。大家也许也会有一些自己的看法。