2010-05-27 372 views
15

我有一个简单的Windows应用程序,弹出一个输入框供用户输入日期进行搜索。VB.NET Inputbox - 如何识别何时按下取消按钮?

如何识别用户是否点击了取消按钮,或者只是在没有输入任何数据的情况下按下确定,因为两者似乎都返回相同的值?

我发现了一些在VB 6中处理这个问题的例子,但是他们都没有在.NET世界中真正起作用。

理想情况下,我想知道如何处理空的OK和取消单独,但我会完全确定只是一个很好的方式来处理取消。

回答

18

这里是我做的,它完美地工作了什么,我一直在寻找这样做:

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 

这关键是要设置的输入框中的默认值是一个实际的空间,因此按下OK按钮的用户将返回值“”,同时按下取消返回“”

从可用性的角度来看,输入框中的默认值开始突出显示,并在用户键入时清除,以便体验e与箱子没有价值没有区别。

13
input = InputBox("Text:") 

If input <> "" Then 
    ' Normal 
Else 
    ' Cancelled, or empty 
End If 

MSDN来自:

如果用户单击取消,该函数返回一个零长度的字符串( “”)。

+0

这会工作,但有没有办法告诉取消按下按钮或空行之间的区别? (就像Jamie在下面发布的内容一样,但是可以与.NET一起工作) – 2010-05-27 21:07:00

+0

输入框非常有限,正如Jamie所说,只需编写自己的对话框即可。 – 2010-05-28 07:15:08

-2
Dim input As String 

input = InputBox("Enter something:") 

If StrPtr(input) = 0 Then 
    MsgBox "You pressed cancel!" 
Elseif input.Length = 0 Then 
    MsgBox "OK pressed but nothing entered." 
Else 
    MsgBox "OK pressed: value= " & input 
End If 
+2

这是我正在寻找的概念,但StrPtr无法在VB.NET中有效 – 2010-05-27 21:07:36

-1

试试这个。我已经尝试了解决方案,它的工作原理。

Dim ask = InputBox("") 
If ask.Length <> 0 Then 
    // your code 
Else 
    // cancel or X Button 
End If 
-1
Dim userReply As String 
userReply = Microsoft.VisualBasic.InputBox("Message") 
If userReply = "" Then 
    MsgBox("You did not enter anything. Try again") 
ElseIf userReply.Length = 0 Then 
    MsgBox("You did not enter anything") 
End If 
+0

我不熟悉VB风格指南,并且答案中没有缩进,但我试图恢复丢失的内容格式化(以及复制和粘贴?) – 2013-05-04 09:48:12

+1

'userReply <>“”和'userReply.Length = 0'的条件是什么? – 2014-04-23 19:38:06

-2

为什么不是没有检查?

if not inputbox("bleh") = nothing then 
'Code 
else 
' Error 
end if 

这是我通常使用的,因为它更容易阅读。

+0

没有什么不同于用户单击“取消”时返回的空字符串。 – 2014-04-23 19:02:40

2

我喜欢使用String类,像这样的IsNullOrEmpty方法...

input = InputBox("Text:") 

If String.IsNullOrEmpty(input) Then 
    ' Cancelled, or empty 
Else 
    ' Normal 
End If 
+1

是否无法区分取消操作中的空值? – JCarlos 2017-02-04 02:15:33

3

1)创建一个全局函数(最好在一个模块中,所以你只需要申报一次)

Imports System.Runtime.InteropServices     ' required imports 
Public intInputBoxCancel as integer     ' public variable 

Public Function StrPtr(ByVal obj As Object) As Integer 
    Dim Handle As GCHandle = GCHandle.Alloc(obj, GCHandleType.Pinned) 
    Dim intReturn As Integer = Handle.AddrOfPinnedObject.ToInt32 
    Handle.Free() 
    Return intReturn 
End Function 

2)在窗体加载事件把这个(使可变intInputBoxCancel =取消事件)

intInputBoxCancel = StrPtr(String.Empty)  

3)现在,你可以在任何地方在你的形式(或项目使用,如果StrPtr声明模块全球)

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 
1

虽然这个问题被要求为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 
+0

StrPtr在VB.NET中不可用。 – eniacAvenger 2016-06-29 15:18:30

+0

对不起你是什么意思?至于我使用的代码,它的工作原理。 – zmd94 2017-10-26 09:02:07

0

家伙记住,你可以使用尝试捕捉结束事件

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 
0

您可以在一个简单的方式做到这一点使用DialogResult.cancel方法。

如:

Dim anInput as String = InputBox("Enter your pin") 

If anInput <>"" then 

    ' Do something 
Elseif DialogResult.Cancel then 

    Msgbox("You've canceled") 
End if