繁体   English   中英

如何在多个列(VBA)中添加多个复选框

[英]How to add multiple checkboxes in multiple columns (VBA)

我有一个具有多个列的ListView。 更准确地说,ListView包含8列。 其中2个应填写复选框。

当前,只有第一列包含复选框。 定义如下:

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

有谁知道如何添加最后一列中的复选框?

编辑:

是否可以通过.Controls添加来实现此列?

ListViewListBox控件的扩展版本。 另请参见msdn上的ListBox控件

它们都显示行记录(ListView具有更高级的格式设置选项)。 但是,这意味着一条记录是一行。 因此,当您选择一项时,您将选择一行。

复选框的功能是允许用户标记其选择的记录的行。

因此,在行的前面,每行只有一个复选框。

考虑以下代码(这是Excel 2003 VBA,但可以帮助您):

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

您可以根据需要执行自定义ListBoxListView 您可以创建一个框架,并在其上放置Labels和CheckBoxes。 这是在我测试过的Excel2003中执行此操作的唯一方法。 ListBox对象没有Controls子级。

但这更像是一个数据网格,而不是一个ListBoxListView ,它实际上是记录(行)的列表。

更新:

我看到了您的更新,并且您真的想将CheckBox放在行的末尾。

如果只希望最后一行有一个复选框,则可以执行此自定义复选框。 同样,这是为ListBox编写的,因此如果需要,需要将其转换为ListView

仍然需要自定义处理,但是我花了一些时间,所以我做了这段代码。 看看是否喜欢:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM