Programming language Visual Basic 6 (Chapter VII - Using List Controls (continued))


Search trong Text File

We know that the ListBox can contain lots of text (of a maximum number 65535). I've used to display content of a text file in a Listbox. I used to display the ListBox Events (incidents) occur in real-time.Suppose we record all Events happen in real-time of a security system, we know who that is, into and out of doors, at what time. The Events have just been down to a text log file, newly added to a ListBox to always display the latest event at the end of the ListBox.
Once you have all Events in the ListBox, we can search (search) if a person has to go through the door of the building by any iterate through each row in the ListBox and a Text Pattern recognition of the  InStr function .
In this form, you type the name of a person into the TextBox and then click  Find  and then  Find Next  to highlight the Events in the ListBox shows at the person's name appears. While searching for a Text Pattern can allow both uppercase, lowercase and by covert all text to Uppercase before working with them.

This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (839x557)

Listing của 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 
   ' 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 
   ' Only get here if Not found
   MsgBox "Not found!" 
End Sub 
In this paper we have used a  DriveListBox  for User to select a disk drive, a  DirListBox  user to select a Folder / Directory and a  FileListBox  to display the names of the Files in a Folder.
All three together ListBoxes this link to tell us that eating rhythm changes when changing from Disk User Disk Drive Drive through another, or from the folder to another folder. The codes of conduct this very simple as follows:
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 
We can choose only the filenames that a certain extension (eg  log ) by the Property Pattern  of FileListBox value " *. log ".

Every time the user clicks on the name of a file, program content of the file will load itinto the right ListBox EventList.

After a selected number of rows then, the User can either print them out by clicking the button  Print or copy them to Clipboard by Click  Copy .

You can download the source code of this program to fully co

Dùng ItemData

Neu  Property List  ListBox được xem cua Nhu mot Text Array thi  ItemData  là mot Number Array, vaList1.ItemData (i) the  cap can DJI  List1.List (i) . Tuc Trong là chi List1.List (i) the thi Nhu Hien mat cua mot tam trước Bản thi List1.ItemData (i) được COI Nhu nam o mat cua sau tam Bản ay. Chi mot List item thay đổi vi tri Trong Listbox co vi su bien đổi Trong ListBox (thi du bi Items removed hay được cho inline VAO) thi cua ItemData List item Djo cũng theo DJI can no.

I see the following example. Into a  Sorted  Listbox name of the staff in the office. Immediately after an employee is called for in the Property Listbox  NewIndex  contains the location of the item to be added to it in the ListBox. I used to assign values NewIndex ItemData with employee ID number. When the user clicks on a name in the Listbox, program will display the number of employee names and employee ID. To test this example, you can Paste the code below into the Declaration of a Form that contains a Listbox and a Label. Remember to set the Sorted property to True List1.
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 

ListBox to use Queue

When to see a theater, we often have to stand lined up to buy tickets. The row is called  Queue . The purpose of the Queue is used for large number of people need a service that will be served in turn in order of who comes first will be addressed first. Queue of such principles is called  First-In-First-Out  (the first, the first one). Conversely, if people want to be served before we will have the most turbulent, and ultimately, no one can be resolved.

For example we have a Form named frmServer, which have a Listbox named List1. If there are many other Forms in the same program to a service by frmServer what, we'll Queue Add an item by the end of List1. In the Item that contains the details that will need to know to frmServer service.
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 
FrmServer party, every 3 seconds it will above all Remove Item (ie index = 0) in Item List1 and handling it.In this paper we only Remove Item 0 then add it into 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 

You can download the source code of this program to test

CheckBox Listbox

If you select the value of a CheckBox Property Style Listbox instead of Standard, allitems in the Listbox will have a square box in front to the User can select at run program.Square box of items that are checked (tick), then he is "Selected Item.

Suppose we have a Checkbox Listbox List1 with Style and more Items to buy in the supermarket. When users choose to run progarm some items and then click  Process , program will display the selected item.

Listing của 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 
   ' Display shopping list
   MsgBox Mess, vbInformation, "Selected Shopping Items" 
End Sub 

Listbox with multiple columns

Listbox has a Property called Columns. Normally, the Property Columns with a value of 0. But if you give it by 3 for example, the Listbox will try to show four columns within the width of the Listbox. If so I still do not fully display all Items in a Listbox Horizontal Scrollbar will appear, and when you click on it to the right Listbox will display more columns remaining.
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (513x224)


Listbox Combobox very similar. It is a combination of a textbox at the top to the User for the data and a Listbox that contains the items that user can choose to open it. Combo box there are methods such as Clear, AddItem and RemoveItem . However, no Combobox  Selected Property because the user does select the item that is displayed in the Item Text box above.

Combobox with 3 styles.  Drop-down Combo  Style is the most common. It elective or choose a User Item List or reviews data from the textbox. In the picture below User type in text instead of selecting from the Elephant Items available.
Drop-down List  User required to select one item in the List, do not type new data into the textbox. Even while running program (at run-time) you can not Assign a value to the Text property of the Combobox this category. But you can make Combobox display such items by the 3rd set of Combo ListIndex property to 2.


Post a Comment