|
楼主 |
发表于 2019-3-17 18:13
|
显示全部楼层
动态添加控件,滚动条不好实现,下面程序根据字段数自动调整高度,以多行显示:
Private Sub UserForm_Activate()
Dim l%, m%, n%, i%, h%
CheckBox100.Value = True
l = WorksheetFunction.RoundUp(d.Count / 2, 0)
If d.Count > 20 Then '新加
h = WorksheetFunction.RoundUp(d.Count / 10, 0)
l = WorksheetFunction.RoundUp(d.Count / h, 0)
Me.Height = Me.Height + (h - 2) * 30
Frame1.Top = Frame1.Top + (h - 2) * 30
ListView1.Height = ListView1.Height + (h - 2) * 30
End If
For i = 0 To d.Count - 1 '逐个字段添加动态复选框
m = i Mod l '求余数,决定控件的横坐标
n = Int(i / l) * 24 '控件的纵坐标增量
With Controls.Add("Forms.CheckBox.1", "CheckBox" & i + 2, True) '添加动态复选框
.Move 250 + 60 * (m + 1), 1 + n '前面是X(距左边的距离),后面是Y(距上边的距离)
.Caption = d.Keys(i) '显示字段名
.Width = 60 '宽度
.Height = 30
End With
Next
If dic.Count = 1 Then '全部工作表列数相同
CheckBox1.Value = True
Else '列数不同
CheckBox1.Enabled = False '“*”复选框无效
CheckBox101.Value = True '全部字段
End If
If flag Then CheckBox102.Enabled = False
End Sub
|
|