给你看看我做过的一个例子:我动态加载LABEL控件.
Private Sub Setup_Button(ByVal iLeft As Integer, ByVal iTop As Integer, ByVal strCaption As String) Dim lblArray As New Label()
lblArray.Left = iLeft
lblArray.Top = iTop
lblArray.Width = IcoWidth
lblArray.Height = IcoHeight
If Len(strCaption) <= 8 Then
lblArray.Text = strCaption
Else
lblArray.Text = strCaption.Substring(0, 6) + "..."
End If
lblArray.TextAlign = ContentAlignment.BottomCenter
lblArray.Cursor = Windows.Forms.Cursors.SizeAll Controls.Add(lblArray) lblArray.CreateControl()
AddHandler lblArray.MouseMove, AddressOf lblArray_MouseMove
End Sub
下面是自动排列的程序:
Const IcoWidth = 60 '图标宽度
Const IcoHeight = 60
Const XGap = 20 '水平间距
Const YGap = 20
Private Sub mnuArrangeIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuArrangeIcon.Click
Dim NumberOfPerCol As Integer '每列可以显示的控件数
NumberOfPerCol = (Me.Height - ToolBar1.Height - YGap) \ (IcoHeight + YGap) Dim ctl As Control
Dim i As Integer
For Each ctl In Me.Controls
If InStr(ctl.GetType.FullName, "Label") <> 0 Then
ctl.Left = XGap + (i \ NumberOfPerCol) * (IcoWidth + XGap)
ctl.Top = ToolBar1.Height + YGap + (i Mod NumberOfPerCol) * (IcoHeight + YGap)
i = i + 1
End If
Next
Private Sub Setup_Button(ByVal iLeft As Integer, ByVal iTop As Integer, ByVal strCaption As String) Dim lblArray As New Label()
lblArray.Left = iLeft
lblArray.Top = iTop
lblArray.Width = IcoWidth
lblArray.Height = IcoHeight
If Len(strCaption) <= 8 Then
lblArray.Text = strCaption
Else
lblArray.Text = strCaption.Substring(0, 6) + "..."
End If
lblArray.TextAlign = ContentAlignment.BottomCenter
lblArray.Cursor = Windows.Forms.Cursors.SizeAll Controls.Add(lblArray) lblArray.CreateControl()
AddHandler lblArray.MouseMove, AddressOf lblArray_MouseMove
End Sub
下面是自动排列的程序:
Const IcoWidth = 60 '图标宽度
Const IcoHeight = 60
Const XGap = 20 '水平间距
Const YGap = 20
Private Sub mnuArrangeIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuArrangeIcon.Click
Dim NumberOfPerCol As Integer '每列可以显示的控件数
NumberOfPerCol = (Me.Height - ToolBar1.Height - YGap) \ (IcoHeight + YGap) Dim ctl As Control
Dim i As Integer
For Each ctl In Me.Controls
If InStr(ctl.GetType.FullName, "Label") <> 0 Then
ctl.Left = XGap + (i \ NumberOfPerCol) * (IcoWidth + XGap)
ctl.Top = ToolBar1.Height + YGap + (i Mod NumberOfPerCol) * (IcoHeight + YGap)
i = i + 1
End If
Next
还有一个问题例如我想要在窗体里每点击一下鼠标就生成一个按钮,并按b1 b2 b3...次序命名.请问该怎么做.谢谢.
定义button btn=new button();
btn.name="b"+count.tostring()就可以