NewListBox

Description

Adds a single column, unsorted listbox control to a form.

Syntax

[Set oCtl =] oFrm.NewListBox

( Name, Left, Top, Width, Height, [Container] )

Parameters

Part

Description

Set oCtl =

(optional) Store a reference to the new control in the variable oCtl. This variable can be used to access the control's properties and methods.

oFrm

A reference to a form object (see the NewForm method)

Name

The name of the control. It must be unique within the form and may not be empty or start with an asterisk (*)

Left

See the list of properties below

Top

See the list of properties below

Width

See the list of properties below

Height

See the list of properties below

Container

(optional) The name of an existing frame control that will act as container for the listbox. If unspecified or an empty string, the Form itself will be the container. Please note that the Left and Top properties are relative to the container's left and top edges

Properties, Methods and Events

Property

Description

BackColor

Background color (use the RGB function to assign a color value)

Columns

(read-only) Returns 0 to indicate a single column listbox

Enabled

(boolean) Determines if the control can respond to user-generated events

Fontname

The font used to display text. The default is determined by the system. Check the glossary for an explanation about setting font properties.

Fontsize

The size of the font in points. The maximum is 2160 points.

Fontbold

(boolean) Enable or disable the bold style of a font

Fontitalic

(boolean) Enable or disable the italic style of a font

Fontstrikethru

(boolean) Enable or disable the strikethru style of a font

Fontunderline

(boolean) Enable or disable the underline style of a font

ForeColor

Foreground color (use the RGB function to assign a color value)

Height

(read-only) The height of the control in twips

IntegralHeight

(read-only) The control resizes to display only complete items.

ItemData

(long integer) A specific number associated with an item in the list. You can use these numbers to further identify the items. For example, you can use an employee's identification number to identify each employee name.

Note : When you insert an item with the AddItem method, the ItemData value isn't reinitialized to zero; it retains the value that was in that position before you added the item. When you use the ItemData property, be sure to set its value when adding new items to a list.

Left

The distance from the left edge of the specified container in twips

List (index)

(string) The text of an item in the list at the specified index. The first item has index 0 and the index of the last item is ListCount – 1.

ListCount

(read-only) The total number of items in the list

ListIndex

(integer) Returns or set the index of the currently selected item in the list

-1

Indicates no item is currently selected or the user has entered new text into the text box portion

n

(default) A number indicating the index of the currently selected item. The index of the first item is 0 and the index of the last item is ListCount – 1

MultiSelect

(read-only) Returns 0 to indicate that multiple selection isn't allowed

NewIndex

(read-only) The index of the item most recently added. Returns -1 if there are no items or if an item was deleted since the last item was added.

Parent

(read-only) Returns the parent form, object or collection. You can use the parent property to access the properties and methods of an object's parent

Selected (index)

(boolean) Returns or sets the selection status of an item in the list. This is particularly useful if MultiSelect is set. Otherwise use the ListIndex property

Sorted

(read-only) Returns False to indicate the items are not automatically sorted

Style

(read-only) Returns 0 to indicate that the control is a standard listbox (without a checkbox next to each item)

TabStop

(boolean) Determines whether the TAB key can be used to move the focus

Tag

(reserved) Used internally to store the user-defined controlname

Text

(read-only) The currently selected item. Equivalent to List(ListIndex)

ToolTipText

Explanatory text that appears in a small rectangle below the object when you pause the mouse pointer (hover) over it for about one second

Top

The distance from the top edge of the specified container in twips

TopIndex

(integer) The index of the item displayed in the topmost position

Visible

(boolean) Determines whether the control is visible or hidden

Width

The width of the control in twips

Method

Description

AddItem

Parameters : item [, index ]

item

(string) The item to be added to the the list

index

(integer) The position in the list where the new item is placed. If index is omitted, the item is added to the end of the list

Clear

Clears all the items in the list

Move

Parameters: left [, top [, width ]]

Only the left argument is required. However, to specify any other arguments, you must specify all arguments that appear in the syntax before the argument you want to specify. For example, you can't specify width without specifying left and top. Any trailing arguments that are unspecified remain unchanged

Refresh

Forces a complete repaint of a control

RemoveItem

Parameter : index

Removes the item at the specified index from the list

SetFocus

Moves the focus to the specified control

Event

Description

Event raised

Click

Occurs when the user selecs an item with the left mouse button, the arrow keys or the HOME, END, PGUP or PGDN keys

Controlname

To be able to handle events you need to operate a form in callback mode.
This subject is described in the following topics:
Callback forms, ClickHandler

Example

Option 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 listbox control named LBX1 to the oFrm form
'and store a reference to the control in the variable oCtl
Set oCtl = oFrm.NewListBox("LBX1", 150, 150, 2500, 1000)

'Use the reference variable to add some items to the listbox
oCtl.AddItem "The first item"
oCtl.AddItem "The second item"
oCtl.AddItem "The third item"
oCtl.AddItem "The fourth item"
oCtl.AddItem "The fifth item"

'Make the first item (with index 0) the currently selected item
oCtl.ListIndex = 0

'Add an OK button and set it's Default property
Set oCtl = oFrm.NewButton("OK", 900, 1250, 1000, 375, "&OK")
oCtl.Default = True

'Automatically size the form and show it (modally)
oFrm.Autosize
oFrm.Show vbModal

'Show which item was selected (only if the OK button was clicked)
If oDlg.Clicked = "OK" Then
    MsgBox oFrm.Ctl("LBX1") & " (index " & oFrm.Ctl("LBX1").ListIndex & ") was selected"
End If