Code for selecting a form using the vbscript script

Source: Internet
Author: User

Q:
Hi, Scripting Guy! I want to create a form with four radio buttons, each of which represents a computer. You can select one single-choice button and click another button, and the script will run on the selected computer. How can I do this?

-- CW

A:
Hi, CW. If we only talk about VBScript and Windows Script Host, the problem is simple: No. Apart from message boxes, VBScript and WSH cannot create graphical user interfaces. You cannot use single-choice buttons, list boxes, drop-down lists, and other graphic elements through scripts.

But -- oh, you must have seen this before. Yes: Let's tell you what you can't do first, and then tell you how to do it. (Hi, you need more skills, don't you ?) Yes: although you do not use VBScript to implement this, you can use HTA (HTML application.

We will not spend much time talking about HTA today. If you are interested in this, you can find the network broadcast we launched for this topic about a year ago. It can be said that HTA allows us to combine Internet Explorer and script code, and in turn provides a graphical user interface for the script. Although there are still some ways to merge graphical user interfaces into scripts, this is probably the easiest way for users who are getting involved in GUI development.

Let's provide the HTA code first, and then explain how it works. In this example, HTA displays four single-choice buttons, each of which represents a computer. Select a computer and click a "Run Script" button. After the task is completed, a subroutine is Run. The program will connect to the selected computer and then report the name of the operating system installed on the computer. Pretty good, right? To learn how it works, copy the code, paste it into notepad, and save the file with the. hta file extension (for example, OS _name.hta. Do not use the. vbs file extension. This does not work. The extension must be. hta.

Copy codeThe Code is as follows: <script language = "VBScript">

Sub RunScript

If ComputerOption (0). Checked Then
StrComputer = ComputerOption (0). Value
End If
If ComputerOption (1). Checked Then
StrComputer = ComputerOption (1). Value
End If
If ComputerOption (2). Checked Then
StrComputer = ComputerOption (2). Value
End If
If ComputerOption (3). Checked Then
StrComputer = ComputerOption (3). Value
End If

If strComputer = "" Then
Exit Sub
End If

Set ob1_miservice = GetObject _
("Winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colItems = obw.miservice. ExecQuery _
("Select * From Win32_OperatingSystem ")
For Each objItem in ColItems
Msgbox objItem. Caption
Next

End Sub

Sub CancelScript
Self. Close ()
End Sub

</SCRIPT>

<BODY>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-01"> atl-ws-01 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-02"> atl-ws-02 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-03"> atl-ws-03 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-04"> atl-ws-04 <P>

<Input id = runbutton class = "button" type = "button" value = "Run Script" name = "OK _button"
OnClick = "RunScript">

<Input id = runbutton class = "button" type = "button" value = "Cancel" name = "cancel_button"
OnClick = "CancelScript">

</BODY>

So what content does the Code contain? We can divide the code into four parts: two parts Use HTML tags to implement Single-choice buttons and the "Run Script" and "Cancel (Cancel)" buttons, the other two parts Run the subroutine based on whether you click "Run Script" or "Cancel. Let's take a closer look at these parts.

For example, the HTML code displays the four radio buttons here. (If you know HTML, There is nothing special here; this is a standard HTML code .) Note that all buttons have the same name (ComputerOption); this is to ensure that only one button can be selected at a time. Note that the "value" of each button is set to the name of the corresponding computer:

<BODY>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-01"> atl-ws-01 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-02"> atl-ws-02 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-03"> atl-ws-03 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-04"> atl-ws-04 <P>

This is the code that displays the "Run Script" and "Cancel" buttons. The key here is the "onClick" parameter (which indicates which subroutine to run when you click the button ). As you can see by clicking the first button, the RunScript subroutine runs. Click the second button to run the CancelScript Subroutine:

<Input id = runbutton class = "button" type = "button" value = "Run Script" name = "OK _button"
OnClick = "RunScript">

<Input id = runbutton class = "button" type = "button" value = "Cancel" name = "cancel_button"
OnClick = "CancelScript">

</BODY>

By the way, the CancelScript subroutine will disable HTA. As you can see, the entire process is not complicated at all:

Sub CancelScript
Self. Close ()
End Sub

Now -- Final! -- We have to deal with good things. Select a single-choice button and click "Run Script ". How does our HTA know the selected button and the computer on which to run the script? So where should I run the script? Relax; everything is in the RunScript routine:

Copy codeThe Code is as follows: Sub RunScript

If ComputerOption (0). Checked Then
StrComputer = ComputerOption (0). Value
End If
If ComputerOption (1). Checked Then
StrComputer = ComputerOption (1). Value
End If
If ComputerOption (2). Checked Then
StrComputer = ComputerOption (2). Value
End If
If ComputerOption (3). Checked Then
StrComputer = ComputerOption (3). Value
End If

If strComputer = "" Then
Exit Sub
End If

Set ob1_miservice = GetObject _
("Winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colItems = obw.miservice. ExecQuery _
("Select * From Win32_OperatingSystem ")
For Each objItem in ColItems
Msgbox objItem. Caption
Next

End Sub

The first half of this subroutine is used to determine the selected button. A single button (at least a button with the same name) is stored as an array. The first button in the array is project 0, and the second button is Project 1. What we do here is to determine which button is selected. This can be achieved by viewing the "Checked (selected)" attribute of each button. For example, this line of code is used to determine whether the "Checked" attribute of Button 0 (the first button in the array) is true. If yes, it indicates that the selected button is:

If ComputerOption (0). Checked Then
What if "Checked" is true? Then, assign the value of the single-choice button to the variable strComputer (remember that the value of this button is exactly the name of the computer ):

StrComputer = ComputerOption (0). Value
What if "Checked" is False? No big deal; after all, we have checked the values of each single-choice button. Sooner or later, you will find out which button is selected (and only one button can be selected ). If no button is selected, the child routine will exit. This is the work completed by the Code:

Copy codeThe Code is as follows: If strComputer = "" Then
Exit Sub
End If

If a button is selected, strComputer is the name of the computer to be connected. This is the work completed in the second half of the subroutine: connecting to the specified computer and returning the name of the operating system installed on it is a standard WMI script.

Yo! We bet you will be very happy to be able to accomplish this, won't you? Or, at least if we do, you will be happy. However, there is another point to note. The example HTA provided here can retrieve the name of the operating system installed on the computer and display it in the message box. This is good, but what should you do if you want to display a list of all services installed on your computer? At this time, you will find that you will respond to dozens of message boxes. This is not your desired user experience.

Can this problem be solved? Of course. We don't want to spend more time on this, but we need to do a few things. First, we add a SPAN area to HTA. This is only a recognizable area on the screen, where information can be written. Use this code to place SPAN (with the DataArea ID) below the button:

<P> <span id = DataArea> </span>
Second, you need to collect the data and save it all to a variable, instead of displaying all the data in a message box. This Code sets the value of the variable strText to any value in strText plus the value of the "caption" attribute, add the <BR> tag (equivalent to the "ENTER" key on the keyboard in the HTML Script ):

StrText = strText & objItem. Caption & "<BR>"
Finally, set the "InnerHTML" attribute of SPAN to the value of the variable strText:

DataArea. InnerHTML = strText
Do you understand? In addition, to avoid too much explanation, there is a modified HTA, which can collect the names of all services installed on the computer, and then automatically write these names into HTA:

Copy codeThe Code is as follows: <script language = "VBScript">

Sub RunScript

If ComputerOption (0). Checked Then
StrComputer = ComputerOption (0). Value
End If
If ComputerOption (1). Checked Then
StrComputer = ComputerOption (1). Value
End If
If ComputerOption (2). Checked Then
StrComputer = ComputerOption (2). Value
End If
If ComputerOption (3). Checked Then
StrComputer = ComputerOption (3). Value
End If

If strComputer = "" Then
Exit Sub
End If

Set ob1_miservice = GetObject _
("Winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colItems = obw.miservice. ExecQuery _
("Select * From Win32_Service ")
For Each objItem in ColItems
StrText = strText & objItem. Name & "<BR>"
Next

DataArea. InnerHTML = strText

End Sub

Sub CancelScript
Self. Close ()
End Sub

</SCRIPT>

<BODY>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-01"> atl-ws-01 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-02"> atl-ws-02 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-03"> atl-ws-03 <BR>
<Input type = "radio" name = "ComputerOption" value = "atl-ws-04"> atl-ws-04 <P>

<Input id = runbutton class = "button" type = "button" value = "Run Script" name = "OK _button"
OnClick = "RunScript">

<Input id = runbutton class = "button" type = "button" value = "Cancel" name = "cancel_button"
OnClick = "CancelScript">
<P>
<Span id = DataArea> </span>
</BODY>

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.