6

我有一个文本框绑定到表单上的对象属性(实际上是几个文本框)。这是一个对象的编辑器。当我在其中一个文本框中编辑一些对象并修改值时,我无法退出文本框(既不能通过选项卡也不能单击另一个文本框)。然而,情况并非总是如此 - 在编辑其他对象(相同类型)时它工作正常。

这是一个代码片段:

txtValue.DataBindings.Add("Text", _SourceObject, "PlannedValue", True, DataSourceUpdateMode.OnPropertyChanged, Nothing, "c")
txtEstPlacements.DataBindings.Add("Text", _SourceObject, "EstimatedPlacementCount")
txtReference.DataBindings.Add("Text", _SourceObject, "Reference")

有什么建议么?

4

3 回答 3

16

听起来像是数据验证问题。检查窗体上的控件是否将其 CausesValidation 属性设置为 true 或 false。

还要检查表单上的 AutoValidate 属性。它可能设置为 EnablePreventFocusChange(这是默认设置)。

也可能是文本框中提供的值无法转换为它在源数据对象上绑定到的属性的类型。我相信 Convert 类用于此(尽管我在这里可能错了)。

您可能想查看MSDN 上的这篇文章,其中详细介绍了 winforms 验证。

于 2008-10-20T16:54:20.613 回答
5

如果您的表单具有 AutoValidate==EnablePreventFocusChange,那么您最终会将焦点卡在任何未通过验证的字段中。

请注意,如果将值写入对象时出现异常,则认为验证失败。

尝试在绑定到光标卡住的控件的属性的设置器的入口点设置断点。然后,单步查看是否引发异常。

If the breakpoint never fires, the exception may be occuring within the Databinding framework.

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.

于 2008-10-22T10:06:48.830 回答
5

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:

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.

and on:

Can't escape empty textbox

于 2012-03-14T13:23:36.783 回答