Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Kha Hm Th Visual Basic 6.

0
Listbox
Search trong Text File Ta bit rng ListBox c th cha rt nhiu hng text (con s hng ti a l 65535). Ta quen vi vic hin th content ca mt text file trong mt Listbox. Ta dng ListBox display cc Events (s c) xy ra trong real-time. Gi d, ta ghi li tt c mi Events xy ra trong realtime ca mt h thng an ninh, tc l ta bit ai ra, vo ca no, lc my gi. Cc Events ny va uc log xung mt Text file, va c cho vo mt ListBox lun lun hin th Event mi nht cui ListBox. Khi c mi Events nm trong ListBox, ta c th Search (tm kim) xem mt ngi no i qua nhng ca no ca building bng cch iterate qua tng hng trong ListBox v nhn din mt Text Pattern hng vi Function InStr. Trong bi mu di y, ta nh tn ca mt ngi vo trong TextBox ri click nt Find v sau Find Next highlight nhng Events trong ListBox cho thy nhng lc tn ngi xut hin. Trong khi tm kim mt Text Pattern ta c th cho php c ch Hoa , ln ch Thng bng cch covert mi text ra Uppercase trc khi lm vic vi chng.

Listing ca Sub Find_Click nh sau:


Private Sub CmdFind_Click() Dim i, ALine, FText ' Get out if the Listbox is empty If EventList.ListCount = 0 Then MsgBox "There 's no text available" Exit Sub End If ' Check if user has entered the Text Pattern If Trim(txtFind) = "" Then MsgBox "Please enter the Text Pattern to search for" Exit Sub End If ' Clear all selected lines For i = 0 To EventList.ListCount - 1 EventList.Selected(i) = False Next ' Convert the Text Pattern to Uppercase

FText = UCase(txtFind.Text) ' Iterate through every line in the ListBox For i = 0 To EventList.ListCount - 1 ' Convert this line to Uppercase ALine = UCase(EventList.List(i)) ' If pattern exists in this line then highlight it If InStr(ALine, FText) > 0 Then EventList.Selected(i) = True ' Highlight the line ' Mark Current line as the Starting line for FindNext operation If i < EventList.ListCount - 1 Then CurrentLine = i + 1 ' get out Exit Sub End If Next ' Only get here if Not found MsgBox "Not found!" End Sub

Trong bi ny ta c dng mt DriveListBox cho User chn mt Disk drive, mt DirListBox user chn mt Folder/Directory v mt FileListBox hin th tn ca nhng Files trong mt Folder. C ba loi ListBoxes ny lin kt nhau cho ta thy s thay i n nhp mi khi User i t Disk Drive ny qua Disk Drive khc, hay t Folder ny qua Folder khc. Cc hng codes thc hin vic ny rt n gin nh sau:
Private Sub Drive1_Change() ' Make Path of Folder same as new Drive Dir1.Path = Drive1.Drive End Sub Private Sub Dir1_Change() ' Make Path of FileList same as new Path of Folder ' The filenames in the Folder will be displayed automatically in FileListBox FileList.Path = Dir1.Path End Sub

Ta c th chn la ch nhng Filenames c mt Extension no (th d nh log) bng cch cho Property Pattern ca FileListBox value "*.log". Mi khi User click ln tn ca mt File, program s load content ca File y vo ListBox EventList bn phi. Sau khi selected mt s hng ri, User c th hoc Print chng ra bng cch Click nt Print, hoc Copy chng vo Clipboard bng cch Click nt Copy. Bn c th download source code ca program LogFile.zip ny c y . Dng ItemData

Nu Property List ca ListBox c xem nh mt Text Array th ItemData l mt Number Array, v List1.ItemData(i) i cp vi List1.List(i). Tc l trong khi List1.List(i) hin th nh mt trc ca mt tm bn th List1.ItemData(i) c coi nh nm mt sau ca tm bn y. Khi mt List item thay i v tr trong Listbox v c s bin i trong ListBox (th d Items b removed hay c cho thm vo) th ItemData ca List item cng i theo vi n. Ta th xem th d sau. Cho vo mt Sorted Listbox tn ca cc nhn vin trong s. Ngay sau khi tn mt nhn vin c cho vo Listbox th Property NewIndex cha v tr ca item mi c cho vo y trong ListBox. Ta dng gi tr NewIndex assign ItemData vi S nhn vin ID. Khi User clicks ln mt tn trong Listbox, program s hin th c S nhn vin ID ln tn nhn vin. th th d ny, bn c th Paste phn code di y vo phn Declaration ca mt Form c cha mt Listbox v mt Label. Nh set property Sorted ca List1 ra True.

Private Sub Form_Load() ' Fill List1 and ItemData array with ' corresponding items in sorted order. List1.AddItem "John Green" ' Add an employee name ' Use NewIndex to synchronise with Employee ID ' Assign Employee ID to ItemData of the List Item List1.ItemData(List1.NewIndex) = 62310 List1.AddItem "Tran The Tam" List1.ItemData(List1.NewIndex) = 42859 List1.AddItem "Alan Bradshaw" List1.ItemData(List1.NewIndex) = 63732 List1.AddItem "Peter Costello" List1.ItemData(List1.NewIndex) = 34127 End Sub Private Sub List1_Click() ' Fetch the employee number Msg = List1.ItemData(List1.ListIndex) & " " ' Concatenate it with the employee name. Msg = Msg & List1.List(List1.ListIndex)

' Assign string to Label to display Label1.Caption = Msg End Sub

Dng ListBox lm Queue Khi i coi ht, ta thng phi ng sp hng mua v. Ci hng gi l Queue. Mc ch ca vic dng Queue l cho s ngi ng cn mt dch v s c phc v ln lt theo th t ai n trc s c gii quyt trc. Nguyn tc ca Queue nh th c gi l FirstIn-First-Out ( vo trc nht, ra trc nht). Ngc li, nu ai cng mun c phc v trc nht ta s c s no lon, v rt cuc c th chng c ai c gii quyt. Th d ta c mt Form tn frmServer, m trong c mt Listbox tn List1. Nu c nhiu Forms khc trong cng mt chng trnh mun nh frmServer phc v mt chuyn g, chng s Queue bng cch Add mt Item vo cui List1. Trong Item c cha nhng chi tit m frmServer s cn bit phc v.
Private Sub CmdAddToQueue_Click() Dim myRequest As String Dim PersonId As String * 5 Dim PersonName As String * 20 ' Assign PersonId to fixed length text PersonId = txtPersonId.Text ' Assign PersonName to fixed length text PersonName = txtPersonName.Text ' Concatenate Id and Name myRequest = PersonId & PersonName ' Queue the request frmServer.List1.AddItem myRequest End Sub

Bn frmServer, c mi 3 giy n s Remove Item trn ht ( tc l Index=0) trong List1 v x l Item y. Trong bi ny ta ch Remove Item 0 ri Add n vo List2.
Private Sub Timer1_Timer() Dim Item If List1.ListCount > 0 Then ' Look at the item at the head of the queue Item = List1.List(0) ' Process Item - just add it to List2 here List2.AddItem Item ' Remove item from queue List1.RemoveItem 0 End If End Sub

Bn c th download source code ca program QueueServer.zip ny chy th. CheckBox Listbox Nu bn chn value ca Property Style ca Listbox l CheckBox thay v Standard th mi items trong Listbox s c mt hp vung pha trc User c th chn lc chy program. Hp vung ca item no c checked (nh du) th Item y c Selected. Gi s ta c mt Listbox List1 vi Style Checkbox v c nhiu Items mua trong siu th. Khi chy progarm user chn mt s items ri click nt Process, program s hin th cc item c chn. Listing ca Sub CmdProcess_Click nh sau:
Private Sub CmdProcess_Click() Dim Mess As String ' get out if there's nothing in the list If List1.ListCount = 0 Then Exit Sub ' Iterate through every item of the checkBox Listbox For i = 0 To List1.ListCount - 1 ' If item is selected then include it in the shopping list If List1.Selected(i) Then ' Append Item and a Carriage Return-LineFeed Mess = Mess & List1.List(i) & vbCrLf End If Next ' Display shopping list MsgBox Mess, vbInformation, "Selected Shopping Items" End Sub

Listbox vi nhiu ct Listbox c mt Property gi l Columns. Bnh thng, Property Columns c gi tr 0. Nhng nu bn cho n bng 3 chng hn, th Listbox s c gng hin th 4 ct trong phm vi chiu ngang ca Listbox. Nu nh th vn khng hin th ht mi Items trong Listbox th s c mt Horizontal Scrollbar hin ra, v khi bn click n qua bn phi Listbox s cho hin th thm cc columns cn li.

Combobox
Combobox rt ging nh Listbox. N l tp hp ca mt Textbox nm pha trn User cho vo data v mt Listbox cha cc items m User c th la chn khi m n ra. Combo box cng c nhng methods nh Clear, AddItem v RemoveItem. Tuy nhin, Combobox khng c Property Selected, v khi User chn Item no th Item y c hin th trong Textbox pha trn.

Combobox c 3 styles. Drop-down Combo Style l thng dng nht. N cho User nhim hoc chn mt Item t List hoc nh data vo Textbox. Trong hnh di y User nh vo ch Elephant thay v chn t cc Items c sn.

Drop-down List bt buc User phi chn mt trong nhng Item nm trong List, ch khng c nh data mi vo Textbox. Ngay c trong lc chy program (at run-time) bn cng khng th Assign mt value vo property Text ca Combobox loi ny. Nhng bn c th lm cho Combobox hin th Item th 3 chng hn bng cch set property ListIndex ca Combo bng 2. Cc lin mng VB khc Nhng cu hi v Visual Basic Nhng mo vt ca Visual Basic Lp trnh vi VBA
c cung cp t Vivosoft

You might also like