yuro
yuro

Reputation: 2209

How to add multiple checkboxes in multiple columns (VBA)

I have a ListView with multiple columns. More precisely, the ListView contains 8 columns. 2 of them should be filled with checkboxes.

Currently only the first column contains checkboxes. It is defined as follows:

While Not rs.EOF
   //first column with checkboxes
   ListViewCustomer.ListItems.Add , , rs("Id")
   ListViewCustomer.ListItems(ListViewCustomer.ListItems.Count).tag = rs("Status")
   //second column etc.     
   ListViewCustomer.ListItems(ListViewCustomer.ListItems.Count).ListSubItems.Add , , rs("name")
   ....
   //Here is the second column, which doesn't display the checkboxes
   ListViewCustomer.ListItems(ListViewCustomer.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs("date_from")), "", rs("date_from"))
   ListViewCustomer.ListItems(ListViewCustomer.ListItems.Count).tag = rs("Status2")
Wend

Do anyone have an idea how to add the checkboxes in the last column?

EDIT:

Is it possible to realize this column with adding via .Controls?

Upvotes: 0

Views: 3179

Answers (1)

ib11
ib11

Reputation: 2568

A ListView is a more expanded version of the ListBox control. See ListBox control on msdn as well.

They both display records of rows (the ListView has more advanced formatting options). This however means that a record is a row. Therefore you select a row when you select one of the items.

The function of the checkbox is to allow the user to mark the row(s) that is the records(s) he selects.

Thus there is only one checkbox per row, at the front of the row.

Consider this code (this is Excel 2003 VBA, but gives you the idea):

Private Sub UserForm_Initialize()

    Dim MyArray(6, 8)
    'Array containing column values for ListBox.

    ListBox1.ColumnCount = 8
    ListBox1.MultiSelect = fmMultiSelectExtended

    'Load integer values MyArray
    For i = 0 To 5
        MyArray(i, 0) = i
        For j = 1 To 7
            MyArray(i, j) = Rnd
        Next j
    Next i

    'Load ListBox1
    ListBox1.List() = MyArray

End Sub

You could do a custom ListBox or ListView if you really want. You could create a frame and put Labels and CheckBoxes on it. This is the only way to do this in Excel2003 where I tested. The ListBox object has no Controls child.

But this is more like a datagrid and not really a ListBox or ListView which by definition are a listing of records (rows).

Update:

I saw your update and that you really want to place the CheckBox at the end of the row.

If you only want one checkbox at the last row, you could do this custom checkbox. Again this is written for the ListBox, so need to convert it to your ListView if you want to.

Still requires a custom handling, but I had some time, so I did this code. See if you like it:

Private Sub ListBox1_Change()

For i = 0 To ListBox1.ListCount - 1
    ListBox1.List(i, 3) = ChrW(&H2610)
Next i

ListBox1.List(ListBox1.ListIndex, 3) = ChrW(&H2611)

End Sub

Private Sub UserForm_Initialize()

Dim MyArray(5, 3)
'Array containing column values for ListBox.

ListBox1.ColumnCount = 4
ListBox1.MultiSelect = 0
ListBox1.ListStyle = 0


    'Load integer values MyArray
    For i = 0 To 5
        MyArray(i, 0) = i
        For j = 1 To 2
            MyArray(i, j) = Rnd
        Next j
        MyArray(i, 3) = ChrW(&H2610)
    Next i

    'Load ListBox1
    ListBox1.List() = MyArray

End Sub

Upvotes: 1

Related Questions