求助,vb里动态生成n个label并能够根据窗口大小自动调整每行个数和每个label的文字大小。

2025-05-10 19:02:15
推荐回答(2个)
回答(1):

下面是我为你写的一个函数,我用的名字是label1 设其index=0(让它为一个数组控件)
Private Function CreateLable(nstr As String) As Integer
Dim i As Integer
Load Label1(Label1.Count)'这个是添加,如果重新排列 ,一个for 下面的改下!
With Label1(Label1.Count - 1)'如果要重新再排时,这里稍作修改就行!
.AutoSize = True
.Caption = nstr
.Move IIf(Me.ScaleWidth > .Width + Label1(Label1.Count - 2).Left + Label1(Label1.Count - 2).Width, Label1(Label1.Count - 2).Left + Label1(Label1.Count - 2).Width, 0), _
IIf(Me.ScaleWidth > Label1(Label1.Count - 2).Left + Label1(Label1.Count - 2).Width + .Width, Label1(Label1.Count - 2).Top, Label1(Label1.Count - 2).Top + Label1(Label1.Count - 2).Height)
.Visible = True
End With
CreateLable = Label1.Count - 1
End Function

回答(2):

Private Sub LoadLabel()
Dim i As Integer, n As Integer

n = Me.Height / Label1(0).Height

With Label1(0)
.Left = 0
.Top = 0
.AutoSize = True
End With

For i = 1 To n
Load Label1(n)
Next

For i = 0 To n
With Label1(i)
Me.Width = .Width
.Visible = True
End With
Next
End Sub