繁体   English   中英

如何在vb.net中动态地将面板控件克隆为相同形式

[英]How to clone the panel control to the same form dynamically in vb.net

我需要在表单中的同一面板下生成现有面板的副本

我已经编写了一些代码,可以完成我想要的工作,但是并不能根据我的需要工作,但是它破坏了主面板并生成了一个副本,但是我不想破坏第一个,我想保留所有面板...

这是我在按钮“ +”单击事件上调用的功能

Friend Function AddNewPanel() As System.Windows.Forms.Panel
        Dim Pnl As New System.Windows.Forms.Panel
        Pnl = MediPanel  'This is the main panel that I want to copy
        Pnl.Top = 500
        'Pnl.Left = 100        
        ParentPanel.Controls.Add(Pnl)  'ParentPanel in which I want to generate a copy
        Return Pnl
    End Function

这正是我真正想要的,实际上是这样

因此,我想在按“ +”按钮时生成面板的副本,而且较早的面板也不应销毁。

动态生成一个新控件。 加载表单时,在按钮上设置一个标签,指示原始面板的底部位置。

button1.tag = Medipanel.Bottom

当您单击+按钮时

Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click

   Dim new_panel As New Panel
   With new_panel
     .Location = New Point(Medipanel.Left, Cint(button1.tag) + 5)
     .Size = New Size (Medipanel.Width, Medipanel.Height)
     ....
   End With

   Dim some_label = New Label
   With some_label
    .AutoSize = True
    .Name = "label"
    .Location = New Point(0, 0)  ''' Position in the new panel
    .Text = "Some Text" 
   End With
   new_panel.Controls.Add(some_label)

   Dim some_button as New Button
   With some_button
        .Tag = "some value" '''' A way to Identify the button if clicked
        ....
        ....
        AddHandler some_button.click, AddressOf some_button_click 
        '''' The Sub that determines what happens when that button is clicked.
   End With

   ....
   ....     

   ParentPanel.Controls.Add(new_panel)

   button1.tag = new_panel.bottom '''' Set the bottom position of the last panel added.

End Sub

单击新按钮时:

Private Sub some_button_click(sender As Object, e As EventArgs)

   Dim sending_button As Button = DirectCast(sender, Button)
   Dim ref As String = DirectCast(sending_button.Tag, String)

   ''''Perform your stuff here

End Sub

这只会添加一个新控件,而不会破坏旧控件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM