Ghosted, MultiSelect Properties Example

This example populates a ListView control with the contents of the Authors table from the Biblio.mdb database, and lets you use OptionButton controls to set MultiSelect property options. You can select any item, or hold down the SHIFT Key and select multiple items. Clicking on the CommandButton sets the Ghosted property of the selected items to True. To try the example, place a control array of two OptionButton controls, a ListView control, an ImageList control, and a CommandButton control on a form and paste the code into the form's Declarations section.

Note   The example will not run unless you add a reference to the Microsoft DAO 3.0 Object Library by using the References command on the Tools menu. Run the example, select a MultiSelect option by clicking an OptionButton, click on items to select them and click the CommandButton to ghost them.

Private Sub Command1_Click()
   Dim x As Object
   Dim i As Integer
   ' Ghost selected ListItem.
If ListView1.SelectedItem Is Nothing Then Exit Sub
   For i = 1 To ListView1.ListItems.Count
      If ListView1.ListItems(i).Selected = True Then
         ListView1.ListItems(i).Ghosted = True
      End If
   Next i
End Sub

Private Sub Form_Load()
   ' Create an object variable for the ColumnHeader object.
   Dim clmX As ColumnHeader
   ' Add ColumnHeaders. The width of the columns is the width
   ' of the control divided by the number of ColumnHeader objects.
   Set clmX = ListView1.ColumnHeaders. _
   Add(, , "Company", ListView1.Width / 3)
   Set clmX = ListView1.ColumnHeaders. _
   Add(, , "Address", ListView1.Width / 3)
   Set clmX = ListView1.ColumnHeaders. _
   Add(, , "Phone", ListView1.Width / 3)
   
   ' Label OptionButton controls with MultiSelect options.
      Option1(0).Caption = "No MultiSelect"
      Option1(1).Caption = "MultiSelect"
      ListView1.MultiSelect = 1 ' Set MultiSelect to True

   ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
   ListView1.View = lvwReport   ' Set View property to Report.
   ' Add one image to ImageList control.
   Dim imgX As ListImage
   Set imgX = ImageList1.ListImages. _
   Add(, , LoadPicture("icons\mail\mail01a.ico"))
   ListView1.Icons = ImageList1

   ' Create object variables for the Data Access objects.
   Dim myDb As Database, myRs As Recordset
   ' Set the Database to the BIBLIO.MDB database.
   Set myDb = DBEngine.Workspaces(0).OpenDatabase("BIBLIO.MDB")
   ' Set the recordset to the Publishers table.
   Set myRs = myDb.OpenRecordset("Publishers", dbOpenDynaset)
   
   ' Create a variable to add ListItem objects.
   Dim itmX As ListItem

   ' While the record is not the last record, add a ListItem object.
   ' Use the Name field for the ListItem object's text.
   ' Use the Address field for the ListItem object's SubItem(1).
   ' Use the Phone field for the ListItem object's SubItem(2).

   While Not myRs.EOF
      Set itmX = ListView1.ListItems.Add(, , CStr(myRs!Name))
      itmX.Icon = 1  ' Set icon to the ImageList icon.

      ' If the Address field is not Null, set SubItem 1 to the field.
      If Not IsNull(myRs!Address) Then
         itmX.SubItems(1) = CStr(myRs!Address)  ' Address field.
      End If

      ' If the Phone field is not Null, set SubItem 2 to the field.
      If Not IsNull(myRs!Telephone) Then
         itmX.SubItems(2) = myRs!Telephone  ' Phone field.
      End If

      myRs.MoveNext  ' Move to next record.
   Wend

   ListView1.View = lvwIcon  ' Show Icons view.
   Command1.Caption = "Cut"  ' Set caption of the CommandButton.
   ' Add a caption to the form.
   Me.Caption = "Select any item(s) and click 'Cut'."
End Sub

Private Sub Option1_Click(Index as Integer)
   ListView1.MultiSelect = Index
End Sub