如何为AcceptButton抛弃(处理)一个ENTER按键? [英] How can I throw out (handle) an ENTER keypress for an AcceptButton?

查看:49
本文介绍了如何为AcceptButton抛弃(处理)一个ENTER按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到如果用户通过按返回关闭表单(

,而OK按钮有焦点或者如果表单的AcceptButton设置为OK)

然后输入按键事件触发了呼叫形式!这对我来说非常糟糕

,因为在我的应用程序中,Form1通过

调用Form2来响应一个ENTER按键。如果用户通过ENTER关闭Form2,那么Form1只需再次打开它,就可以在Form2中捕获用户(他们仍然可以从X关闭
)当然)。


有谁知道防止这种情况的方法?我需要以某种方式抛出或

" handle"在Form1中输入按键,但只有它来自来自 Form2。


任何帮助都将不胜感激,

AJ

I have noticed that if a user closes a form via pressing return (either
while the OK button has focus or if AcceptButton is set to OK for the form)
then the "ENTER" keypress event fires ON THE CALLING FORM! This is very bad
for me, because in my application, Form1 responds to an ENTER keypress by
calling Form2. If the user closes Form2 via an ENTER, then Form1 just
reopens it again, kind of trapping the user in Form2 (they can still close
it from the X, of course).

Does anyone know of a way to prevent this? I need to somehow throw out or
"handle" the enter keypress in Form1, but ONLY if it "came from" Form2.

Any help would be appreciated,
AJ

推荐答案

你有form1开放形式2 - 它有一个ok按钮作为接受按钮 -

但是当用户点击输入密钥时,表单1打开form3?是否更准确地描述了它?


如果是这种情况,你试过以模态方式打开form2吗?


hth,


史蒂夫

" Adam J. Schaff" <关于***** @ cascodev.com>在消息中写道

新闻:或者************** @ TK2MSFTNGP14.phx.gbl ...

|我注意到了如果用户通过按返回关闭表单(

|,而OK按钮具有焦点,或者如果

表单将AcceptButton设置为OK)

|然后是输入按键事件触发了呼叫形式!这是非常好的b $ b坏了

|对我来说,因为在我的应用程序中,Form1通过

|响应一个ENTER按键调用Form2。如果用户通过ENTER关闭Form2,那么Form1只需

|重新打开它,有点在Form2中捕获用户(他们仍然可以从X关闭

|它当然)。

|

|有谁知道防止这种情况的方法?我需要以某种方式抛出或

| "处理"在Form1中输入按键,但只有它来自来自 Form2。

|

|任何帮助将不胜感激,

| AJ

|

|
you have form1 opening form2 - which has an "ok" button as an acceptbutton -
but when user hits enter key, form 1 opens up form3? does that more
accurately describe it?

if this is the case, have you tried opening form2 modally?

hth,

steve
"Adam J. Schaff" <as*****@cascodev.com> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...
|I have noticed that if a user closes a form via pressing return (either
| while the OK button has focus or if AcceptButton is set to OK for the
form)
| then the "ENTER" keypress event fires ON THE CALLING FORM! This is very
bad
| for me, because in my application, Form1 responds to an ENTER keypress by
| calling Form2. If the user closes Form2 via an ENTER, then Form1 just
| reopens it again, kind of trapping the user in Form2 (they can still close
| it from the X, of course).
|
| Does anyone know of a way to prevent this? I need to somehow throw out or
| "handle" the enter keypress in Form1, but ONLY if it "came from" Form2.
|
| Any help would be appreciated,
| AJ
|
|


Adam,

你能发帖(或发邮件) )这个例子,因为我无法重新创建它。


使用以下内容:


公共类Form1

继承System.Windows.Forms.Form


#Region" Windows窗体设计器生成的代码


Private Sub Form1_KeyPress(ByVal sender As Object,ByVal e As

System.Windows.Forms.KeyPressEventArgs)处理MyBase。 KeyPress

如果e.KeyChar = ControlChars.Cr那么

Dim对话框As New Form2

dialog.ShowDialog(Me)

dialog.Dispose()

结束如果

结束次级


结束班级


公共类Form2

继承System.Windows.Forms.Form


#Region" Windows窗体设计器生成的代码


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

Me.Close()

结束子


结束班


我只按Enter键时,请参阅Form2一次。以上是Windows XP SP2上的VS.NET 2003

(.NET 1.1 SP1)。

Form1是一个空白表单,我试过两个KeyPreview = True &安培; KeyPreview = False。

Form2只有Button1,Form2.AcceptButton = Button1。


希望这有帮助

Jay

" Adam J. Schaff" <关于***** @ cascodev.com>在消息中写道

news:OR ************** @ TK2MSFTNGP14.phx.gbl ...
Adam,
Can you post (or email) an example of this, as I am not able to recreate it.

Using the following:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = ControlChars.Cr Then
Dim dialog As New Form2
dialog.ShowDialog(Me)
dialog.Dispose()
End If
End Sub

End Class

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
End Sub

End Class

I only see Form2 once when I press the Enter key. The above is VS.NET 2003
(.NET 1.1 SP1) on Windows XP SP2.

Form1 is a blank form, I tried both KeyPreview=True & KeyPreview=False.
Form2 only has Button1, Form2.AcceptButton = Button1.

Hope this helps
Jay

"Adam J. Schaff" <as*****@cascodev.com> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...
我注意到如果用户通过按下返回来关闭表单(当OK按钮具有焦点时
或者如果
表单将AcceptButton设置为OK)
然后是ENTER按钮。按键事件触发了呼叫形式!这对我来说非常糟糕,因为在我的应用程序中,Form1通过调用Form2来响应一个ENTER按键。如果用户通过ENTER关闭Form2,那么Form1只是再次重新打开它,有点在Form2中捕获用户(他们仍然可以从X关闭它,当然)。
<有谁知道防止这种情况的方法?我需要以某种方式抛出或处理处理在Form1中输入按键,但只有它来自来自 Form2。

任何帮助将不胜感激,
AJ
I have noticed that if a user closes a form via pressing return (either
while the OK button has focus or if AcceptButton is set to OK for the
form)
then the "ENTER" keypress event fires ON THE CALLING FORM! This is very
bad
for me, because in my application, Form1 responds to an ENTER keypress by
calling Form2. If the user closes Form2 via an ENTER, then Form1 just
reopens it again, kind of trapping the user in Form2 (they can still close
it from the X, of course).

Does anyone know of a way to prevent this? I need to somehow throw out or
"handle" the enter keypress in Form1, but ONLY if it "came from" Form2.

Any help would be appreciated,
AJ



周杰伦,

要重现,请使用两种形式创建一个新的vb.net winforms应用程序。在每个表单上放置一个

按钮。

将此代码添加到form1:

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e作为

System.EventArgs)处理Button1.Click

Dim f作为新Form2

f.ShowDialog()

End Sub


Private Sub Button1_KeyUp(ByVal sender As Object,ByVal e As

System.Windows.Forms.KeyEventArgs)处理Button1.KeyUp

停止

结束子

将此代码添加到form2:

Private Sub Form2_Load(ByVal sender As System .Object,ByVal e As

System.EventArgs)处理MyBase.Load

Me.AcceptButton = Button1

End Sub


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

Me.Close()

结束子


运行应用程序,单击form1上的按钮。打开Form2,按Enter键。请注意,

form1中的断点被击中!我不明白为什么,因为在form2打开时输入是按

。更重要的是,我不明白如何阻止它。


p.s.框架v1.1,windows xp-sp2

" Jay B. Harlow [MVP - Outlook]" < JA ************ @ msn.com>在消息中写道

news:ud ************* @ TK2MSFTNGP10.phx.gbl ...
Jay,
To reproduce, create a new vb.net winforms app with two forms. Place a
button on each form.
Add this code to form1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim f As New Form2
f.ShowDialog()
End Sub

Private Sub Button1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles Button1.KeyUp
Stop
End Sub
Add this code to form2:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.AcceptButton = Button1
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
End Sub

Run the app, click the button on form1. Form2 opens, press Enter. Note that
the breakpoint in form1 gets hit! I don''t understand why, since enter was
pressed while form2 was open. More importantly, I don''t understand how to
prevent it.

p.s. framework v1.1, windows xp-sp2
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ud*************@TK2MSFTNGP10.phx.gbl...
Adam,
你可以发布(或发电子邮件)一个这样的例子,因为我无法重新创建
它。
使用以下内容:

公共类Form1
继承系统.Windows.Forms.Form

#Region" Windows窗体设计器生成的代码

Private Sub Form1_KeyPress(ByVal sender As Object,ByVal e As
System.Windows.Forms.KeyPressEventArgs)处理MyBase.KeyPress
如果e。 KeyChar = ControlChars.Cr然后
Dim对话框As New Form2
dialog.ShowDialog(Me)
dialog.Dispose()
结束如果
End Sub

最终课程

Public Class Form2
继承System.Windows.Forms.Form

#Region" Windows窗体设计器生成的代码

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e
As System.EventArgs)Handles Button1.Click
Me.Close()
End Sub

结束课

按Enter键时,我只看到一次Form2。以上是Windows XP SP2上的VS.NET 2003
(.NET 1.1 SP1)。

Form1是一个空白表单,我尝试了两个KeyPreview = True& KeyPreview = False。
Form2只有Button1,Form2.AcceptButton = Button1。

希望这有帮助
Jay

Adam J. Schaff <关于***** @ cascodev.com>在消息中写道
新闻:或者************** @ TK2MSFTNGP14.phx.gbl ...
Adam,
Can you post (or email) an example of this, as I am not able to recreate it.
Using the following:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = ControlChars.Cr Then
Dim dialog As New Form2
dialog.ShowDialog(Me)
dialog.Dispose()
End If
End Sub

End Class

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub

End Class

I only see Form2 once when I press the Enter key. The above is VS.NET 2003
(.NET 1.1 SP1) on Windows XP SP2.

Form1 is a blank form, I tried both KeyPreview=True & KeyPreview=False.
Form2 only has Button1, Form2.AcceptButton = Button1.

Hope this helps
Jay

"Adam J. Schaff" <as*****@cascodev.com> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...
我注意到如果用户关闭了通过按下返回形式(当OK按钮具有焦点时
或者如果
形式将AcceptButton设置为OK)
然后是ENTER按钮。按键事件触发了呼叫形式!这对我来说非常糟糕,因为在我的应用程序中,Form1通过调用Form2来响应一个ENTER按键
。如果用户通过ENTER关闭Form2,那么Form1只需再次重新打开它,就像在Form2中捕获用户一样(当然,他们仍可以
从X关闭它)。
<有谁知道防止这种情况的方法?我需要以某种方式抛出
或处理在Form1中输入按键,但只有它来自来自 Form2。

任何帮助将不胜感激,
AJ
I have noticed that if a user closes a form via pressing return (either
while the OK button has focus or if AcceptButton is set to OK for the
form)
then the "ENTER" keypress event fires ON THE CALLING FORM! This is very
bad
for me, because in my application, Form1 responds to an ENTER keypress by calling Form2. If the user closes Form2 via an ENTER, then Form1 just
reopens it again, kind of trapping the user in Form2 (they can still close it from the X, of course).

Does anyone know of a way to prevent this? I need to somehow throw out or "handle" the enter keypress in Form1, but ONLY if it "came from" Form2.

Any help would be appreciated,
AJ




这篇关于如何为AcceptButton抛弃(处理)一个ENTER按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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