I've got a text box bound to an object's property (in fact several text boxes) on a form. This for is an editor for an object. When i'm editing some objects and modify values in the one of the text boxes i can't exit from the text box (neither by tab nor clicking on another text box). However that's not always the case - when editing other objects (of the same type) it works fine.
我有一个文本框绑定到表单上的对象属性(实际上是几个文本框)。这是一个对象的编辑器。当我正在编辑某些对象并修改其中一个文本框中的值时,我无法从文本框中退出(既不是通过选项卡也不是单击另一个文本框)。然而情况并非总是如此 - 当编辑其他对象(相同类型)时,它可以正常工作。
Here's a code snipet:
这是一个代码snipet:
txtValue.DataBindings.Add("Text", _SourceObject, "PlannedValue", True, DataSourceUpdateMode.OnPropertyChanged, Nothing, "c")
txtEstPlacements.DataBindings.Add("Text", _SourceObject, "EstimatedPlacementCount")
txtReference.DataBindings.Add("Text", _SourceObject, "Reference")
Any suggestions?
3 个解决方案
#1
12
Sounds like a data validation issue. Check if the controls on the form have their CausesValidation properties set to true or false.
听起来像数据验证问题。检查表单上的控件是否将其CausesValidation属性设置为true或false。
Also check the AutoValidate property on the form. It is probably set to EnablePreventFocusChange (which is the default).
还要检查表单上的AutoValidate属性。它可能设置为EnablePreventFocusChange(这是默认值)。
It may also be the case that the value being supplied in the text box can not be converted to the type of the property it is bound to on the source data object. I believe the Convert class is used for this (though I may be wrong here).
也可能是这样的情况:文本框中提供的值无法转换为它在源数据对象上绑定的属性的类型。我相信Convert类用于此(虽然我可能在这里错了)。
You may want to check out this article on MSDN that covers winforms validation in some detail.
您可能希望在MSDN上查看本文,其中详细介绍了winforms验证。
#2
5
If your Form has AutoValidate==EnablePreventFocusChange, then you'll end up with the focus stuck in any field that fails validation.
如果您的表单具有AutoValidate == EnablePreventFocusChange,那么您最终会将焦点停留在未通过验证的任何字段中。
Note that validation is considered to have failed if there is an exception when writing the value into the object.
请注意,如果在将值写入对象时发生异常,则认为验证失败。
Try setting a breakpoint at the entry point of the setter of the property that's bound to the control where the cursor gets stuck. Then, single step to see if an exception is raised.
尝试在绑定到光标卡住的控件的属性的setter的入口点设置断点。然后,单步查看是否引发了异常。
If the breakpoint never fires, the exception may be occuring within the Databinding framework.
如果断点永远不会触发,则可能在Databinding框架内发生异常。
Contrary to popular believe, the databinding framework does log errors and other useful information - it uses support from the System.Diagnostics namespace to do this. I forget the details, but they're on MSDN - you should be able to view the diagnostics in the messages window of Visual Studio while your application runs. Very useful for troubleshooting issues with Databinding.
与流行认为相反,数据绑定框架会记录错误和其他有用信息 - 它使用System.Diagnostics命名空间的支持来执行此操作。我忘记了细节,但它们在MSDN上 - 您应该能够在应用程序运行时在Visual Studio的消息窗口中查看诊断信息。对于数据绑定的故障排除问题非常有用。
#3
3
In order to fix the validation failure, which is due to the inability of the databinding to set DBNull.Value
into the textbox.text, you may add the following line in the Form_Load section:
为了修复验证失败,这是由于数据绑定无法将DBNull.Value设置为textbox.text,您可以在Form_Load部分中添加以下行:
TextBox1.DataBindings["Text"].NullValue = string.Empty;
for each text box you want to allow empty value to be validated correctly.
对于每个文本框,您希望允许正确验证空值。
See more details on Microsoft Connect.
查看有关Microsoft Connect的更多详细信息。
and on:
无法转义空文本框
#1
12
Sounds like a data validation issue. Check if the controls on the form have their CausesValidation properties set to true or false.
听起来像数据验证问题。检查表单上的控件是否将其CausesValidation属性设置为true或false。
Also check the AutoValidate property on the form. It is probably set to EnablePreventFocusChange (which is the default).
还要检查表单上的AutoValidate属性。它可能设置为EnablePreventFocusChange(这是默认值)。
It may also be the case that the value being supplied in the text box can not be converted to the type of the property it is bound to on the source data object. I believe the Convert class is used for this (though I may be wrong here).
也可能是这样的情况:文本框中提供的值无法转换为它在源数据对象上绑定的属性的类型。我相信Convert类用于此(虽然我可能在这里错了)。
You may want to check out this article on MSDN that covers winforms validation in some detail.
您可能希望在MSDN上查看本文,其中详细介绍了winforms验证。
#2
5
If your Form has AutoValidate==EnablePreventFocusChange, then you'll end up with the focus stuck in any field that fails validation.
如果您的表单具有AutoValidate == EnablePreventFocusChange,那么您最终会将焦点停留在未通过验证的任何字段中。
Note that validation is considered to have failed if there is an exception when writing the value into the object.
请注意,如果在将值写入对象时发生异常,则认为验证失败。
Try setting a breakpoint at the entry point of the setter of the property that's bound to the control where the cursor gets stuck. Then, single step to see if an exception is raised.
尝试在绑定到光标卡住的控件的属性的setter的入口点设置断点。然后,单步查看是否引发了异常。
If the breakpoint never fires, the exception may be occuring within the Databinding framework.
如果断点永远不会触发,则可能在Databinding框架内发生异常。
Contrary to popular believe, the databinding framework does log errors and other useful information - it uses support from the System.Diagnostics namespace to do this. I forget the details, but they're on MSDN - you should be able to view the diagnostics in the messages window of Visual Studio while your application runs. Very useful for troubleshooting issues with Databinding.
与流行认为相反,数据绑定框架会记录错误和其他有用信息 - 它使用System.Diagnostics命名空间的支持来执行此操作。我忘记了细节,但它们在MSDN上 - 您应该能够在应用程序运行时在Visual Studio的消息窗口中查看诊断信息。对于数据绑定的故障排除问题非常有用。
#3
3
In order to fix the validation failure, which is due to the inability of the databinding to set DBNull.Value
into the textbox.text, you may add the following line in the Form_Load section:
为了修复验证失败,这是由于数据绑定无法将DBNull.Value设置为textbox.text,您可以在Form_Load部分中添加以下行:
TextBox1.DataBindings["Text"].NullValue = string.Empty;
for each text box you want to allow empty value to be validated correctly.
对于每个文本框,您希望允许正确验证空值。
See more details on Microsoft Connect.
查看有关Microsoft Connect的更多详细信息。
and on:
无法转义空文本框