请教.如何向panel添加控件数组
请教 如何在panel动态添加label控件数组 在网上了找了很多教程.. 都是向窗体中动态添加.如下类这是VB.NET的一个类
Public Class ButtonArray
Inherits System.Collections.CollectionBase
Private ReadOnly ParentForm As System.Windows.Forms.Form
Public Sub New(ByVal pForm As System.Windows.Forms.Form)
ParentForm = pForm
End Sub
Default Public ReadOnly Property Item(ByVal index As Integer) As System.Windows.Forms.Button
Get
Return Me.List.Item(index) ' ButtonArray的List 属性从CollectionBase 继承
End Get
End Property
Public Sub AddItem()
Dim btnItem As New System.Windows.Forms.Button()
Me.List.Add(btnItem)
ParentForm.Controls.Add(btnItem) '向窗体中增加控件
btnItem.Tag = Me.Count 'Count属性从CollectionBase 继承
btnItem.Top = Me.Count * 30
btnItem.Left = 200
btnItem.Text = "Button" & Me.Count.ToString
AddHandler btnItem.Click, AddressOf btnItem_Click '绑定事件处理程序
End Sub
Public Sub RemoveItem()
If Me.Count > 0 Then
ParentForm.Controls.Remove(Me(Me.Count - 1))
Me.List.RemoveAt(Me.Count - 1)
End If
End Sub
Public Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'在这里编写控件数组对点击事件的响应
'例如:
MsgBox("点击:" & sender.GetType().ToString & CType(CType(sender, Button).Tag, String))
End Sub
End Class
此类可以向窗休中添加..但不知如何向Panel中添加
--------------------编程问答-------------------- Private ReadOnly ParentForm As System.Windows.Forms.panel
Public Sub New(ByVal pForm As System.Windows.Forms.panel)
ParentForm = pForm
End Sub
以修改成相应的这样..
用dim aaa as ButtonArray (me),,,在窗体上动态添加Button就可以
用dim aaa as ButtonArray (panel1),,, 这样为什么就不行呢... --------------------编程问答-------------------- 把ParentForm.Controls.Add(btnItem)换成Panel.Controls.Add(btnItem) 就行啦.
容器控件使用方式都是一样的. --------------------编程问答-------------------- 不行啊..我试过了..从 Dim la As New ButtonArray(panel1) 进类时.就没能把panel1传到类里面去 用me 就能传进去
补充:.NET技术 , C#