Anuncios

Tambien hay una version en castellano de este post, click aca.

Welcome to this post, today we’ll talk about two basic functions to work with VBscript because of it doesn’t run on a browser. These two functions are used to show and input information in our codes.

Anuncios

Msgbox

This is a dialog box that show us a message on screen. Although has a default button but we can customize to our need and we can store the value given by the pressed button, its syntax is:

msgbox("message"[, buttons][, title][, help, context])
Anuncios

The unique not optional is message, the rest are optionals. The buttons are assigned through values, check the following table:

ValueConstantDescription
0 vbOkOnlyButton Ok
1 vbOkCancel Button Ok & Cancel
2 vbAbortRetyIgnore Button Abort, Rety & Ignore
3 vbYesNoCancelButton Yes, No & Cancel
4 vbYesNo Button Yes, No
5 vbRetryCancel Button Retry & Cancel
16 vbCritical Critical message icon
32 vbQuestion Question icon
48 vbExclamation Exclamation icon
64 vbInformation Information icon
0 vbDefaultButton1 First button is default
256 vbDefaultButton2 Second button is defa
512 vbDefaultButton3 Third button is default
768 vbDefaultButton4 Fourth button is default
0 vbApplicationModal Stop app until interact with msgbox
4096 vbSystemModal All apps stop until interact with msgbox
Anuncios

We can split the buttons in four ranges:

  • 0-5, establish the buttons on the box
  • 16-64, establish the icon on the box
  • 0-768, establish the desfault button
  • 0-4096, establish the working mode
Anuncios

Let’s make an example, create a file with the name hello.vbs and write this code:

hello.vbs

dim msg
msg = msgbox("Hello, World!", 305, "First example")
wscript.echo(msg)
Anuncios

First declare a variable, then define the msgbox with a message, a title and the buttons, in this case Ok & Cancel, and in the variable we’ll store the value sent by the buttons. For the last we’ll show the value in the variable, let’s try this code:

Anuncios

If we press the cancel button we’ll get this message

Anuncios

wscript.echo is a function to show messages too but it works only with text, instead with msgbox we can make a better box to show our messages. The buttons are defined with the sum of the values in the above table, let’s take the value of this example:

1 + 48 + 256 -> 305
Anuncios

The first value are the buttons, the second is the icon and the third is for establish which is the default button. With this commented we can pass to the next theme.

Anuncios

Inputbox

This function allows us to input data to our code. This store it in a variable and has two buttons to send the data or cancel it, let’s talk about its syntax:

inputbox("message[, title][, default][, xpos][, ypos][, help, context])
Anuncios

Again, message is the only mandatory and the rest are optionals, but they allow custom our box, let’s talk about the other options:

  • title, sets a title for the box
  • default, sets a default value
  • xpos, sets the x axys for the box
  • ypos, sets the y axys for the box
  • help, it’s the help file and must be with context
  • context, it’s the context number for help
Anuncios

Let’s make an example, take again the file and change its code with the next one:

dim name
name = inputbox("Enter your name","holis","Enter your name here")
msgbox("Hello, " & name)
Anuncios

First declare the variable, this variable store the data sent by the function. In this case we establish a message, a title and a default value. In the next line we show the received value, let’s see the output:

Anuncios
Note:
In the case of msgbox and inputbox, the message field no must exceed the 1024 characters.
Anuncios

In summary, we saw msgbox and inputbox, what they are, theirs syntax, and a pair of examples, I hope you’ve found it useful.

Anuncios

Donation

It’s for maintenance of the site, thanks!

$1.50