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

前端 未结 12 1776
灰色年华
灰色年华 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: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
    

提交回复
热议问题