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

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

    I know this is a very old topic, but the correct answer is still not here.

    The accepted answer works with a space, but the user can remove this space - so this answer is not reliable. The answer of Georg works, but is needlessly complex.

    To test if the user pressed cancel, just use the following code:

    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
    

提交回复
热议问题