验证如何在WinForms中工作? [英] How does Validation Work in WinForms?

查看:50
本文介绍了验证如何在WinForms中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用一个或更少的音节向我解释,我是如何获得一个表格的验证活动。


我有一个带有一个文本框和两个按钮的表单:确定和取消。


我将OK按钮设置为AcceptButton,取消按钮设置为

....嗯,你已经猜到了。


我点击OK然后表格关闭,而没有参加Form1_Validating事件。

我可以在OK按钮的Click事件中验证,但我不能取消那里附近的

表格?这是一个简单的形式,开箱即用。


有人可以请我直截了当吗?


TIA


Charles

解决方案

嗨Charles,


这不是你要找的地方?

Private Sub Form1_Closing(ByVal _

发件人作为对象,ByVal _
$ b $作为System.ComponentModel.CancelEventArgs)_

处理MyBase.Closing

e.Cancel = False

End Sub


或者它是验证本身,它在控制失去之前或之后开火

焦点(这取决于键盘,鼠标或焦点方法)。


不清楚,再问一次?


Cor


这听起来很像我在关闭时所遇到的问题/>
事件和验证。如果你使用

Handles条款,我认为订单有点恭喜。


我的问题是,我会点击X按钮,但是,如果我是在一个文本框中,

导致了验证,首先被解雇了。


我不知道这是否可归类为bug。但它当然是烦人的b $ b。不幸的是,我没有解决方案。我部分的唯一方式得到

它的工作是在我的文本框的验证事件上我打电话给

Application.DoEvents(),这有点工作。我会回去看看它

更多。


-CJ


" Charles Law" ; < BL *** @ nowhere.com>在消息中写道

新闻:英国************** @ TK2MSFTNGP12.phx.gbl ...

有人可以解释一下我,用一个音节或更少的单词,如何
我得到一个表格的验证事件。

我有一个带有一个文本框和两个按钮的表单:确定和取消。

我把OK按钮设置为AcceptButton,将取消按钮设置为
......好吧,你已经猜到了。

我点击单击确定,表单将关闭,而不会转到Form1_Validating
事件。我可以在OK按钮的Click事件中验证,但是我不能取消
那里的表格?这是一个简单的形式,开箱即用。

有人可以请我直截了当吗?

TIA

Charles



*" Charles Law" < BL *** @ nowhere.com> scripsit:

有人可以用一个或更少的词语向我解释我如何获得一个表格的验证活动。

我有一个带有一个文本框的表单和两个按钮:确定和取消。

我将OK按钮设置为AcceptButton,将取消按钮设置为
......好吧,你'我已经猜到了。

我单击OK然后关闭表单,而不进入Form1_Validating事件。
我可以在OK按钮的Click事件中验证,但我不能从那里取消
表格?这是一个简单的形式,开箱即用。




在设计时不要给按钮/表格分配''DialogResult'。 />
如果你调用表格''

''关闭''方法,则在按钮''''点击''事件处理程序中分配它。

-

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps.org/>


Could someone please explain to me, in words of one syllable or less, how I
get the Validating event to fire for a form.

I have a form with one text box, and two buttons: OK and Cancel.

I have the OK button set as the AcceptButton and the Cancel button as the
.... well, you''ve guessed it.

I click OK and the form closes, without going to the Form1_Validating event.
I could validate in the Click event of the OK button, but I can''t cancel the
form close from there? This is a simple form, straight out of the box.

Could someone please put me straight?

TIA

Charles

解决方案

Hi Charles,

Is this not where you looking for?
Private Sub Form1_Closing(ByVal _
sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
e.Cancel = False
End Sub

Or is it the validating itself, it fires before or after that a control lost
focus (this depends if it keyboard, mouse or the focus method).

Not clear, ask again?

Cor


This sounds an aweful lot like the problem I''ve been having with the Closing
event and validation. I think the order is kinda fubared if you use the
Handles clause.

My issue was, I would hit the X button, however, if I was in a text box that
caused validation, that was fired first.

I don''t know if this could be classified as a "bug" but it certainly is
annoying. Unfortunatly, I have no solution. The only way I "partially" got
it to work was on the validation event of my textbox I called
Application.DoEvents(), which kinda worked. I''ll go back and look at it
more.

-CJ

"Charles Law" <bl***@nowhere.com> wrote in message
news:uK**************@TK2MSFTNGP12.phx.gbl...

Could someone please explain to me, in words of one syllable or less, how I get the Validating event to fire for a form.

I have a form with one text box, and two buttons: OK and Cancel.

I have the OK button set as the AcceptButton and the Cancel button as the
... well, you''ve guessed it.

I click OK and the form closes, without going to the Form1_Validating event. I could validate in the Click event of the OK button, but I can''t cancel the form close from there? This is a simple form, straight out of the box.

Could someone please put me straight?

TIA

Charles



* "Charles Law" <bl***@nowhere.com> scripsit:

Could someone please explain to me, in words of one syllable or less, how I
get the Validating event to fire for a form.

I have a form with one text box, and two buttons: OK and Cancel.

I have the OK button set as the AcceptButton and the Cancel button as the
... well, you''ve guessed it.

I click OK and the form closes, without going to the Form1_Validating event.
I could validate in the Click event of the OK button, but I can''t cancel the
form close from there? This is a simple form, straight out of the box.



Don''t assign a ''DialogResult'' to the buttons/form at design time.
Assign it in the buttons'' ''Click'' event handlers if you call the form''s
''Close'' method.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


这篇关于验证如何在WinForms中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆