Frage

Does any one know how I could make a 16 button all in two side by side vertical row GUI. Each button will set a variable called menuoutput to a value like 1 for the first button 2 for the second ECT. I have been testing this code but I don't see if there is a way to add 16 buttons to it.

x=msgbox("Do you want to start" ,4, "Program")
If x = 6 Then
Wscript.Echo "yes"
Else
Wscript.Echo "no"
End If
War es hilfreich?

Lösung 2

VBScript don't support GUI creation natively. You can either go the HTA way (as illustrated by Ekkehard.Horner above or you can go hybrid way to do this. In my example, I used shell script to achieve it.

1- Create Test.ps1 script with following code

Add-Type -AssemblyName System.Windows.Forms

$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Testing Custom MessageBox"
$Form.Width = 350
$Form.Height = 100
$Form.ControlBox = $False
$Form.StartPosition = "CenterScreen"

$Font = New-Object System.Drawing.Font("Tahoma",10,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font

$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Custom MsgBox!"
$Label.AutoSize = $True
$Form.Controls.Add($Label)

$Button1 = new-object System.Windows.Forms.Button
$Button1.Location = new-object System.Drawing.Size(5,25)
$Button1.Size = new-object System.Drawing.Size(75,25)
$Button1.Text = "Button1"
$Button1.Add_Click({Write-Host "Button1 was pressed";$Form.Close()})
$Form.Controls.Add($Button1)
$Button2 = new-object System.Windows.Forms.Button
$Button2.Location = new-object System.Drawing.Size(85,25)
$Button2.Size = new-object System.Drawing.Size(75,25)
$Button2.Text = "Button2"
$Button2.Add_Click({Write-Host "Button2 was pressed";$Form.Close()})
$Form.Controls.Add($Button2)
$Button3 = new-object System.Windows.Forms.Button
$Button3.Location = new-object System.Drawing.Size(165,25)
$Button3.Size = new-object System.Drawing.Size(75,25)
$Button3.Text = "Button3"
$Button3.Add_Click({Write-Host "Button3 was pressed";$Form.Close()})
$Form.Controls.Add($Button3)
$Button4 = new-object System.Windows.Forms.Button
$Button4.Location = new-object System.Drawing.Size(245,25)
$Button4.Size = new-object System.Drawing.Size(75,25)
$Button4.Text = "Button4"
$Button4.Add_Click({Write-Host "Button4 was pressed";$Form.Close()})
$Form.Controls.Add($Button4)

$Form.ShowDialog() | Out-Null
Exit 0

2- Call this in your vbscript file - Test.vbs

Set objShell = CreateObject("WScript.Shell")
Set exec = objShell.Exec("powershell -executionpolicy bypass -noninteractive -file C:\Users\pankaj.jaju\Desktop\Test.ps1")
exec.StdIn.Close()
WScript.Echo exec.StdOut.ReadAll()
Set exec = Nothing
Set objShell = Nothing
WScript.Quit 0

So when the script is called you would be able to see the message box enter image description here

And when you press any of the button, you would get the corresponding message enter image description here

Let me know if this works for you.

Andere Tipps

The 'natural' way to add a GUI to VBScript is to write an .HTA. To get you started:

<html>
 <head>
  <Title>menubuttons</Title>
  <hta:application id="menubuttons" scroll = "no">
  <script type="text/vbscript">
   Function window_onload
     Dim spMenu : Set spMenu = document.getElementById("spMenu")
     Dim nB
     For nB = 1 To 5
         Dim oB : Set oB = document.createElement("input")
         oB.setAttribute "type", "button"
         oB.id    = "B" & nB
         oB.value = oB.id
         Set oB.onclick = GetRef("menu")
         spMenu.appendChild oB
     Next
   End Function
   Function menu
     MsgBox Me.id, vbOKOnly, "menu selection"
   End Function
  </script>
 </head>
 <body>
  <span id = "spMenu"></span>
  <hr />
 </body>
</html>

To learn about the DOM, start here.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top