分类:
2008-10-13 16:09:17
Private Sub cmdMove_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdMove.Click Dim Label2NewIndex As Integer Try Label2NewIndex = numIndex.Value Me.Controls.SetChildIndex(Me.Label2, Label2NewIndex) ShowIndex() Catch ex As Exception Throw New Exception(ex.Message) End Try End Sub Sub ShowIndex() txtLabel1Index.Text = Me.Controls.GetChildIndex(Me.Label1).ToString txtLabel2Index.Text = Me.Controls.GetChildIndex(Me.Label2).ToString End Sub
Private Sub cmdCopy_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdCopy.Click ReDim MyArrayOfControls(Me.Controls.Count) Me.Controls.CopyTo(MyArrayOfControls, 0) End Sub Private Sub cmdNew_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdNew.Click Dim frm As New frmArrayClient Dim ct As Control frm.ShowDialog() Me.Controls.AddRange(MyArrayOfControls) For Each ct In Me.Controls If ct.GetType.ToString = "System.Windows.Forms.Button" Then ct.Visible = True End If Next End Sub Private Sub cmdNewForm2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdNewForm2.Click Dim frm As New frmArrayClient frm.ShowDialog() End Sub
Module modControls Friend Function CloneControl(ByVal c As Control) As Control ' instantiate another instance of the given control and ' clone the common properties Dim newControl As Control = CType(NewAs(c), Control) CloneProperties(newControl, c, "Visible", "Size", "Font", _ "Text", "Location", "BackColor", "ForeColor", "Enabled", _ "BackgroundImage") ' clone properties unique to specific controls If TypeOf newControl Is ButtonBase Then CloneProperties(newControl, c, "DialogResult", _ "BackgroundImage", "FlatStyle", "TextAlign", "Image", _ "ImageAlign", "ImageIndex", "ImageList") ElseIf TypeOf newControl Is LinkLabel Then CloneProperties(newControl, c, "VisitedLinkColor", _ "LinkVisited", "LinkColor", "LinkBehavior", "LinkArea", _ "FlatStyle", "BorderStyle", "DisabledLinkColor", _ "ActiveLinkColor", "Image", "ImageAlign", "ImageIndex", _ "ImageList") End If Return newControl End Function End Module Public Module ObjectFactory Public Function NewAs(ByVal t As Type) As Object Return t.Assembly.CreateInstance(t.FullName) End Function Public Function NewAs(ByVal x As Object) As Object Return ObjectFactory.NewAs(x.GetType()) End Function Public Sub CloneProperties( ByVal target As Object, ByVal source As _ Object, ByVal ParamArray propertyNames() As String) Dim sourceProperties As New PropertyAccessor(source) Dim targetProperties As New PropertyAccessor(target) Dim p As String For Each p In propertyNames targetProperties(p) = sourceProperties(p) Next End Sub End Module
Imports System.Reflection Public Class PropertyAccessor Public Sub New(ByVal target As Object) Me.target_ = target End Sub Public ReadOnly Property Target() As Object Get Return Me.target_ End Get End Property Default Public Property Item(ByVal propertyName As String) As Object Get Dim prop As PropertyInfo = _ Me.Target.GetType().GetProperty(propertyName) Return prop.GetValue(Me.Target, Nothing) End Get Set(ByVal value As Object) Dim prop As PropertyInfo = _ Me.Target.GetType().GetProperty(propertyName) prop.SetValue(Me.Target, value, Nothing) End Set End Property #Region " Private State " Private target_ As Object #End Region End Class