使用转义按钮关闭userform [英] Close userform with escape button

查看:134
本文介绍了使用转义按钮关闭userform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题。


  1. 当我按 esc 按钮,然后关闭 Userform1


  2. 当我在 TextBox1 打开应该显示$ c>然后 Userform2 。另外在 Userform1 中自动清除 TextBox1


我尝试了以下代码:

  Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms。 returnInteger)
如果textbox1.value =open,那么
userform2.show
textbox1.value =
End If
End Sub


解决方案


关闭userform1与 Esc


如果您没有对userform进行任何控制,那么只需使用此代码

  Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
如果KeyAscii = 27然后卸载我
End Sub

如果你说一个TextBox和一个命令按钮,那么使用这个

  Private Sub UserForm_Initialize()
CommandButton1.Cancel = True
End Sub

Private Sub TextBox1_KeyPress(ByV al KeyAscii As MSForms.ReturnInteger)
如果KeyAscii = 27然后卸载我
End Sub

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
如果KeyAscii = 27然后卸载我
End Sub

Private Sub CommandButton1_Click()
卸载我
End Sub

如果您有任何其他控件可以关注焦点,那么您将不得不使用该控件的 KeyPress 事件,如当我向textbox1输入open时,我为 TextBox


,然后userform2显示还自动在userform1中清除textbox1。


KeyPress 将只捕获一个键。使用更改事件来比较文本框中的内容。

  Private Sub TextBox1_Change()
如果LCase(TextBox1.Value)=open,然后
TextBox1.Value =
UserForm2.Show
End If
End Sub


I have 2 questions.

  1. When I pressed esc button then close Userform1

  2. When I input open in TextBox1 then Userform2 should show. Also clear TextBox1 in Userform1 automatically.

I have tried the below code:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If textbox1.value = "open" then
        userform2.show
        textbox1.value =""
    End If
End Sub

解决方案

Close userform1 with Esc

If you don't have any controls on userform then simply use this code

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 27 Then Unload Me
End Sub

If you have say a TextBox and a Command Button then use this

Private Sub UserForm_Initialize()
    CommandButton1.Cancel = True
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 27 Then Unload Me
End Sub

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 27 Then Unload Me
End Sub

Private Sub CommandButton1_Click()
    Unload Me
End Sub

If you have any other control that can take focus then you will have to use the KeyPress event of that control like I did for TextBox

when I input "open" to textbox1 then userform2 showed also clear textbox1 in userform1 automatically.

KeyPress will capture only one key. Use the Change event to compare what is there in the textbox.

Private Sub TextBox1_Change()
    If LCase(TextBox1.Value) = "open" Then
        TextBox1.Value = ""
        UserForm2.Show
    End If
End Sub

这篇关于使用转义按钮关闭userform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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