2010-05-27 649 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