Winforms 文本框在跳出其他控件时重置文本
我有一个有几个控件的窗体。存在禁用“textBoxOtherRelationship”并将文本设置为string.empty的情况。但是,当我转到另一个控件并进行制表时,数据再次出现,而该控件仍处于禁用状态Winforms 文本框在跳出其他控件时重置文本,winforms,.net-3.5,c#-3.0,Winforms,.net 3.5,C# 3.0,我有一个有几个控件的窗体。存在禁用“textBoxOtherRelationship”并将文本设置为string.empty的情况。但是,当我转到另一个控件并进行制表时,数据再次出现,而该控件仍处于禁用状态 textBoxOtherRelationship.DataBindings.Add(new Binding("Text", _binder, "RelationshipNotes")); private void ComboBoxRelationShipSelectedValueCh
textBoxOtherRelationship.DataBindings.Add(new Binding("Text", _binder, "RelationshipNotes"));
private void ComboBoxRelationShipSelectedValueChanged(object sender, EventArgs e)
{
if ((Constants.Relationship)comboBoxRelationShip.SelectedItem.DataValue == Constants.Relationship.Other)
{
textBoxOtherRelationship.Enabled = true;
if (_formMode != ActionMode.ReadOnly)
{
textBoxFirstName.BackColor = Color.White;
}
}
else
{
textBoxOtherRelationship.Enabled = false;
_model.RelationshipNotes = null;
textBoxOtherRelationship.Text = string.Empty;
if (_formMode != ActionMode.ReadOnly)
{
textBoxFirstName.BackColor = Color.LightYellow;
}
}
}
嗯。。所以我在这里看到这一行:
textBoxOtherRelationship.DataBindings.Add(
new Binding("Text", _binder, "RelationshipNotes"));
这说明您已经在textBoxOtherRelationship上的Text
属性和datasource\u binder
上名为“RelationshipNotes”的属性之间设置了绑定
太好了
因此,我假设双向绑定工作正常,并且当您在textBoxOtherRelationship
中键入某个内容,并且该控件失去焦点时,基础RelationshipNotes属性也会得到更新,对吗
现在,看看您的代码,我认为当您将Text
属性设置为string.Empty
时,底层数据源不会被更新,因为在文本框失去焦点并且禁用控件之前,通常不会发生这种情况
如果您添加:
textBoxOtherRelationship.DataBindings[0].WriteValue();
将值设置为string.Empty
后,该字符串将被存储回数据源,因为数据绑定将知道有东西要更新。从编程角度来说,它不是
我看你有这句话:
textBoxOtherRelationship.Enabled = false;
_model.RelationshipNotes = null; <<<----------------------
textBoxOtherRelationship.Text = string.Empty;
textBoxOtherRelationship.Enabled=false;
_model.RelationshipNotes=null SelectedIndexChanged事件在控件失去焦点之前不会提交数据绑定,因此快速修复方法是先在事件中写入值:
private void ComboBoxRelationShipSelectedValueChanged(object sender, EventArgs e)
{
if (comboBoxRelationShip.DataBindings.Count > 0) {
comboBoxRelationShip.DataBindings[0].WriteValue();
if ((Constants.Relationship)comboBoxRelationShip.SelectedItem.DataValue ==
Constants.Relationship.Other) {
textBoxOtherRelationship.Enabled = true;
if (_formMode != ActionMode.ReadOnly) {
textBoxFirstName.BackColor = Color.White;
}
} else {
textBoxOtherRelationship.Enabled = false;
_model.RelationshipNotes = null;
textBoxOtherRelationship.Text = string.Empty;
if (_formMode != ActionMode.ReadOnly) {
textBoxFirstName.BackColor = Color.LightYellow;
}
}
}
}