2017-06-03 28 views
1

我需要動態地克隆面板控制,以相同的形式是生成現有面板的同一面板下的副本,我的形式如何在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 

This is what I exactly want但是This is actually happened

所以,我要生成面板的副本,我按下「+」按鈕,也更早板不應該被銷燬..

回答

0

動態生成一個新的控制。當您的表單加載時,爲您的按鈕設置一個標籤,指示原始面板的底部位置。

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 

這隻會增加一個新的控制和不破壞舊的。