vb6 16 (8)

Download Vb6 16 (8)

Post on 15-Jul-2015

71 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

Chng By - Dng List Controls

Chng By - Dng List ControlsC hai loi List controls dng trong VB6. l Listbox v Combobox. C hai u display mt s hng ta c th la chn. Listbox chim mt khung ch nht, nu chiu ngang nh th c khi khng display y mt hng, nu chiu di khng display tt c mi hng th Listbox t ng cho ta mt vertical scroll bar cho bit cn c nhiu hng b che v ta c th xem cc hng y bng cch dng vertical scroll bar. Combobox thng thng ch display mt hng, nhng ta c th chn display bt c hng no khc. Combobox ging nh mt tp hp ca mt Textbox nm pha trn v mt Listbox nm pha di.

Listbox c rt nhiu cng dng v n rt uyn chuyn. Trong chng ny ta s hc qua cc p dng sau ca Listbox:

Display nhiu s la chn User selects bng cch click hay drag-drop

Nhng cch dng Property Sorted

Cch dng Multiselect

Dng display Events

Dng Search hay process text

Cch dng Itemdata song song vi cc Items ca List

Dng lm Queue

Listbox Display nhiu s la chn Ta hy bt u vit mt chng trnh gm c mt Listbox tn lstNames nm trong mt Form. Trong lstNames ta nh vo tn ca by ngi, mi ln xung hng nh nh Ctrl-Enter, thay v ch Enter, nu khng VB6 tng ta nh xong nn close property List. Cc tn ny l nhng hng s hin ra trong Listbox khi ta bt u chy program.

Ngoi lstNames ta cho thm mt Label vi Caption STUDENTS trang hong, v mt Label khc tn lblName. Mi khi User click ln hng tn no ta mun display hng tn y trong lblName. Sau cng ta cho vo mt CommandButton tn CmdExit cho User phng tin Stop ci program. Ta s c chng trnh nh sau:

Private Sub lstNames_Click()

' Assign the selected line of Listbox lstNames to Caption of Label lblName lblName.Caption = lstNames.List(lstNames.ListIndex) ' or = lstNames.textEnd Sub

Private Sub CmdExit_Click()

End

End Sub

Gi s ta click vo tn John Smith trn Listbox, ta s thy tn y cng uc display trong Label lblName.

Trong th d ny, Listbox lstNames c 7 hng ( Items). Con s Items ny l Property ListCount ca Listbox. Cc Items ca Listbox c m t 0 n ListCount-1. Trong trng hp ny l t 0 n 6. Khi User click ln mt hng, Listbox s generate Event lstNames_Click. Lc by gi ta c th bit c User va mi Click hng no bng cch hi Property ListIndex ca lstNames, n s c value t 0 n ListCount-1. Lc program mi chy, cha ai Click ln Item no ca Listbox th ListIndex = -1.Nhng Items trong Listbox c xem nh mt Array ca String. Array ny c gi l List. Do , ta ni n Item th nht ca Listbox lstNames bng cch vit lstNames.List(0) , v tng t nh vy, Item cui cng l lstNames.List( lstNames.ListCount-1). Ta c th ni n item va c Clicked bng hai cch: hoc l lstNames.List(lstNames.ListIndex), hoc l lstNames.text.

Save content ca Listbox By gi lu tr content ca lstNames, ta thm mt CommandButton tn CmdSave. Ta s vit code khi User click nt CmdSave program s m mt Output text file v vit mi items ca lstNames vo :

Private Sub CmdSave_Click()

Dim i, FileName, FileNumber

' Obtain Folder where this program's EXE file resides FileName = App.Path

' Make sure FileName ends with a backslash If Right(FileName, 1) "\" Then FileName = FileName & "\"

FileName = FileName & "MyList.txt" ' name output text file MyList.txt ' Obtain an available filenumber from the operating system FileNumber = FreeFile

' Open the FileName as an output file , using FileNumber as FileHandle Open FileName For Output As FileNumber

' Now iterate through each item of lstNames For i = 0 To lstNames.ListCount - 1

' Write the List item to file. Make sure you use symbol # in front of FileNumber Print #FileNumber, lstNames.List(i)

Next

Close FileNumber ' Close the output fileEnd Sub

App l mt Object c bit i din cho chnh ci program ang chy. y ta dng Property Path bit lc program ang chy th execute module EXE ca n nm u. L do l thng thng ta cc files lin h cn thit cho program ln qun hoc ngay trong folder ca program hay trong mt subfolder, chng hn nh data, logs, .v.v..App cn c mt s Properties khc cng rt hu dng nh PrevInstance, Title, Revision ..v.v.Nu mi started mt program m thy App.PrevInstance = True th lc by gi cng c mt copy khc ca program ang chy. Nu cn ta End program ny trnh chy 2 copies ca program cng mt lc. App.Title v App.Revision cho ta tin tc v Title v Revision ca program ang chy. vit ra mt Text file ta cn phi Open n trong mode Output v tuyn b t ry tr i s dng mt con s (FileNumber) i din ci File thay v dng chnh FileName. trnh dng mt FileNumber hin hu, tt nht ta hi xin Operating System cung cp cho mnh mt con s cha ai dng bng cch gi Function FreeFile. Con s FileNumber ny cn uc gi l FileHandle (Handle l tay cm). Sau khi ta Close FileNumber con s ny tr nn FREE v Operating System s c th dng n li.Do bn phi trnh gi FreeFile lin tip hai ln, v OS s cho bn cng mt con s. Tc l, sau khi gi FreeFile phi dng n ngay bng cch Open mt File ri mi gi FreeFile ln k c mt con s khc. cch dng ch Input, Output cho files l relative (tng i) vi v tr ca program (n nm trong memory ca computer). Do t trong memory vit ra hard disk th ni l Output. Ngc li c t mt Text file nm trn hard disk vo memory cho program ta th gi l Input.

Load mt Text file vo Listbox Trong bi ny, thay v nh cc Items ca Listbox vo Property List ca lstNames ta c th populate (lm y) lstNames bng cch c cc Items t mt Text file. Ta th thm mt CommandButton tn CmdLoad. Ta s vit code khi User click nt CmdLoad program s m mt Input text file v c tng hng b vo lstNames:

Private Sub CmdLoad_Click()

Dim i, FileName, FileNumber, anItem

' Obtain Folder where this program's EXE file resides FileName = App.Path

' Make sure FileName ends with a backslash If Right(FileName, 1) "\" Then FileName = FileName & "\"

FileName = FileName & "MyList.txt"

' Obtain an available filenumber from the operating system FileNumber = FreeFile

' Open the FileName as an input file , using FileNumber as FileHandle Open FileName For Input As FileNumber

lstNames.Clear ' Clear the Listbox first ' Now read each line until reaching End-Of-File, i.e. no more data Do While NOT EOF(FileNumber)

Line Input #FileNumber, anItem ' Read a line from the Text file into variable anItem lstNames.AddItem anItem ' Add this item to the bottom of lstNames Loop

Close FileNumber ' Close the input fileEnd Sub

c t mt Text file ta cn phi Open n trong mode Input.Trc khi populate lstNames ta cn phi delete tt c mi items c sn bn trong. thc hin vic ta dng method Clear ca Listbox.Sau ta dng method AddItem cho thm tng hng vo trong Listbox. By default, nu ta khng ni nht vo ch hng no th AddItem nht Item mi vo di cht ca Listbox.Nu mun nht hng mi vo ngay trc item th 5 (ListIndex = 4), ta vit:

lstNames.AddItem newItemString, 4 ' newItemString contains "Ross Close", for example' To insert a new Item at the beginning of the Listbox, write:lstNames.AddItem newItemString, 0

Nh l mi ln bn Add mt Item vo Listbox th ListCount ca Listbox increment by 1.Mun delete mt item t Listbox ta dng method RemoveItem, th d nh mun delete item th ba (ListIndex=2) ca lstNames, ta vit:

lstNames.RemoveItem 2

Mi ln bn RemoveItem t Listbox the ListCount ca Listbox decrement by 1. Do nu bn dng ci Test da vo ListCount ca mt ListBox nhy ra khi mt Loop th phi coi chng trnh lm cho value ListCount thay i trong Loop v AddItem hay RemoveItem.Ta c tng hng ca mt Text file bng cch dng Line Input #FileNumber. Khi c n cui File, system d cho ta value EOF(FileNumber) = True. Ta dng value y cho program nhy ra khi While.. Loop.Cu Do While NOT EOF(FileNumber) c ngha Trong khi cha n End-Of-File ca Text File i din bi FileNumber th c t hng v b vo Listbox. Ging nh "Trong khi cha tr ht n nh v th phi tip tc r".

Drag-Drop Ta hc qua Click Event ca Listbox. By gi dng Drag-Drop cho Listbox bn hy t 2 Labels mi ln Form. Ci th nht tn g cng c nhng c Caption l Room A. Hy gi Label th hai l lblRoom v cho Property BorderStyle ca n bng Fixed Single. K n select c hai Labels (Click a Label then hold down key Ctrl while clicking the second Label) ri click copy v paste ln Form. VB6 s cho bn Array ca hai lblRoom labels. cho lstNames mt DragIcon, bn click lstNames, click Property DragIcon pop-up mt dialog cho bn chn mt dragdrop icon t folder C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Dragdrop, chng hn nh DRAG2PG.ICO:

Ta s dng Event MouseDown ca lstNames pop-up DragIcon hnh 2 trang giy cho User Drag n qua bn phi ri b xung ln mt trong hai lblRoom. Khi DragIcon ri ln lblRoom, lblRoom s generate Event DragDrop. Ta s dng Event DragDrop ny assign property Text ca Source (tc l lstNames, ci control t n pht xut Drag action) vo Property Caption ca lblRoom. Lu v y ta dng cng mt tn cho c hai lblRoom nn ch cn vit code mt ch handle Event DragDrop.

Private Sub lstNames_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

' Start Pop-up DragIcon and start Drag action lstNames.Drag

End Sub

Private Sub lblRoom_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)

' Assign Property Text of Source (i.e. lstNames) to Label's Caption lblRoom(Index).Caption = Source.Text

End Sub

Kt qu sau khi Drag hai tn t Listbox qua Labels l nh sau:

Dng Property Sorted

Trong th d trn ta c th quyt nh v tr ca mt Item mi khi ta nht n vo Listbox. i khi ta mun cc Items ca Listbox c t ng sp theo th t Alphabet. Bn c th set Property Sorted = True thc hin chuyn ny. C mt gii hn l bn phi cho Property Sorted mt value (True hay False) trong lc design, ch trong khi chy program bn khng th lm cho Property Sorted ca Listbox thay i.Gi d ta mun sort cc Items ca mt Listbox khi cn. Vy th ta lm sao? Gii php rt n gin. Bn to