Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Create a ListBox

3,663 views
Skip to first unread message

Andrew HUANG

unread,
Nov 28, 2007, 4:27:01 PM11/28/07
to
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?

Thanks,
-Andrew

McKirahan

unread,
Nov 28, 2007, 4:43:44 PM11/28/07
to
"Andrew HUANG" <Andre...@discussions.microsoft.com> wrote in message
news:54BB901C-05F9-450F...@microsoft.com...

> 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?

Define ListBox? Do you mean an HTML <select>?

If so, that requires an interface which VBScript doesn't really have.
However, you could use an HTA (HTML Application) which is
basically a Web page controled by VBScript (or JScript).

Also, what do you want to do with it?


Andrew HUANG

unread,
Nov 28, 2007, 4:55:00 PM11/28/07
to
Thanks, McKirahan.

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?

Regards,
-Andrew

Tom Lavedas

unread,
Nov 28, 2007, 5:13:40 PM11/28/07
to
On Nov 28, 4:55 pm, Andrew HUANG

<AndrewHU...@discussions.microsoft.com> wrote:
> Thanks, McKirahan.
>
> 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?
>
> Regards,
> -Andrew
>
> "McKirahan" wrote:
> > "Andrew HUANG" <AndrewHU...@discussions.microsoft.com> wrote in message

> >news:54BB901C-05F9-450F...@microsoft.com...
> > > 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?
>
> > Define ListBox? Do you mean an HTML <select>?
>
> > If so, that requires an interface which VBScript doesn't really have.
> > However, you could use an HTA (HTML Application) which is
> > basically a Web page controled by VBScript (or JScript).
>
> > Also, what do you want to do with it?

Here is an example that makes use of InternetExplorer.Application ...

Option Explicit
Dim aOpt
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" _
)
wsh.echo "You selected:", SelectBox("Select a default printer", aOpt)

Function SelectBox(sTitle, aOptions)
Dim oIE, s, item
set oIE = CreateObject("InternetExplorer.Application")
With oIE
.FullScreen = True ' remove if using IE 7+
.ToolBar = False : .RegisterAsDropTarget = False
.StatusBar = False : .Navigate("about:blank")
While .Busy : WScript.Sleep 100 : Wend
.width= 400 : .height=175
With .document
with .parentWindow.screen
oIE.left = (.availWidth-oIE.width)\2
oIE.top = (.availheight-oIE.height)\2
End With ' ParentWindow
s = "<html><head><title>" & sTitle _
& "</title></head><script language=vbs>bWait=true<" & "/
script>" _
& "<body bgColor=ghostwhite><center><b>" & sTitle & "<b><p>" _
& "<select id=entries size=1 style='width:325px'>" _
& " <option selected>" & sTitle & "</option>"
For each item in aOptions
s = s & " <option>" & item & "</option>"
Next
s = s & " </select><p>" _
& "<button id=but0 onclick='bWait=false'>OK</button>" _
& "</center></body></html>"
.open
.WriteLn(s)
.close
Do until .ReadyState ="complete" : Wscript.Sleep 50 : Loop
With .body
.scroll="no"
.style.borderStyle = "outset"
.style.borderWidth = "3px"
End With ' Body
.all.entries.focus
oIE.Visible = True
CreateObject("Wscript.Shell").AppActivate sTitle
On Error Resume Next
While .ParentWindow.bWait
WScript.Sleep 100
if oIE.Visible Then SelectBox = "Aborted"
if Err.Number <> 0 Then Exit Function
Wend ' Wait
On Error Goto 0
With .ParentWindow.entries
SelectBox = .options(.selectedIndex).text
End With
End With ' document
.Visible = False
End With ' IE
End Function

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Andrew HUANG

unread,
Nov 28, 2007, 5:40:00 PM11/28/07
to
Thanks a lot, Tom.

mayayana

unread,
Nov 28, 2007, 5:57:14 PM11/28/07
to
There's also a class here that you can paste into a
script:
www.jsware.net/jsware/scripts.php3#msgb

It provides the ability to easily create custom messages,
listboxes, etc. that have the appearance of the system
msgbox.

Sample pictures are here:
www.jsware.net/jsware/msggal.php3

Two notes:
1) The above script works fine in IE5/6 but in IE7
you can't remove the "chrome" or frame around
the window.

2) Msgbox and Inputbox are pretty much all there
is built into VBS/WSH. The samples you're getting
here use Internet Explorer. If that's OK, and you
don't have IE7, then you're all set. If you wanted
a real system listbox you'd have to use a 3rd-party
ActiveX component that would need to be installed
on all machines where the script would run.


McKirahan

unread,
Nov 28, 2007, 6:35:11 PM11/28/07
to
"Tom Lavedas" <tglb...@cox.net> wrote in message
news:757dd92b-5aa5-4045...@f3g2000hsg.googlegroups.com...

[snip]

> > > "Andrew HUANG" <AndrewHU...@discussions.microsoft.com> wrote in
message
> > >news:54BB901C-05F9-450F...@microsoft.com...
> > > > 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?

[snip]

Tom, not that you care but here's an HTA version of your code.

<html>
<head>
<title>Select a default printer</title>
<HTA:Application ID="HTA"
ApplicationName="ListBox"
Border="thin"
BorderStyle="normal"
Caption="yes"
Icon=""
MaximizeButton="yes"
MinimizeButton="yes"
ShowInTaskBar="yes"
SingleInstance="yes"
SysMenu="yes"
Version="1.0"
WindowState="maximize"
>
<script type="text/vbscript">
Option Explicit

Sub Window_Onload()
Const cWid = 400
Const cHei = 175
window.resizeTo cWid, cHei
window.MoveTo screen.width/2-(cWid/2),screen.height/2-(cHei/2)
'*


Dim aOpt
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" _
)

Dim iOpt
Dim sOpt
sOpt = "<b>" & document.title & "<b><p>" _
& "<select id='entries' style='width:325px'>" & vbCrLf _
& "<option selected>" & document.title & "</option>" & vbCrLf
For iOpt = 0 To UBound(aOpt)
sOpt = sOpt & "<option>" & aOpt(iOpt) & "</option>" & vbCrLf
Next
sOpt = sOpt & "</select><p>" & vbCrLf _
& "<button onclick='Selected()'>OK</button>"
document.getElementById("opts").innerHTML = sOpt
End Sub

Sub Selected()
Dim iSel
iSel = document.getElementById("entries").selectedIndex
Dim sTxt
stxt = document.getElementById("entries").options(iSel).text
Alert "You selected: " & sTxt
End Sub
</script>
</head>
<body bgColor="ghostwhite">
<center>
<span id="opts"></span>
</center>
</body>
</html>


Csaba Gabor

unread,
Nov 28, 2007, 11:19:32 PM11/28/07
to
On Nov 28, 11:13 pm, Tom Lavedas <tglba...@cox.net> wrote:
> On Nov 28, 4:55 pm, Andrew HUANG
> <AndrewHU...@discussions.microsoft.com> 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...@discussions.microsoft.com> wrote in message
> > >news:54BB901C-05F9-450F...@microsoft.com...
> > > > 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

Henry Bordeleau

unread,
Jan 31, 2008, 10:57:29 AM1/31/08
to
Is there any way to set the focus on this listbox window so it comes to the
top? Whenever I run it as a script is appears behind the calling script
window. I would rather it had the focus so you can select the option as
needed.

url:http://www.ureader.com/msg/16756906.aspx

Csaba Gabor

unread,
Feb 4, 2008, 7:06:10 AM2/4/08
to
On Jan 31, 4:57 pm, "Henry Bordeleau"<henr...@DontWantNoSPAM.ca>
wrote:

Yes. Check out the following posts in this group from
February 2005 on bringing IE / Popups to the foreground:

Feb 21: Bringing IE to the foreground
http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_frm/thread/4919f0ad47e35c2a/

Feb 25: Bringing Script MsgBox to foreground
http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_frm/thread/f02a828a7c5f381d/

Feb 28: Active Window
http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_frm/thread/b5a4788bb2dacc09/

On topness (without some kind of coersion) seems to often be dictated
by whether there are already any apps from the same (thread/app/class/
exe/something) active.

Csaba Gabor from Vienna

0 new messages