首页->技巧->控件->详细内容

Scrolling Through Two List Box Controls Simultaneously

Abstract
You can add code to your Visual Basic application to allow a user to
scroll through the contents of two separate List Box controls at the
same time. This article explains how you can add this functionality to
your program.

The TopIndex Property of List Box Controls
When using a List Box control, the user can click the mouse on the
scroll bar to move up or down the list of items. If the user clicks
the mouse on an individual item, that item is said to be selected. The
ListIndex property is a unique value that represents the selected
item's position within the List Box.

You can also scroll through a List Box control by using the TopIndex
property. This property, however, can only be changed at run time, not
during design time. The TopIndex property moves you through the items
in the List Box control. In other words, it works just as if the user
had used the scroll bar.
Let's assume that you have two List Box controls on a form in your
Visual Basic application. As you scroll through the items in the
first List Box control, you want to also scroll through the same items
in the second List Box control.

In an application, you can use the TopIndex property to move a
specific item in the List Box control so that that item appears at
the top of the List Box. The following statement, for example, moves
the third item in the List Box to the top of the control:

List1.TopIndex = (2)

In the example program below, we want to scroll through both List Box
controls at the same time. To do this, we use a Timer control so that
the second List Box control is updated as soon as the item is selected
in the first List Box control.

We first use a static variablethat is, a variable whose contents do
not change when we exit a procedureto keep track of the currently
selected item in the first List Box. Each time a new item is selected
in the List Box, this variable is set to that item's TopIndex value.
Next, we set the ListIndex property of the second List Box control
equal to that of the first List Box control. This highlights the two
items in each List Box that have the same ListIndex value. It doesn't
matter what the actual item is the items are both selected based on
their position within the controls.

Each time you select an item in the first List Box, that same item is
also selected in the second List Box.

Example Program
This example program shows how to scroll through the contents of two
List Box controls simultaneously.

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following code to the General Declarations section of
Form1:

Option Explicit
DefInt A-Z

3. Add the following code to the Form_Load event for Form1:

Private Sub Form_Load()
Dim X As Integer
'Initialize two list boxes with alphabet
For X = 1 To 26
List1.AddItem Chr$(X + 64)
Next X
For X = 1 To 26
List2.AddItem Chr$(X + 64)
Next X
timer1.Interval = 1
timer1.Enabled = True
End Sub

4. Add a List Box control to Form1. List1 is created by default.
5. Add a second List Box control to Form1. List2 is created by
default.
6. Add a Command Button control to Form1. Command1 is created by
default.
7. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
End
End Sub

8. Add a Timer control to Form1. Timer1 is created by default.
9. Add the following code to the Timer1 event for Timer1:

Private Sub timer1_Timer()
Static PrevList1
Dim TopIndex_List1 As Integer

'Get the index for the first item in the visible list.
TopIndex_List1 = List1.TopIndex

'See if top index has changed.
If TopIndex_List1 <> PrevList1 Then
'Set the top index of List2 equal to List1,
'so that the list boxes scroll together.
List2.TopIndex = TopIndex_List1
PrevList1 = TopIndex_List1
End If
'Select the item in the same position in both list boxes.
If List1.ListIndex <> List2.ListIndex Then
List2.ListIndex = List1.ListIndex
End If

End Sub
返回

Detecting Right Mouse Button Clicks on List Box ControlsAbstract

When using a List Box control in a Visual Basic application, the user
can click on an item with the left mouse button. That item then
becomes selected. This article explains how you can select items with
the right mouse button instead of the left mouse button.

Intercepting Right Mouse Button Click Events
The LB_GETITEMRECT message can be used to determine which item in a
List Box was selected. This message retrieves the coordinates of a
bounding rectangle for the selected item in the List Box control. To
invoke this message, you must tell it the entry number, starting at
zero, whose dimensions you want to retrieve, as well as a RECT
structure that will hold the coordinate information.

To determine which item a user clicked on with the right mouse button,
you trap the MouseUp event. The MouseUp event can be used to determine
which mouse button was pressed and the mouse's current X and Y
coordinates on the form or control.
Once we have determined the mouse's position over the List Box control,
we can use the Windows application programming interface (API)
SendMessage function to return the index number of the item the mouse
was positioned over when the MouseUp event was triggered.

Example Program
The example program below displays a List Box control on a form.
Whenever you click the right mouse button on an item in the List Box,
the message "Right Click on" is displayed in the Text Box along with
the index number corresponding to the selected item.

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following code to the Form_Load event for Form1:

Private Sub Form_Load()
List1.AddItem "Item #1"
List1.AddItem "Item #2"
List1.AddItem "Item #3"
End Sub

3. Add a List Box control to Form1. List1 is created by default.
4. Add the following code to the MouseUp event for List1 (note that
the Private lines must be typed as a single line of code):

Private Sub List1_MouseUp(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Dim Item%
If (Button = 2) Then
Item% = GetRClickedItem(List1, X, Y)
If (Item% = LB_ERR) Then
Text1.Text = "ERROR"
Else
Text1.Text = "Right Click on " + Str(Item%)
End If
End If
End Sub

5. Add a Text Box control to Form1. Text1 is created by default.
6. Add a new module to the project. Module.Bas is created by default.
7. Add the following code to the Module.Bas file (note that the
Private and If lines must be typed as a single line of code):

Type RECT
Left As Integer
Top As Integer
Right As Integer
Bottom As Integer
End Type

Global Const WM_USER = &H400
Global Const LB_GETITEMRECT = (WM_USER + 25)
Global Const LB_ERR = (-1)

Private Declare Function SendMessage Lib "User" (ByVal hWnd
As Integer, ByVal wMsg As Integer, ByVal wParam
As Integer, lParam As Any) As Long
Function GetRClickedItem%(MyList As Control, X As Single, Y As Single)

Dim ClickX%, ClickY%, Ret&, CurRect As RECT
ClickX% = X \ Screen.TwipsPerPixelX
ClickY% = Y \ Screen.TwipsPerPixelY
i% = 0
Do While True
Ret& = SendMessage(MyList.hWnd, LB_GETITEMRECT, i%, CurRect)
If (Ret& = LB_ERR) Then
GetRClickedItem% = LB_ERR: Exit Function
End If
If (ClickX% >= CurRect.Left) And (ClickX% <= CurRect.Right) And
(ClickY% >= CurRect.Top) And (ClickY% <= CurRect.Bottom) Then
GetRClickedItem% = i%: Exit Function
End If
i% = i% + 1
Loop
End Function
返回Retrieving Multiple Filenames from the Common Dialog Control

Abstract
The Common Dialog control in Visual Basic allows you to display an
Open File dialog box. You can select one or more filenames from the
Open File dialog box to use within your Visual Basic program. This
article explains how you can retrieve multiple filenames from the
dialog box, parse them into separate strings, and display them in a
List Box control.

Parsing Filenames from the Common Dialog Control
In a Visual Basic application, you can use an Open File dialog box
to allow your users to select a file. Using the Open File dialog box,
users can select the drive and directory, as well as the individual
files they want to use. To select a file, the user simply clicks the
filename. The dialog boxs FileName property can be used in your
program to determine the name of the selected file.

If the Flags property of the Common Dialog control is set to a value
of 512 (&H200), the user can select a group of files to work with. To
select multiple files, the user would hold the Shift key down while
clicking the mouse on each filename. As with selecting a single file,
the FileName property of the dialog box would return the names of all
the selected files. Each filename is separated by a space character.
The InStr function can be used within a Do-While loop to parse, or
extract, each individual filename from the FileName property. Assuming
that the filenames are stored in the string called FileNames, we can
tell the InStr function to search through the string until it finds a
space character. To extract a single filename, you need to first save
the position in the target string that you are starting to search from
(this is the beginning of the filename). Then you would use the InStr
function to search for the first space character in the string. If a
space character is found, you can use the starting position and the
position returned by InStr to extract that single filename.

Example Program

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add a Common Dialog control to Form1. CommonDialog1 is created by
default.
3. Add a List Box control to Form1. List1 is created by default.
4. Add a Command Button control to Form1. Command1 is created by
default.
5. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
Dim DelimPos As Integer
Dim FileNames As String
Dim NextName As String

CommonDialog1.Flags = &H200&
CommonDialog1.Action = 1
CommonDialog1.Filter = 1

FileNames = CommonDialog1.FileName

Do While Len(FileNames) > 0
DelimPos = InStr(FileNames, " ")
If DelimPos = 0 Then
NextName = FileNames
FileNames = ""
Else
NextName = Mid$(FileNames, 1, DelimPos - 1)

FileNames = Mid$(FileNames, DelimPos + 1)
End If
List1.AddItem NextName
Loop
End Sub
返回

Creating a Scrolling "Credits" Control

Abstract
You can add visual appeal to your Visual Basic=AE applications by
including a routine that automatically scrolls text vertically within
a picture box. This article explains how you can add this
functionality to your programs.

Scrolling Text Vertically Within a Picture Box
The Windows=AE application programming interface (API) BitBlt function
can be used to copy a section of a Picture Box control to another
section of that same control. You must remember to set the ScaleMode
property of the Picture Box control to Pixel mode.

The example program below shows how to use the BitBlt function to
print scrolling text on a Picture Box control. A Timer control is
used to print a string of text on the Picture Box control at selected
time intervals.

Example Program
1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following code to the General Declarations section of
Form1 (note that the Declare statement must be typed as a single
line of code):

Const SRCCOPY =3D &HCC0020
Const ShowText$ =3D "This line of text scrolls vertically."

Private Declare Function BitBlt Lib "GDI" (ByVal hDestDC As Integer,
ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer,
ByVal nHeight As Integer, ByVal hSrcDC As Integer,
ByVal XSrc As Integer, ByVal YSrc As Integer,
ByVal dwRop As Long) As Integer

Dim ShowIt%

3. Add a Picture Box control to Form1. Picture1 is created by
default. Set its ScaleMode property to 3-Pixel.
4. Add a Timer control to Form1. Timer1 is created by default. Set
its Interval property to 25.
5. Add the following code to the Timer event for Timer1 (note that
the Ret =3D line must be typed as a single line of code):

Private Sub Timer1_Timer()
Dim Ret As Integer
If (ShowIt% =3D 30) Then
Picture1.CurrentX =3D 0
Picture1.CurrentY =3D Picture1.ScaleHeight - 30
Picture1.Print ShowText$
ShowIt% =3D 0
Else
Ret =3D BitBlt(Picture1.hDC, 0, 0, Picture1.ScaleWidth,
Picture1.ScaleHeight - 1, Picture1.hDC, 0, 1, SRCCOPY)
ShowIt% =3D ShowIt% + 1
End If
End Sub

Run the program by pressing the F5 function key. After a short time,
the text "This line of text scrolls vertically." will be displayed in
the Picture Box control. Each time the Timer control reaches 25, the
line of text will be scrolled upward in the Picture Box control.
返回

Using Drag-and-Drop on Multiple items in a List Box Control

Abstract
The drag-and-drop functionality provided in many Windows厝based
applications allows you to copy an item from one program to another
or from one control to another control in the same application. This
article explains how to use this drag-and-drop technique in Visual
Basic to copy multiple items selected in a List Box control to
another List Box control.

Dragging Multiple list Box Items
Many Windows-based applications include drag-and-drop functionality.
This means that you can select an item, such as an entry in a List
Box control, click on the item, and, while holding the mouse button
down, drag that item to another window or control and drop it on its
new location.

The example program below shows how you can add this drag-and-drop
feature to your Visual Basic applications. This program allows you
to select multiple items in the source List Box control and drag the
whole group of selected items to a second List Box control all at one
time.

Example Program
This program shows how to drag several items selected in one List Box
control to another List Box control. Run the example program by
pressing the F5 function key. From the first List Box control, click
the mouse on several items to select (highlight) them. While clicking
each item, hold down the SHIFT key. When you want to drag the selected
items to the second List Box control, click once on the first List Box
control and hold the mouse button down while you drag the control to
the second List Box. Release the mouse button to drop the selected
items onto the second List Box control.

While using this program, you can select the items from the first
List Box either by holding the SHIFT key down while you click on each
entry, or by simply clicking the mouse on each individual entry. If
you hold the SHIFT key down when selecting entries, those entries will
remain selected (highlighted) in the first List Box control after the
items have been dropped onto the second List Box control. If the
SHIFT key is not used, one of the selected items will not retain its
selected status after the drag-and-drop operation has finished.

1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following code to the General Declarations section of
Form1:

Option Explicit
Dim IG As Integer
Dim LIG(20) As Integer
Dim LGlobal As Long
Const VK_SHIFT = &H10

3. Add the following code to the Form_Load event for Form1:

Private Sub Form_Load()
Dim X As Integer
IG = 0
For X = 0 To 9
List1.AddItem "Item #" + Str$(X)
Next X
List1.DragMode = 0
LGlobal = 99999
End Sub

4. Add a List Box control to Form1. List1 is created by default. Set
its MultiSelect property to 1-Simple.
5. Add the following code to the MouseDown event for List1 (note that
the Private line must be typed as a single line of code):

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single,
Y As Single)
LGlobal = List1.ListIndex
For X = 1 To IG
List1.Selected(LIG(X)) = True
Next X
List1.Drag
End Sub

6. Add a second List Box control to Form1. List2 is created by
default. Set its MultiSelect property to 1-Simple.
7. Add the following code to the DragDrop event for List2:

Private Sub List2_DragDrop(Source As Control, X As Single, Y As Single)
For X = 0 To List1.ListCount - 1
If X = LGlobal Then
List2.AddItem List1.List(X)
Else
If List1.Selected(X) Then
List2.AddItem List1.List(X)
End If
End If
Next X
LGlobal = 99999
IG = 0
End Sub
返回

Back to top