Tips for using the inputbox function returned values in vbs

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.