Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how create a listbox in vbscript

Status
Not open for further replies.

celia1

MIS
Nov 25, 2002
20
FR
I want to create a list box
 
hta sounds like the general answer.
WSH/Vbscript native doesnt have a gui as such, best you will get is InputBox
 
If you don't mind using a (freeware) third-party OCX then Jim Warrington's amazing 'wshLtWtNonModalDialog.ocx' control will do it. Sadly, Jim's website is no longer available but you can still download his 'wshLtWtNonModalDialog.ocx' from
Unzip then register the OCX using:-

Code:
RegSvr32 /s wshLtWtNonModalDialog.ocx

Alternatively, for a real easy method of registering/unregistering components, just copy and paste the following into Notepad and save as 'ocxdllreg.reg'.

Code:
REGEDIT4

;  This adds the ability to Right-Click on a .dll or .ocx
;  and get the Register / UnRegister options.
;
;  If it doesn't work for you, mail me and tell me about it.
;  Jon Evans <jon@evansoft.demon.co.uk>

; ==========
; .DLL files
; ==========

[HKEY_CLASSES_ROOT\.dll]
"Content Type"="application/x-msdownload"
@="dllfile"

[HKEY_CLASSES_ROOT\dllfile]
@="Application Extension"

[HKEY_CLASSES_ROOT\dllfile\Shell\Register\command]
@="regsvr32.exe \"%1\""

[HKEY_CLASSES_ROOT\dllfile\Shell\UnRegister\command]
@="regsvr32.exe /u \"%1\""

; ==========
; .OCX files
; ==========

[HKEY_CLASSES_ROOT\.ocx]
@="ocxfile"

[HKEY_CLASSES_ROOT\ocxfile]
@="OCX"

[HKEY_CLASSES_ROOT\ocxfile\Shell\Register\command]
@="regsvr32.exe \"%1\""

[HKEY_CLASSES_ROOT\ocxfile\Shell\UnRegister\command]
@="regsvr32.exe /u \"%1\""

; End

Once you've saved it as a REG file, double-click on it (or right-click and select 'merge') to import the info into the registry. As the comments in the REG file show, all you have to do to register/un-register DLL's and OCX's is to right-click on them and choose whatever option you want.

Unfortunately, one of the best example scripts to demonstrate Jim's control isn't on the site so copy/paste the following into Notepad and save as 'wshNonModalDlgDemo_wCmdBtns.vbs'. I know it's a bit long but it's a great demo about how to show how to create a listbox and other effects.

Code:
' demo script, to show how to use wshLtWtProgressDialog object  (jw 07/15/99)
' author: jwarrington@worldnet.att.net

' --- revision history ---------------------------
' 17Jul00: revised to demo buttons (new feature)
' 18Dec00: revised to work with wshLtWtNonModalDialog.ocx,
'    (and removed all the type conversions, let the variants roll)...
' --- end of revisions ---------------------------
  
Option Explicit

Dim oNMD : Call Instantiate(oNMD, "wshLtWtNonModalDialog.ucNMD", "oNMD_")  ' as Object

' --- module level variables ---------------------
Dim m_btnStartID  ' as integer
Dim m_btnCancelID  ' as integer
'
Dim bCloseFlag  ' t/f if user closed the form...
Dim bStartClick, bCancelClick  ' as boolean
Dim iSetPoint, pctDone
Dim m_MSTitles(10), m_MSRemark(10)
Dim sIndent
'
' --- constant declarations ----------------------
Const sDlgCaption = "   billy g's portfolio - [wshLtWtNonModalDialog demo]"
' --- end of declarations and constants ----------

' ================================================
' === MAIN LINE SCRIPT LOGIC =====================
' ================================================

  bCloseFlag = FALSE  ' set close flag as undetected.  
  Call SetUp_MSConstants()  ' set up messages for demo...

  ' Create the Form, and add the controls...
  Call Create_Form(sDlgCaption)

  ' present the introduction, and, tell the user 
  '   we ain't gonna start until he/she clicks the start btn...
  oNMD.AddLine "Welcome to the wsh Lite Weight Progress Dialog Demo,"
  oNMD.AddLine "   brought to you by:  jawar productions. (clap, clap, clap) "
  oNMD.AddLine " "  ' space
  oNMD.AddLine "Click the START button to begin... "
  oNMD.AddLine " "  ' space

  ' Now, wait around for the user to start the process...
  bStartClick = False

  Do While bStartClick = False  ' wait for start button click...
    WScript.Sleep 100  ' wait-a-bit (and hopefully, allow for "DoEvents")
    if bCloseFlag then  ' exit here...
      oNMD.AddLine "  => User Clicked Close [X] Button, " 
      WScript.Sleep 1500  ' wait-a-bit
      oNMD.ShowDialog False  ' clean up oNMD...
      oNMD.UnloadDialog  
      set oNMD = Nothing
      WScript.Quit  ' end of script
    End If
  Loop  ' wait loop

  ' starting now...
  ' change dual-purpose button caption...
  oNMD.frmDialog.Button(1).Caption = "Cancel"  
  bCancelClick = False
  oNMD.AddLine "Process Initiated"
  oNMD.AddLine sIndent & " updating billy g's portfolio"
  WScript.Sleep 400  ' allow that to "sink in" for a moment

  ' set point for the first milestone
  iSetPoint = 10
  For pctDone = 1 to 100
    WScript.Sleep 100  ' adjust for loop timing (and "DoEvents")...

    ' check for user cancel or close...
    if bCancelClick  then  Exit For
    if bCloseFlag    then  Exit For 

    if  pctDone = iSetPoint  then  ' periodically add messages to LstBox
        oNMD.AddLine m_MSTitles(Int(iSetPoint/10))
        oNMD.AddLine sIndent & m_MSRemark(Int(iSetPoint/10))
        iSetPoint = iSetPoint + 10
        End If

    oNMD.PctComplete pctDone  ' advance progbar

  Next  ' end of "for" loop...

  ' test if normal finish (neigher button clicked)...
  if (bCancelClick = False) and (bCloseFlag = False)  then
    ' is a normal finish, figure out what to do...
    oNMD.AddLine ""  ' space
    oNMD.AddLine " ..This script is finished. Review Results, click -EXIT- to close. "
    oNMD.frmDialog.Button(1).Caption = "Exit" 
    
    bCancelClick = False
    Do  'wait to close...
      WScript.Sleep 200
    Loop Until bCancelClick
  End If 

  ' add advisory exit messages to the listbox...
  oNMD.AddLine " "  ' space
  if bCancelClick then  oNMD.AddLine "  => User Clicked Cancel, "  
  if bCloseFlag   then  oNMD.AddLine "  => User Clicked Close [X] Button, "  
  oNMD.AddLine "           (This window will CLOSE in 2 secs) "
  WScript.Sleep 1500  ' 1.5 sec (for slow readers, slow this down)...

oNMD.ShowDialog False  ' clean up oNMD...
oNMD.UnloadDialog  
set oNMD = Nothing
WScript.Quit  ' end of script



' ================================================
' === SUBROUTINES FOLLOW =========================
' ================================================


' --- click event handlers ---
Sub oNMD_ButtonClick(btnID)
  ' MsgBox("Button Clicked, ID = " & CStr(btnID))
  if btnID = m_btnStartID  then  bStartClick = True
  if btnID = m_btnCancelID  then  bCancelClick = True
End Sub

Sub oNMD_UserClose()
  ' MsgBox(" .. user close detected")
  bCloseFlag = TRUE
End Sub


' --- this code creates the form and adds the controls ---
Sub Create_Form(sCaption)
Dim wdForm, htForm, wdBtn, htBtn, wdBtnSp  ' as long
Const sLBLabel = "Milestone Report"
Const sLogo = "brought to you by:  jawar productions. (clap, clap, clap)... " 

  ' do some geometry calculations...
  wdForm = 460 : htForm = 320 : wdBtn = 100 : htBtn = 25
  wdBtnSp = Int((wdForm - wdBtn) / 2) - 3

  oNMD.CreateDialog sCaption, 80,100, wdForm,htForm
  oNMD.MinMaxBtns = False  ' hide the min/max buttons
        
  oNMD.AddLabel sLBLabel, 20,10, 300,20
  oNMD.AddLstBox 20,30, 400,170
    
  oNMD.AddProgressBar 40,205, 360,20

  ' "dual purpose" button, start now, cancel later...
  oNMD.AddButton "Start", wdBtnSp,240, wdBtn,htBtn
  m_btnStartID = 101  ' the first button created has an ID of 101
  m_btnCancelID = 101  ' same button...
  
  oNMD.AddLabel sLogo, 100,275, 340,15
  ' --- finished with creating the form ---

  With oNMD.frmDialog  ' use the form object to get at control props...
    .Label(2).ForeColor = &H3C14DC  ' Crimson (was vbRed)
    .Label(2).BackColor = &HC4E4FF  ' Bisque (was vbYellow)
    .Label(2).Font.Italic = True
  End With

  oNMD.ShowDialog True

End Sub 


Sub SetUp_MSConstants()  ' generate table of milestone messages...

  sIndent = String(10, " ")

  m_MSTitles(1) = "Launching Script... " 
  m_MSTitles(2) = "Milestone 1 Completed " 
  m_MSTitles(3) = "Milestone 2 Completed " 
  m_MSTitles(4) = "Milestone 3 Completed " 
  m_MSTitles(5) = "Milestone 4 Completed " 
  m_MSTitles(6) = "Milestone 5 Completed " 
  m_MSTitles(7) = "The GOOD News... "
  m_MSTitles(8) = "The BAD News... " 
  m_MSTitles(9) = "Final Recommendations... " 
  m_MSTitles(10) = "This Script is Finished. " 
  
  m_MSRemark(1) = "Connecting to Internet (with msInternetExplorer)"
  m_MSRemark(2) = "Downloading Quotes (from msInvestor)"
  m_MSRemark(3) = "Updating Investment Model (using msVBA Macros)"
  m_MSRemark(4) = "Evaluating Portfolio (using msExcel)"
  m_MSRemark(5) = "Analyzing Results (using msVisualBasic)"
  m_MSRemark(6) = "Preparing Report (using msPowerPoint)"
  m_MSRemark(7) = "billy, you've done very well with your investments"
  m_MSRemark(8) = "but billy, your portfolio holds just ONE STOCK"
  m_MSRemark(9) = "DIVERSIFY, DIVERSIFY, DIVERSIFY... "
  m_MSRemark(10)= "  (Billy, contact your broker TODAY... )"
End Sub




' --- INSTANTIATE ACTX OBJECT (or class) AND CHECK ----
'   (using a sub to get this ugly instantiation code out of main line code)...

Sub Instantiate (oObject, sProgramID, sEventPrefix)
Const sME = "[sub Instantiate], "
  ' check variant sub-type parameters...
  BugAssert (VarType(sProgramID) = vbString), sME & "sProgramID must be a STRING!"
  BugAssert (VarType(sEventPrefix) = vbString), sME & "sEventPrefix must be a STRING!"
  On Error Resume Next  ' turn on error checking
  Set oObject = WScript.CreateObject(sProgramID, sEventPrefix)
    BugAssert (err.number = 0), sME & "This script requires: " & sProgramID & vbCrlf _
        & "     kindly INSTALL and REGISTER this ActX component... "
    On Error goto 0  ' turn off error checking...
End Sub


' --- BUGASSERT (yes, it's for debugging) --------

Sub BugAssert (bTest, sErrMsg)

  ' BugAssert is a Bruce McKinney creation.
  '   It is used to test for intermediate results...
  if  bTest  then  Exit Sub
  MsgBox "Error Detected by BugAssert: " & vbCr & vbCr & sErrMsg, _
    vbCritical, " << BugAssert FAILED >> "
  WScript.Quit

End Sub

Apologies for the length of this post but I hope it helps...
 
Thanks a lot.
Just a precision, i see in different web sites, listbox.additem. What is it?
 
Listbox.additem is a method of the listbox object.
What are you scripting for? If it's a web page you could use HTML to give you a List box. If your creating standalone scripts, take a look at HTML Applications.

Everybody body is somebodys Nutter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top