bof wrote:
> I have a script that performs 100 actions at regular interval and I'd
> like to give a progress indication.
> hi Bof,
Yes, yes, I've said this before, but here goes again.
vbs is sadly lacking in a gui interface, and so most people
here will recommend using IE as a gui. But there are other
possibilities if you don't mind using 3rd party software.
There is kixForms, written by: Shawn Tassie of CGI Canada:
http://www.kixforms.org/assets/index.htm,
and wshDialog, written by: Peter J.C. van der Klugt:
http://home.hccnet.nl/p.vd.klugt/,
Note that kixForms was written for the kix language,
but works perfectly well with vbs.
I have attached two vbs scripts, both showing progressbar
dialogs. The kixForms script was written by me, and the
wshDlg example comes from Peter v.d.K's help file.
cheers, jw
____________________________________________________________
You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)
p.s. note that the file extensions were changed to txt,
to avoid nasty accusations by some AV software as being
"malicious" scripts.
' wshKixtartFormsDemo Script, jw 17Dec07
'
' --- description block --------------------------
'
' Title: Kixtart Forms Demo Script...
'
' Description: The Kixtart Forms (KixForms) actX object was originally
' intended for use by Kixtart scripters to provide a GUI interface.
' But, since it is an actX object you may also use it from vbScript...
'
' In this example, a status message / progress bar dialog is
' shown -- something that vbScript ought to have had of its own.
'
' Author: mr_unreliable
' Website: none at present (but may be found lurking around the vbs ng)...
'
' Usage: Use at you own risk, tested on win98se...
'
' --- revision history ---------------------------
' 17Dec07: initial attempt...
' 19Dec07: (finally) figured out how the KixForms events work(?)...
' --- end of description block -------------------
'
' --- these objects are global in scope ----------
' The following line imports the System (root) namespace:
Dim oKixSystem : Set oKixSystem = CreateObject("Kixtart.System")
' Set Up the Top-Level Container (called "the Form Object")
Dim oForm : Set oForm = oKixSystem.Form()
Dim oBtn, oLbl, oProgBar ' as object(s)
' --- end of global variables --------------------
' ================================================
' === MAIN LINE SCRIPT LOGIC HERE ================
' ================================================
Call Create_Dialog() ' using kixforms
' run through the demo, showing status messages and advancing progbar...
Dim iPct : iPct = 0
Do
WScript.Sleep 100
if ((iPct Mod 10) = 0) then oLbl.Text = "Script Status: " & CStr(iPct) & " pct complete"
if (iPct < 100) then iPct = iPct + 1
oProgBar.Value = iPct ' advance the progbar
' --- discussion of event processing ----------
' o.k., can't say how this works exactly. The Documentation says:
' "DoEvents returns a string representing the next event in the queue".
' Apparently that string referred to is the string stored in the event
' property. For example: say a click event is detected, then the
' DoEvents method will return the string stored in the onClick property.
' Then that string (retrieved by DoEvents) gets executed here...
'
' As to the DoEvents "wait" parameter, according to the documentation:
' false means process an event if one is detected, but otherwise DON'T WAIT for an event.
' true means wait (i.e., don't return until there is some event to report).
' But here, it seems to work JUST THE OPPOSITE from what the doc says.
' Ain't this fun...
' --- end of discussion ----------------------
Execute(oForm.DoEvents(True)) '
Loop Until (iPct >= 100)
' o.k., the demo is finished, now exit gracefully...
oLbl.Text = "the demo is finished"
WScript.Sleep 1000
oLbl.Text = "this dlg will close in 2ses"
WScript.Sleep 2000
oForm.Visible = False
Set oForm = nothing
Set oKixSystem = nothing
' provide reassurance that this script closed
MsgBox("Script Terminating Now... ")
WScript.Quit
' ================================================
' === SUBROUTINES FOLLOW =========================
' ================================================
Sub oForm_Click()
MsgBox("form click detected(!)")
End Sub
Sub oBtn_Click()
MsgBox("button click detected(!)")
End Sub
Sub Create_Dialog()
With oForm
.Text = "vbs (KixForms) StatusMsg/Progbar dlg... " ' the form caption
.MinimizeBox = "False"
.MaximizeBox = "False"
.Resizable = 0
.BackColor = "Azure" ' &HD0D0D0 ' Lt Gray
.Width = 300
.Height = 130
.FontName = "Verdana"
.FontBold = True
.FontSize = 10
.onClick = "oForm_Click()"
' add a button to the form...
Set oBtn = .Controls.Add("Button")
With oBtn
.Left = 65 : .Top = 70 : .Width = 180 : .Height = 20
.BackColor = &HFF ' this doesn't seem to work
.FontSize = 9
.FontBold = False
.Text = "Cancel the Script!"
.onClick = "oBtn_Click()" ' "MsgBox(""hi"")" ' (this works too)
End With ' obtn
Set oLbl = .Controls.Add("Label")
With oLbl
.Left = 20 : .Top = 10 : .Width = 270 : .Height = 30
.Text = "Status Message Here... "
End With ' olbl
Set oProgBar = .Controls.Add("ProgressBar")
With oProgBar
.Left = 20 : .Top = 40 : .Width = 260 : .Height = 20
.Minimum = 0 : .Maximum = 100 : .Style = 1 ' smooth
.Value = 50
End With ' oprogbar
.Show ' show the form
End With ' oform
End SubOption Explicit
Const vbModal = 1
Dim oDlg, oFrm, oCtl
'Create the WshDialog.Kit object and store a reference in oDlg
Set oDlg = Wscript.CreateObject("WshDialog.Kit", "oDlg_")
'Add a new form and store a reference to it in the variable oFrm
Set oFrm = oDlg.NewForm("Sample")
'Add a progressbar control and store a reference in the variable oCtl
Set oCtl = oFrm.NewProgressBar("BAR1", 150, 100, 2450, 250, 0, 100, 0)
'Add a timer with an interval of 1 second
oFrm.NewTimer "TIMER", 1
'Automatically size the form and enable event handling (callback)
oFrm.Autosize
oFrm.CallBack = True
'Show the form (modally)
oFrm.Show vbModal
MsgBox "Done"
'--------------------------------------------------------------------------------------------------
' oDlg_ClickHandler handles the events sent by the controls
'--------------------------------------------------------------------------------------------------
Sub oDlg_ClickHandler(sForm, sControl)
Dim oFrm, oCtl, oBar
'Get a reference to the form and the control that raised the event
Set oFrm = oDlg.Frm(sForm)
Set oCtl = oFrm.Ctl(sControl)
'Check which control caused the event
Select Case sControl
Case "TIMER"
'Move the progressbar by 5 percent
Set oBar = oFrm.Ctl("BAR1")
If oBar.Value < oBar.Max Then
oBar.Value = oBar.Value + Cint((oBar.Max - oBar.Min) / 20)
Else
'Maximum reached. Disable the timer and dismiss the form
oCtl.Enabled = False
oFrm.Hide
End If
Case Else
'Ignore all other events. Do NOT use oFrm.Hide here,
'or any event not handled above will dismiss the form
End Select
End Sub