在 Winforms 中,为什么在离开 TextBox 并进入 DataGridView 后不触发验证?
我正在重写自定义 Winforms 文本框中的 OnValidating 事件。我发现如果文本框(绑定到对象)具有焦点,然后我使用鼠标给出网格焦点,则 OnValidating 事件并不总是被触发。当我第一次给予网格焦点时,它会被很好地触发。但是,如果将网格的一个单元格置于编辑状态(光标闪烁),那么从那里开始,当我使用鼠标在文本框和网格之间返回时,它似乎不会被触发。如果我使用 Tab 键更改焦点,验证总是会被触发。如果我使用鼠标将焦点集中到非网格控件,则验证总是会被触发。
我尝试以简单的形式从头开始重新创建此功能,但无法重新创建问题。我在遇到问题的设置中使用的网格是具有自定义列类型的自定义 DataGridView。我想知道是否是网格的问题。但是,我不知道它如何影响文本框事件。有什么想法吗?
I'm overriding the OnValidating event in a custom Winforms text box. I'm finding that if the text box (which is bound to an object) has focus and then I give a grid focus using the mouse, the OnValidating event doesn't always get fired. When I first give the grid focus, it gets fired fine. But, if put one of the grid's cell in edit (blinking cursor), from there on out it seems to not get fired when I go back between the text box and grid using the mouse. If I change focus using the tab key, the validating always gets fired. If I give focus to a non-grid control using the mouse, the validation is always getting fired.
I tried to recreate this functionality from scratch in a simple form and I can't recreate the problem. The grid I'm using in the setup where I'm getting the problem is a custom DataGridView with custom column types. I'm wondering if the grid is the problem. But, I don't see how it could affect the text box events. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它可能与
CausesValidation 有关
属性。如果焦点转到
CausesValidation
设置为 false 的控件,则控件的验证将被抑制。这只是一个疯狂的猜测,但我认为网格内的某些控件具有CausesValidation = false;
此属性适用于“取消”按钮之类的内容,但可能会引起很多混乱。
It probably has to do with the
CausesValidation
property.A control's validation is suppressed if focus is going to a control that has
CausesValidation
set to false. It's just a wild guess, but I'm thinking some control inside the grid hasCausesValidation = false;
This property is meant for things like "Cancel" buttons, but can cause lots of confusion.