VB.NET Inputbox-如何识别何时按下取消按钮?
我有一个简单的windows应用程序,它弹出一个输入框供用户输入搜索日期 我如何识别用户是否单击了“取消”按钮,或者仅仅按下“确定”按钮而没有输入任何数据,因为两者似乎返回相同的值 我在VB6中找到了一些处理这个问题的例子,但没有一个在.NET世界中真正起作用 理想情况下,我想知道如何分别处理空的OK和Cancel,但我完全可以用一个好方法来处理Cancel。VB.NET Inputbox-如何识别何时按下取消按钮?,vb.net,Vb.net,我有一个简单的windows应用程序,它弹出一个输入框供用户输入搜索日期 我如何识别用户是否单击了“取消”按钮,或者仅仅按下“确定”按钮而没有输入任何数据,因为两者似乎返回相同的值 我在VB6中找到了一些处理这个问题的例子,但没有一个在.NET世界中真正起作用 理想情况下,我想知道如何分别处理空的OK和Cancel,但我完全可以用一个好方法来处理Cancel。input=InputBox(“Text:”) input = InputBox("Text:") If input <>
input=InputBox(“Text:”)
input = InputBox("Text:")
If input <> "" Then
' Normal
Else
' Cancelled, or empty
End If
如果输入“”,则
“正常
其他的
“取消了,还是空了
如果结束
发件人:
如果用户单击Cancel,函数将返回长度为零的字符串(“”)
以下是我所做的,它完美地实现了我所期待的目标:
Dim StatusDate As String
StatusDate = InputBox("What status date do you want to pull?", "Enter Status Date", " ")
If StatusDate = " " Then
MessageBox.Show("You must enter a Status date to continue.")
Exit Sub
ElseIf StatusDate = "" Then
Exit Sub
End If
此键用于将输入框的默认值设置为实际空间,因此用户只要按“确定”按钮,就会返回“”值,而按“取消”则返回“”
从可用性的角度来看,输入框中的默认值开始高亮显示,并在用户键入时清除,因此体验与输入框没有值时没有区别。试试这个。我已经尝试过这个解决方案,它是有效的
Dim ask = InputBox("")
If ask.Length <> 0 Then
// your code
Else
// cancel or X Button
End If
Dim ask=InputBox(“”)
如果问。那么长度为0
//你的代码
其他的
//取消或X按钮
如果结束
为什么不检查是否为空
if not inputbox("bleh") = nothing then
'Code
else
' Error
end if
这是我通常使用的方法,因为它更容易阅读。我喜欢使用类字符串的IsNullOrEmpty方法,比如
input = InputBox("Text:")
If String.IsNullOrEmpty(input) Then
' Cancelled, or empty
Else
' Normal
End If
1) 创建一个全局函数(最好是在一个模块中,这样您只需要声明一次)
2) 在load事件表单中放入此项(使变量intInputBoxCancel=cancel事件)
3) 现在,您可以使用表单中的任何位置(如果模块中StrPtr声明为全局,则可以使用项目)
虽然这个问题已经问了5年了。我只想分享我的答案。下面是我如何检测是否有人在输入框中单击了“取消”和“确定”按钮:
Public sName As String
Sub FillName()
sName = InputBox("Who is your name?")
' User is clicked cancel button
If StrPtr(sName) = False Then
MsgBox ("Please fill your name!")
Exit Sub
End If
' User is clicked OK button whether entering any data or without entering any datas
If sName = "" Then
' If sName string is empty
MsgBox ("Please fill your name!")
Else
' When sName string is filled
MsgBox ("Welcome " & sName & " and nice see you!")
End If
End Sub
伙计们记住,你可以使用try-catch-end事件
Dim Green as integer
Try
Green = InputBox("Please enter a value for green")
Catch ex as Exception
MsgBox("Green must be a valid integer!")
End Try
您可以使用
对话框result.cancel
方法以更简单的方式执行此操作
例如:
将输入设置为字符串=输入框(“输入pin”)
如果输入“”,则
“做点什么
Elseif DialogResult。然后取消
Msgbox(“您已取消”)
如果结束
我知道这是一个非常古老的话题,但正确的答案仍然不在这里
接受的答案使用空格,但用户可以删除此空格-因此此答案不可靠。
乔治的答案是可行的,但不必要的复杂
要测试用户是否按了“取消”,只需使用以下代码:
Dim Answer As String = InputBox("Question")
If String.ReferenceEquals(Answer, String.Empty) Then
'User pressed cancel
Else if Answer = "" Then
'User pressed ok with an empty string in the box
Else
'User gave an answer
根据@Theo69的回答,以下几点对我有效:
Dim answer As String = Nothing
answer = InputBox("Your answer")
If answer is Nothing Then
'User clicked the Cancel button.
End If
这会起作用,但有没有办法区分按下“取消”按钮还是空的“确定”按钮?(就像Jamie在下面发布的内容,但在某种程度上与.NET配合使用)输入框非常有限,正如Jamie所说,只需编写自己的对话框即可。这是我在概念上寻找的,但strprtr在VB中无效。NETI不熟悉VB样式指南,并且答案中没有缩进,但是我已经尝试恢复格式化(以及复制粘贴?)中丢失的内容,在什么条件下可以
userReply”“
和userReply.Length=0
?这不起作用。空输入上的“取消”按钮和“确定”都指向If语句的同一分支。没有什么与用户单击“取消”时返回的空字符串不同。这不起作用,因为InputBox在“取消”时返回空字符串,而不是Nothing
。StrPtr在VB.NET中不可用。对不起,这是什么意思?就我所使用的代码而言,它是有效的。不可能区分空值和取消操作吗?这不起作用。它将cancel按钮和空输入都检测为cancel,因为它们都是空字符串。使用Try..Catch
验证输入通常是不好的做法。验证您的输入并使用Try..Catch
对未扩展的输入进行验证通常会更好,因为它不会区分取消和空输入。两者都会引发异常。在DialogResult上,我得到运行时错误“424”:需要对象。我得到了按cancel和按OK输入nothing的取消结果。这对我来说是有效的。我不知道我到底明白为什么。是因为String.Empty
与空字符串“
”不同吗?此代码没有检查String.Empty的值,但在实例上。这对我不起作用fwiw-我给inputbox分配了一个默认值,当我删除它并点击OK时,这里的IF语句返回TrueforMe,它还检测到一个空输入为cancel。这不起作用。取消按钮和空输入都返回长度为0的字符串。对于64位,我必须使用Int64
而不是Integer
。但它不起作用,将空输入视为“取消”按钮。此方法不再适用于Microsoft.VisualBasic命名空间中较新的InputBox。要知道是否选择了“取消”或“确定”,唯一的方法是创建自己的InputBox函数。旧的InputBox是什么?这在最新的.NET框架中是否仍然可用?它是Microsoft.VisualBasic命名空间的一部分(在参考资料中)。如果需要差异,最好只创建自己的InputBox控件。这很简单。新的不起作用的是在Microsoft.VisualBasic中,不是吗?我想知道这段代码所使用的旧代码。
dim ans as string = inputbox("prompt") ' default data up to you
if StrPtr(ans) = intInputBoxCancel then
' cancel was clicked
else
' ok was clicked (blank input box will still be shown here)
endif
Public sName As String
Sub FillName()
sName = InputBox("Who is your name?")
' User is clicked cancel button
If StrPtr(sName) = False Then
MsgBox ("Please fill your name!")
Exit Sub
End If
' User is clicked OK button whether entering any data or without entering any datas
If sName = "" Then
' If sName string is empty
MsgBox ("Please fill your name!")
Else
' When sName string is filled
MsgBox ("Welcome " & sName & " and nice see you!")
End If
End Sub
Dim Green as integer
Try
Green = InputBox("Please enter a value for green")
Catch ex as Exception
MsgBox("Green must be a valid integer!")
End Try
Dim anInput as String = InputBox("Enter your pin")
If anInput <>"" then
' Do something
Elseif DialogResult.Cancel then
Msgbox("You've canceled")
End if
Dim Answer As String = InputBox("Question")
If String.ReferenceEquals(Answer, String.Empty) Then
'User pressed cancel
Else if Answer = "" Then
'User pressed ok with an empty string in the box
Else
'User gave an answer
Dim answer As String = Nothing
answer = InputBox("Your answer")
If answer is Nothing Then
'User clicked the Cancel button.
End If