If you click OK or press enter, the inputbox function returns the content in the text box. If you click Cancel, the function returns a zero-length string ("").
This is a reference manual description of the inputbox function return value. It is simple and clear, and it is necessary to write a special article.ArticleTo discuss? I know that you must have dismissed the title and replaced it with me. I will also be dismissive. If I didn't see a post in Baidu vbs.
Questions about inputbox
----------------------------
If you click OK or press enter, the inputbox function returns the content in the text box. If you click Cancel, the function returns a zero-length string ("").
----------------------------
Then, how can we differentiate users by "OK" without any input characters"
Or is it "canceled?
Sometimes you want to input null characters, and sometimes you want to cancel the input.
According to the reference manual, whether the user does not enter the character and presses "OK" or "cancel", the return value of the function is a zero-length string "". In this case, it seems that there is no way to distinguish whether the user presses "OK" without the input character or "cancel ".
I thought this was not feasible, but some people have provided the solution below:CopyCodeThe Code is as follows: Str = inputbox ("??? ")
If STR = "wffheu" then msgbox ("")
Else if STR = false then "you press... Cancel"
End if
Although there are syntax errors, false can be used to differentiate "OK" and "cancel ". Why? If you click Cancel, inputbox does not return a zero-length string "", but empty.
Use a simpleProgramYou can test it:Copy codeCode: Str = inputbox ("enter a string ")
Msgbox typename (STR)
If no input character is entered, the string is output when "OK" is pressed, and the empty is output when "cancel" is pressed. Therefore, it seems that we can determine whether it is "OK" or "canceled ":Copy codeCode: Str = inputbox ("enter a string ")
If STR = empty then
Msgbox "cancel"
Else
Msgbox "OK"
End if
Unfortunately, the above Code is wrong. Whether you press "OK" or "cancel", cancel will be output. Because whether it is "" = empty (empty will be implicitly converted to "" Before comparison) or empty = empty will return true, the correct method is to use isempty to judge:Copy codeCode: Str = inputbox ("enter a string ")
If isempty (STR) then
Msgbox "cancel"
Else
Msgbox "OK"
End if
Let's also explain why false can be used for determination. "" = false returns false (false is converted to the string "false" by implicit conversion before comparison), and empty = false returns true, so it can be judged.
It seems that the document is not necessarily correct. Believe in the document, it is better to have no document.
Original article: http://demon.tw/programming/vbs-inputbox.html