cbxTimes.ComboBox.DataSource = PlayTimeLengths;
cbxTimes.ComboBox.DisplayMember = "Description";
cbxTimes.ComboBox.ValueMember = "Minutes";
这个
DropDownStyle
属于
ToolStripCombobox
设置为
DropDown
.
一切正常,我可以从下拉列表中选择值,我可以在控件中编写文本。
但是,我想防止用户按下某些控件,并在按下其他控件时替换Text属性。
我正在努力完成这个任务
KeyPress
private void cbxTimes_KeyPress(object sender, KeyPressEventArgs e)
{
var cbxSender = ((ToolStripComboBox)sender).ComboBox;
string S = cbxSender.Text;
//some operations on the S variable
cbxSender.Text = S;
e.Handled = true;
} // breakpoint here shows that cbxSender.Text is not changed to S!
但是,如果我进一步运行程序(我退出了调试),我会看到Text属性发生了变化——更具体地说。我看到控件中的文本。
现在,假设我第二次按任意键,又一次在同一事件中处于调试器中:
private void cbxTimes_KeyPress(object sender, KeyPressEventArgs e)
{
var cbxSender = ((ToolStripComboBox)sender).ComboBox;
string S = cbxSender.Text; // this time breakpoint is here
//some operations on the S variable
cbxSender.Text = S;
e.Handled = true;
} // breakpoint here shows that cbxSender.Text is not changed to S!
但这次我把断点放在第二行,在检查Text属性之后,我发现它仍然没有改变。尽管我在第一次触发事件时修改了它,并且修改后的文本在控件中可见。但在调试器下,我看到的是不同的值,我看到的是一开始就设置好的值。属于结构数组的值。
那么我能做些什么来克服这个问题呢?