Chương Bảy - Dùng List Controls (bài thứ hai)
Listbox
Cách dùng MultiSelect
Cho đến giờ User click vào Listbox để chọn chỉ một Item. Khi một Item được chọn thì
hàng ấy trở nên highlighted với background màu xanh đậm. Nếu kế đó ta click một
hàng khác thì hàng cũ được display trở lại bình thường và hàng mới đuợc selected sẽ
trở nên highlighted.
Listbox cho ta có thể select nhiều Items cùng một lúc bằng cách set Property
MultiSelect = Extended
Ðối với MultiSelected Listbox, ta chọn một nhóm Items liên tục bằng cách click Item
đầu rồi nhấn nút Shift trong khi click Item cuối. Ta cũng có thể tiếp tục
Select/Deselect thêm bằng cách ấn nút Ctrl trong khi click các Items. Nếu ta click một
Item chưa được selected thì nó sẽ trở nên selected (highlighted màu xanh), nếu ta
click một Item đã được selected rồi thì nó sẽ trở nên deselected (không còn màu xanh
nữa). Thí dụ trong program bạn click "Peter Jones", kế đó ấn nút Shift trong khi click
"Sue Rose", kế đó buông nút Shift ra để ấn nút Ctrl trong khi click "Kevin White", bạn
sẽ có những selected Items như trong hình dưới đây:
Ngoài ra bạn cũng có thể MultiSelect nhiều Items trong một Listbox bằng cách dùng
mouse để drag, tức là bạn click lên Item đầu rồi tiếp tục đè mousebutton trong khi
kéo mousepointer đến Item cuối cùng mới buông mousebutton ra.
Cái Bug ác ôn
Bây giờ giả sử ta muốn delete tất cả những Items vừa được selected (highlighted).
Bạn hãy đặt một CommandButton mới tên CmdDeleteSelectedItems vào Form. Ta sẽ
dùng Event Click của Button nầy để delete những selected Items. Một selected Item
của lstNames sẽ có property Selected của nó bằng True. Tức là nếu Item thứ ba
(ListIndex=2) được selected thì ta có lstNames.Selected(2) = True. Ta có ý định
sẽ iterate through mọi Items của lstNames, để xem Item nào được selected thì mình
sẽ delete nó bằng cách dùng method RemoveItem. Ta sẽ viết code cho Sub
CmdDeleteSelectedItems_Click() như sau:
Private Sub CmdDeleteSelectedItems_Click()
Dim i
ngay dưới removed Item được đẩy lên. Listing mới như sau:
Private Sub CmdDeleteSelectedItems_Click()
Dim i
i = 0 ' Initialise value of i to start from first Item
' Note that lstNames.ListCount is evaluated freshly at each iteration
Do While i <= (lstNames.ListCount - 1)
If lstNames.Selected(i) = True Then
lstNames.RemoveItem i
' No need to increment i here because the item below is pushed up
Else
i = i + 1 ' increment i to process the next item
End If
Loop
End Sub
Dùng Listbox để display Event Log
Trong thí dụ sau đây ta muốn display input từ một serial COM port (Ðể đọc data thật
qua serial COM port ta phải dùng control MsCOMM có hình telephone màu vàng trong
ToolBox). Nhưng để tiện việc biểu diển, thay vì đọc một message từ một serial COM
port, ta sẽ emulate nó bằng cách dùng một ComboBox. Khi user select một hàng từ
ComboBox cboInput thì ta xem cboInput.text như data đọc từ serial COM port. Lập
tức lúc ấy ta sẽ display input message trong hai dạng: ASCII và HEX. Mỗi hàng input
message được prefix với ngày và giờ trước khi được cho vào hai Listboxes lstASCII và
lstHexadecimal.
Hình dưới đây cho thấy Form đang display trong ASCII mode.
Nếu bạn click button Display in HEX thì caption của button đổi thành Display in
ASCII, lstASCII trở nên vô hình và lstHexadecimal sẽ hiện ra như sau:
Dưới đây là listing của Function HexDisplay để convert từ ASCII string ra Hexadecimal
string.
Function HexDisplay(InASCII) As String
' Convert an ASCII string to HEX string
' Remove the first Item, i.e. the oldest item
lstHexadecimal.RemoveItem 0
End If
' Highlight the lattest item in the Listbox
lstHexadecimal.ListIndex = lstHexadecimal.ListCount - 1
' Use different log file each day. Filename has format like
Hex15Jun01.log
LogFileName = "Hex" & Format(Now, "ddmmmyy") & ".log"
' Log to file including Date and Time
LogEvent LogFileName, Mess, False, 2
End Sub
In ra content của Listbox
Dưới đây là một áp dụng của Listbox MutiSelect để in ra cả Listbox hay chỉ những
hàng được selected. Sub PrintList nhận:
• Listbox mà ta muốn in
• một Boolean value mà nếu True thì in cả Listbox
• Title của Printout
Sub PrintList(theList As ListBox, PrintAll as Boolean, Title As String)
' Print the whole lot or only selected lines in a listbox
' PrintAll = True means printing the whole content of the listbox
Const MaxLinesPerPage = 50
Dim msg, i, j, PageNo, NumLines, HasSome, Margin
HasSome = False ' Flag indicating existence of data
Margin = Space(10) ' Make a margin of 5 characters
Title = vbLf & vbLf & Title + vbCrLf & vbLf
NumLines = 0 ' Init number of lines on this page
PageNo = 1 ' init Page number
msg = Title ' Msg will contain everything starting with Title
Printer.FontName = "Courier New" ' Initialise Printer Fontname
Printer.FontSize = 10 ' Initialise Printer FontSize