动态添加控件的问题
先看下我的代码吧:
Private WithEvents labelone As Label
Private WithEvents labeltwo As Label
Private Sub Form_Click()
Dim ym As String
Dim ii As Integer
Dim i As Integer
Open App.Path & "\MENU.INI " For Input Access Read As #1
Do While Not EOF(1)
Line Input #1, ym
Set labelone = Controls.Add( "vb.label ", "labelone ", Me)
With labelone
.Alignment = 2
.AutoSize = True
.Font = "标楷体 "
.BackStyle = 0
.FontSize = 24
.FontBold = True
.Left = 220
.Top = 180 + i * 500
.Caption = ym
.Visible = True
End With
Set labeltwo = Controls.Add( "vb.label ", "labeltwo ", Me)
With labeltwo
.Alignment = 2
.AutoSize = True
.Font = "标楷体 "
.BackStyle = 0
.FontSize = 24
.FontBold = False
.Left = 200
.Top = 200 + i * 500
.Caption = ym
.Visible = True
End With
i = i + 1
End If
Loop
Close #1
End Sub
原意是文本MENU.INI中有多少行数据就生成多少乘以2个LABEL控件
每两个控件显示一行,产生投影效果
现在问题是只会显示最后一行数据
大家帮帮忙如何解决
在线等
解决了卖血也把分送上
[解决办法]
Private WithEvents labelone As Label
Private WithEvents labeltwo As Label
Private Sub Form_Click()
Dim ym As String
Dim ii As Integer
Dim i As Integer
Open "d:\MENU.INI " For Input Access Read As #1
Do While Not EOF(1)
Line Input #1, ym
Set labelone = Controls.Add( "vb.label ", "labelone " & i, Me)
With labelone
.Alignment = 2
.AutoSize = True
.Font = "标楷体 "
.BackStyle = 0
.FontSize = 24
'.FontBold = True
.Left = 220
.Top = 180 + i * 500
.Caption = ym
.Visible = True
End With
Set labeltwo = Controls.Add( "vb.label ", "labeltwo " & i, Me)
With labeltwo
.Alignment = 2
.AutoSize = True
.Font = "标楷体 "
.BackStyle = 0
.FontSize = 24
.FontBold = False
.Left = 260
.Top = 200 + i * 500
.Caption = ym
.Visible = True
.ZOrder 1
End With
i = i + 1
Loop
Close #1
End Sub
[解决办法]
//原意是文本MENU.INI中有多少行数据就生成多少乘以2个LABEL控件
每两个控件显示一行,产生投影效果
现在问题是只会显示最后一行数据
第1 窗体中控件的总数目是有限制的
第2 你只定义了两个Label,所以不管怎么写代码,窗体上总终显示的还是2个能相应事件的Label
第3 如果可以这么定义:
Private WithEvents labelone( ) As Label
Private WithEvents labeltwo( ) As Label
就好了,但是,很遗憾,vb不支持这样的写法
第4 建议用控件数组解决,即先在窗体上画两个Label,然后设置其index属性为0,在需要添加控件的时候 用load 控件名(索引)即可添加控件,剩下的问题就是调整控件的大小 坐标 可见性等等了,这个你应该没问题吧