Re: How to Create a ListBox



On Nov 28, 11:13 pm, Tom Lavedas <tglba...@xxxxxxx> wrote:
On Nov 28, 4:55 pm, Andrew HUANG
<AndrewHU...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
No. I want to create a listbox only in VBScript, no HTA.
You know, there are some user components in VBScript, just like MsgBox,
InputBox. Have any onke like ListBox to select item in it?
....
"McKirahan" wrote:
"Andrew HUANG" <AndrewHU...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:54BB901C-05F9-450F-86E8-B6AAF2151FAE@xxxxxxxxxxxxxxxx
Hi, All
I would like to use a ListBox in VBScript.
I am not using a web page or ASP
Is there any way to do it using VBScript?
....
Here is an example that makes use of InternetExplorer.Application ...

.... Nice example

Tom Lavedas

Since everyone is posting examples...
Hi Tom, thanks for that nice example.
Here, without ie formatting, is an example that demonstrates
the same listbox functionality only it is an event driven
mechanism, hooking up VBScript functions to act as
IE event handlers. In particular, the main script displays
the listbox and then sits in an infinite loop. When the
selection is made, the return will be to a different
function (this also means that you can't call on a
function to show the listbox and provide a return
value - it must be processed separately as with
IE's DOM/javascript approach).

When the listbox returns (in this example), it does
what needs doing and then tells IE to quit. That, in
turn, causes another routine to fire which will set a
variable keepLooping to false, which will terminate
the entire script. Note that there are no more
On Error statements necessary.

Event handler hookup is a bit squirrelly. I have not found
a direct way to do it, though in a separate post, I have
shown how to do it directly for window.setTimeout. For
event handler hookup you must first allocate space in a
separate window variable, then set the function using
GetRef, and then set the handler which will reference
the originally desired event handler. Seems like there
ought to be a simpler way. But at least we are able to
get arguments back to the VBScript function, which
is already something to be happy about.

Csaba Gabor from Vienna


Option Explicit
Dim aOpt, ie, keepLooping
aOpt = Array("\\mail\HP 4100 - Clinical - Clare", _
"\\mail\HP 4000 - Admissions", _
"\\medical\HP LaserJet 4000 - Medical", _
"\\mail\HP 4000 - Front Desk", _
"\\mail\HP 4000 - Business Office", _
"\\mail\HP 4100 - Clinical - HRC", _
"\\mail\HP 4100 - Business Office", _
"\\victoria\HP 4500" _
)

DisplayListbox "Select a default printer", aOpt

keepLooping = true
While keepLooping
WScript.Sleep 1000
Wend
MsgBox "Script has ended"

Sub ListboxReturns (lection)
'Handles listbox selection
CreateObject("WScript.Shell").Popup _
lection & vbCrLf & " has been selected", 4, _
"Selection", 131120
'Do anything else here
ie.Quit ' Signal exit to IE
End Sub

Sub onQuit ' Exit handler for ie
CreateObject("WScript.Shell").Popup _
"IE has quit", 4, _
"Termination notice", 131120
keepLooping = false ' Signal exit to WScript
End Sub

Sub DisplayListbox (sTitle, aOptions)
Dim bodyHTML, i 'define the listbox
bodyHTML = _
"<select id=entries size=1 onChange=" & _
"'tmpFunc(this.options[this.selectedIndex].text)'>" & _
"<option selected>" & sTitle & "</option>"
For i=LBound(aOptions) To UBound(aOptions)
bodyHTML = bodyHTML & "<option>" & _
aOptions(i) & "</option>"
Next

' Create the listbox
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate2 "about:blank"
ie.document.title = sTitle
ie.document.body.innerHTML = bodyHTML

' set up the exit handler
ie.document.parentWindow.execScript _
"window.myUnload=false"
ie.document.parentWindow.myUnload = GetRef("onQuit")
ie.document.parentWindow.execScript _
"window.onunload=window.myUnload"

' set up the selection handler
ie.document.parentWindow.execScript _
"window.tmpFunc=false"
ie.document.parentWindow.tmpFunc = _
GetRef("ListboxReturns")

ie.width = ie.document.all.entries.offsetWidth + 40
ie.visible = true
End Sub
.



Relevant Pages

  • Re: Threading WinForm
    ... a> complaining about the listbox being in another thread. ... a> in the button event handler I start the thread ... a> private static void SimulatedClient ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: ListBox, Double-Click and Closing Form
    ... > is there a workaround? ... > Set up a Double-click event handler for a ListBox. ... > doesn't matter. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Newbie Learning Threading
    ... > populates a listbox. ... and hooks up the new one. ... you've only got an event handler for the last one. ...
    (microsoft.public.dotnet.general)
  • Re: How to Create a ListBox
    ... I want to create a listbox only in VBScript, no HTA. ... Dim oIE, s, item ...
    (microsoft.public.scripting.vbscript)
  • Re: How to Create a ListBox
    ... "Tom Lavedas" wrote: ... I want to create a listbox only in VBScript, no HTA. ...
    (microsoft.public.scripting.vbscript)