LẬP TRÌNH TRỰC QUAN - PHẦN II VISUAL BASIC - BÀI 15 doc

12 308 0
LẬP TRÌNH TRỰC QUAN - PHẦN II VISUAL BASIC - BÀI 15 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lập trình trực quan BÀI 15 DÙNG LIST CONTROLS Có hai loại List controls dùng VB6 Ðó Listbox Combobox Cả hai hiển thị số dòng để ta lựa chọn Listbox chiếm khung chữ nhật, chiều ngang nhỏ có khơng hiển thị đầy đủ dịng, chiều dài khơng đủ để hiển thị tất dịng Listbox tự động cho ta vertical scroll bar biết cịn có nhiều dịng bị che ta xem dòng cách dùng vertical scroll bar Combobox thường hiển thị dòng, ta chọn hiển thị dịng khác Combobox giống tập hợp Textbox nằm phía Listbox nằm phía Listbox có nhiều cơng dụng uyển chuyển sử dụng Trong xem xét ứng dụng sau Listbox : - Hiển thị nhiều lựa chọn để người sử dụng chọn cách click hay drag-drop - Những cách dùng Property Sorted - Cách dùng Multiselect - Dùng để hiển thị Events - Dùng để Search hay xử lý text - Cách dùng Itemdata song song với Items danh sách - Dùng làm Queue 115 Lập trình trực quan 15.1 Listbox 15.1.1 Hiển thị nhiều lựa chọn Ta bắt đầu viết chương trình gồm có Listbox tên lstNames nằm Form Trong lstNames ta đánh vào tên bảy người, lần xuống dịng nhớ đánh CtrlEnter, thay Enter, khơng VB6 ngầm hiểu đánh xong nên tự đóng cửa sổ Property List Các tên dòng Listbox ta bắt đầu chạy chương trình Ngồi lstNames ta cho thêm Label với Caption STUDENTS để trang trí, Label khác tên lblName Mỗi người sử dụng click lên dòng tên ta muốn hiển thị dòng tên lblName Sau ta cho vào CommandButton tên CmdExit dùng dừng chương trình Ta có chương trình sau: Private Sub lstNames_Click() lblName.Caption = lstNames.List(lstNames.ListIndex) End Sub Private Sub CmdExit_Click() End End Sub 116 Lập trình trực quan Giả sử ta click vào tên John Smith Listbox, ta thấy tên đuợc hiển thị Label lblName Trong ví dụ này, Listbox lstNames có dòng (Items) Con số Items Property ListCount Listbox Các Items Listbox đếm từ đến ListCount-1 Trong trường hợp từ đến Khi người sử dụng click lên dòng, Listbox generate Event lstNames_Click Lúc ta biết người sử dụng vừa Click dòng cách hỏi Property ListIndex lstNames, có value từ đến ListCount-1 Lúc chương trình chạy, chưa Click lên Item Listbox ListIndex = -1 Những Items Listbox xem mảng xâu ký tự Array gọi List Do đó, ta nói đến Item thứ Listbox lstNames cách viết lstNames.List(0) , tương tự vậy, Item cuối lstNames.List( lstNames.ListCount-1) Ta nói đến item vừa Clicked hai cách: - lstNames.List(lstNames.ListIndex) - lstNames.text 15.1.2 Save content Listbox Bây để lưu trữ nội dung lstNames, ta thêm CommandButton tên CmdSave Ta viết code để người sử dụng click nút CmdSave chương trình mở Output text file viết items lstNames vào đó: 117 Lập trình trực quan Private Sub CmdSave_Click() Dim i, FileName, FileNumber FileName = App.Path ' Make sure FileName ends with a backslash If Right(FileName, 1) "\" Then FileName = FileName & "\" FileName = FileName&"MyList.txt" 'output text file MyList.txt ' Obtain an available filenumber from the operating system FileNumber = FreeFile ' Open the FileName as an output file Open FileName For Output As FileNumber ' Now iterate through each item of lstNames For i = To lstNames.ListCount - ' Write the List item to file Print #FileNumber, lstNames.List(i) Next Close FileNumber ' Close the output file End Sub App Object đặc biệt đại diện cho chương trình chạy Ở ta dùng Property Path để biết lúc chương trình chạy thực thi module EXE nằm đâu Lý ta thường để files liên hệ cần thiết cho chương trình lẩn quẩn folder chương trình hay subfolder, chẳng hạn data, logs, v.v App cịn có số Properties khác hữu dụng PrevInstance, Title, Revision v.v Nếu khởi động chương trình mà thấy App.PrevInstance = True lúc có copy khác chương trình chạy Nếu cần ta End program để tránh chạy chương trình lúc App.Title App.Revision cho ta tin tức Title Revision chương trình chạy Ðể viết Text file ta cần phải Open mode Output khai báo từ trở dùng số (FileNumber) để đại diện tập tin thay dùng FileName Ðể tránh dùng FileNumber hữu, tốt ta hỏi xin hệ điều hành cung cấp cho số chưa dùng cách gọi Function FreeFile Con số FileNumber đuợc gọi FileHandle (Handle tay cầm) Sau ta Close FileNumber số trở nên FREE hệ điều hành dùng lại 118 Lập trình trực quan Do phải tránh gọi FreeFile liên tiếp hai lần, OS cho số Tức là, sau gọi FreeFile phải dùng cách Open File gọi FreeFile lần kế để có số khác Ðể ý cách dùng chữ Input, Output cho files relative (tương đối) với vị trí chương trình (nó nằm memory computer) Do từ memory viết đĩa cứng gọi Output Ngược lại đọc từ Text file nằm hard disk vào memory cho chương trình ta gọi Input 15.1.3 Load Text file vào Listbox Trong này, thay đánh Items Listbox vào Property List lstNames ta populate (làm đầy) lstNames cách đọc Items từ Text file Ta thử thêm CommandButton tên CmdLoad Ta viết code để người sử dụng click nút CmdLoad chương trình mở Input text file đọc dòng để bỏ vào 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 Open FileName For Input As FileNumber lstNames.Clear ' Clear the Listbox first ' Now read each line until reaching End-Of-File Do While NOT EOF(FileNumber) Line Input #FileNumber, anItem ' Read a line from the file lstNames.AddItem anItem ' Add this item to the lstNames Loop Close FileNumber ' Close the input file End Sub Ðể đọc từ Text file ta cần phải Open mode Input 119 Lập trình trực quan Trước populate lstNames ta cần phải xóa tất items có sẵn bên Ðể thực việc ta dùng method Clear Listbox Sau ta dùng method AddItem thêm dòng vào Listbox By default, ta khơng nói nhét vào chỗ dịng AddItem nhét Item vào chót Listbox Nếu muốn nhét dòng vào trước item thứ (ListIndex = 4), ta viết: stNames.AddItem newItemString, ' newItemString contains ' To insert a new Item at the beginning of the Listbox, write: lstNames.AddItem newItemString, Nhớ lần Add Item vào Listbox ListCount Listbox tăng Muốn xóa item từ Listbox ta dùng method RemoveItem, ví dụ muốn xóa item thứ ba (ListIndex=2) lstNames, ta viết: lstNames.RemoveItem Mỗi lần RemoveItem từ Listbox the ListCount Listbox giảm đơn vị Do dùng Test dựa vào ListCount ListBox để nhảy khỏi Loop phải coi chừng tránh làm cho value ListCount thay đổi Loop AddItem hay RemoveItem Ta đọc dòng Text file cách dùng Line Input #FileNumber Khi đọc đến cuối File, system dẽ cho ta value EOF(FileNumber) = True Ta dùng value chương trình nhảy khỏi While Loop Câu Do While NOT EOF(FileNumber) có nghĩa Trong chưa đến End-Of-File Text File đại diện FileNumber đọc dịng bỏ vào Listbox 15.2 Drag-Drop Ta xem qua Click Event Listbox Bây để dùng Drag-Drop cho Listbox đặt Labels lên Form Cái thứ tên có Caption Room A Hãy gọi Label thứ hai lblRoom cho Property BorderStyle Fixed Single Kế đến select hai Labels (Click a Label then hold down key Ctrl while clicking the second Label) click copy paste lên Form VB6 cho Array củahailblRoom labels 120 Lập trình trực quan Ðể cho lstNames DragIcon, click lstNames, click Property DragIcon để popup dialog cho chọn dragdrop icon từ folder C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Dragdrop, chẳng hạn DRAG2PG.ICO: Ta dùng Event MouseDown lstNames để pop-up DragIcon hình trang giấy cho UserDrag qua bên phải bỏ xuống lên hai lblRoom Khi DragIcon rơi lên lblRoom, lblRoom generate Event DragDrop Ta dùng Event DragDrop để assignproperty Text Source (tức lstNames, mục control từ phát xuất Drag action) vào Property Caption lblRoom Lưu ý ta dùng tên cho hai lblRoom nên cần viết code 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 Caption lblRoom(Index).Caption = Source.Text End Sub Kết sau Drag hai tên từ Listbox qua Labels sau: 121 Lập trình trực quan 15.3 Dùng Property Sorted Trong ví dụ ta định vị trí Item ta nhét vào Listbox Ðơi ta muốn Items Listbox tự động theo thứ tự Alphabet Chúng ta set Property Sorted = True để thực chuyện Có giới hạn phải cho Property Sorted value (True hay False) lúc design, chạy chương trình làm cho Property Sorted Listbox thay đổi Giả dụ ta muốn sort Items Listbox cần Vậy ta làm sao? Giải pháp đơn giản Chúng ta tạo Listbox tên lstTemp chẳng hạn Cho Property Visible= False (để khơng thấy nó) Property Sorted=True Khi cần sort lstNames chẳng hạn, ta copy content lstNames bỏ vào lstTemp, đoạn Clear lstNames copy content (đã sorted) lstTemp trở lại lstNames Lưu ý ta AddItem vào Listbox với Property Sorted=True, xác định nhét Item vào trước dịng nào, vị trí Items Listbox định sort Items Ta cho thêm vào Form CommandButton tên CmdSort viết code cho Event Click sau: Private Sub CmdSort_Click() Dim i lstTemp.Clear ' Clear temporary Listbox ' Iterate though every item of lstNames For i = To lstNames.ListCount - ' Add the lstNames item to lstTemp 122 Lập trình trực quan lstTemp.AddItem lstNames.List(i) Next lstNames.Clear ' Clear lstNames ' Iterate though every item of lstTemp For i = To lstTemp.ListCount - ' Add the lstTemp item to lstNames lstNames.AddItem lstTemp.List(i) Next lstTemp.Clear ' Tidy up - clear temporary Listbox End Sub Nhân tiện, ta muốn có option để sort tên theo FirstName hay Surname Việc rắc rối chút, nguyên tắc dùng sorted Listbox vơ hình tên lstTemp Chúng ta đặt lên phía lstName hai Labels tên lblFirstName lblSurName cho chúng Caption "FirstName" "SurName" Từ ta Load file "MyList.txt" vào lstNames cách Click button CmdLoad không Edit Property List lstNames để enter Items lúc design Ngoài ta dùng dấu phẩy (,) để tách FirstName khỏi SurName tên chứa file MyList.txt Content file MyList.txt trở thành sau: Peter,Jones Kevin,White Sue,Rose John,Smith Trevor,Kennedy Alan,Wright Ron, Bruno Ta sửa code Sub CmdLoad_Click lại để nhét tên vào lstNames, FirstName SurName thứ chiếm 10 characters Ðể chữ Items lstNames dòng ngắn ta đổi Font lstNames Courier New Courier New loại Font mà chiều ngang tất chữ nhau, hầu hết Fonts khác Arial, Times Roman v.v Proportional Spacing, có nghĩa độ rộng ký tự khác Listing Sub CmdLoad_Click trở thành sau: 123 Lập trình trực quan Private Sub CmdLoad_Click() Dim i, Pos Dim FileName, FileNumber, anItem Dim sFirstName As String*10 ' fixed length string of 10 chars Dim sSurName As String * 10 ' fixed length string of 10 chars ' 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 Open FileName For Input As FileNumber lstNames.Clear ' Clear the Listbox first ' Now read each line until reaching End-Of-File Do While Not EOF(FileNumber) Line Input #FileNumber, anItem ' Read a line from the file ' Now separate FirstName from SurName Pos = InStr(anItem, ",") ' Locate the comma "," ' The part before "," is FirstName sFirstName = Left(anItem, Pos - 1) sFirstName = Trim(sFirstName) ' Trim off any blank spaces ' The part after "," is SurName sSurName = Mid(anItem, Pos + 1) sSurName = Trim(sSurName) ' Trim off any blank spaces lstNames.AddItem sFirstName & sSurName ' Add this item to the bottom of lstNames Loop Close FileNumber ' Close the input file End Sub Vì FirstName nằm bên trái Item nên sort theo FirstName giống sort Item Việc ta làm Sub CmdSort_Click rồi, người sử dụng click Label lblFirstName ta cần gọi CmdSort_Click sau: Private Sub lblFirstName_Click() CmdSort_Click End Sub 124 Lập trình trực quan Ðể sort theo SurName ta cần phải tạm thời để SurName qua bên trái Item trước bỏ vào lstTemp Ta thực chuyện cách hốn chuyển vị trí FirstName SurName Item trước bỏ vào lstTemp Sau đó, copy Items từ lstTemp để đặt vào lại lstNames ta lại nhớ hoán chuyển FirstName SurName để chúng nằm lại vị trí Code để sort tên theo SurName giống CmdSort_Add sửa đổi chút sau: Private Sub lblSurName_Click() Dim i, anItem Dim sFirstName As String*10 'fixed length string of 10 chars Dim sSurName As String * 10 ' fixed length string of 10 chars lstTemp.Clear ' Clear temporary Listbox ' Iterate though every item of lstNames For i = To lstNames.ListCount - anItem = lstNames.List(i) ' Identify FistName and SurName sFirstName = Left(anItem, 10) sSurName = Mid(anItem, 11) ' Swap FirstName/SurName positions before adding to lstTemp lstTemp.AddItem sSurName & sFirstName Next lstNames.Clear ' Clear lstNames ' Iterate though every item of lstTemp For i = To lstTemp.ListCount - anItem = lstTemp.List(i) sSurName = Left(anItem, 10) ' SurName now is on the left sFirstName = Mid(anItem, 11) ' Add FirstName/SurName in correct positions to lstNames lstNames.AddItem sFirstName & sSurName Next lstTemp.Clear ' Tidy up - clear temporary Listbox End Sub Các Items lstNames sorted theo SurName sau: 125 Lập trình trực quan Nhân tiện ta sửa Sub CmdSave_Click để Save Items theo sorted order cần: Private Sub CmdSave_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 & "\" ' Call Output filename "MyList.txt" FileName = FileName & "MyList.txt" ' Obtain an available filenumber from the operating system FileNumber = FreeFile ' Open the FileName as an output file, using FileNumber Open FileName For Output As FileNumber ' Now iterate through each item of lstNames For i = To lstNames.ListCount - anItem = lstNames.List(i) anItem=Trim(Left(anItem, 10)) & "," & Trim(Mid(anItem, 11)) ' Write the List item to file Make sure you use symbol # in front of FileNumber Print #FileNumber, anItem Next Close FileNumber ' Close the output file End Sub 126 .. .Lập trình trực quan 15. 1 Listbox 15. 1.1 Hiển thị nhiều lựa chọn Ta bắt đầu viết chương trình gồm có Listbox tên lstNames nằm Form Trong lstNames... chương trình Ta có chương trình sau: Private Sub lstNames_Click() lblName.Caption = lstNames.List(lstNames.ListIndex) End Sub Private Sub CmdExit_Click() End End Sub 116 Lập trình trực quan Giả... CmdSave Ta viết code để người sử dụng click nút CmdSave chương trình mở Output text file viết items lstNames vào đó: 117 Lập trình trực quan Private Sub CmdSave_Click() Dim i, FileName, FileNumber

Ngày đăng: 24/07/2014, 16:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan