输入框取消 [英] InputBox Cancel

查看:26
本文介绍了输入框取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个输入框来输入用户名但卡在取消按钮上

I have created an Inputbox to get the username entered but stuck with the cancel button

Private Sub Form_Load()
fsUserName = UCase(InputBox("Please Enter your name.", "User Name", _
"dc"))

If fsUserName = "" Then
MsgBox "No name Entered." & Chr(13) & Chr(13) & _
"You must enter a name.", vbExclamation, "ERROR"
Form_Load
ElseIf VarType(fsUserName) = 0 Then 'If cancel clicked
cmdQuit_Click

End If

还有一种方法可以在单击窗体上的 X 按钮时执行 cmdQuit_Click 以便如果用户单击命令按钮 Quit 或 X ,则运行 Quit 脚本.在退出脚本中有消息框和清理.

Also is there a way that when the X button on the form is clicked it executes cmdQuit_Click so that if the userclicks the command button Quit or X ,the Quit script is run.In the quit script there are message boxes and cleanup.

推荐答案

可以使用StrPtr()来检测是否点击了取消;

You can use StrPtr() to detect if cancel was clicked;

Dim fsUserName As String

fsUserName = InputBox("Please Enter your name.", "User Name", "dc")

If (StrPtr(fsUserName) = 0&) Then
    MsgBox "Cancelled or X clicked"
ElseIf fsUserName = "" Then
    MsgBox "No name Entered." & vbCr & "You must enter a name.", vbExclamation, "ERROR"
Else
    fsUserName = UCase$(fsUserName)
    MsgBox fsUserName
End If

如果你想在表单卸载时做一些事情,你可以使用 Form_Unload 事件或者更好的 Form_QueryUnload 事件,它在实际卸载之前触发 &允许您取消它.
它还会告诉您为什么要卸载表单(如果单击红色 X,UnloadMode 将为 0)

If you want to do something when the form unloads you can use the Form_Unload event or better the Form_QueryUnload event which fires before the actual unload & allows you to cancel it.
It will also tell you why the form is unloading (UnloadMode will be 0 if the red X is clicked)

使用Unload Me"将引发这两个事件.

Using "Unload Me" will raise both of the events.

像这样在 Form_Load 中调用 Form_Load 最终会填满堆栈,最好使用循环来查找丢失的用户名.

Calling Form_Load in Form_Load like that will eventually fill up the stack, better to use a loop to look for a missing username.

这篇关于输入框取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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