VB.NET Inputbox - How to identify when the Cancel Button is pressed?

前端 未结 12 1628
灰色年华
灰色年华 2021-01-03 22:26

I have a simple windows application that pops up an input box for users to enter in a date to do searches.

How do I identify if the user clicked on the Cancel butt

相关标签:
12条回答
  • 2021-01-03 23:21

    Why not check if for nothing?

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

    This is what i typically use, because its a little easier to read.

    0 讨论(0)
  • 2021-01-03 23:22

    Try this. I've tried the solution and it works.

    Dim ask = InputBox("")
    If ask.Length <> 0 Then
       // your code
    Else
       // cancel or X Button 
    End If
    
    0 讨论(0)
  • 2021-01-03 23:24
    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
    
    0 讨论(0)
  • 2021-01-03 23:25

    Although this question is being asked for 5 years ago. I just want to share my answer. Below is how I detect whether someone is clicked cancel and OK button in input box:

    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 讨论(0)
  • 2021-01-03 23:28

    1) create a Global function (best in a module so that you only need to declare once)

    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) in the form load event put this (to make the variable intInputBoxCancel = cancel event)

    intInputBoxCancel = StrPtr(String.Empty)    
    

    3) now, you can use anywhere in your form (or project if StrPtr is declared global in module)

    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
    
    0 讨论(0)
  • 2021-01-03 23:30

    Guys remember that you can use the try catch end event

    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 讨论(0)
提交回复
热议问题