2014-10-07 40 views
4

我想通過用戶控件複製我的窗體上的控件,該控件與添加了'new'的用戶控件具有相同的名稱。 (Label1的----> newLabel1)在Visual Basic 6.0中:創建動態控制數組

這是我的代碼片段:

Private Sub CreateLabel(ByRef lblControl As Control) 

    'lblControl - this is the control i would like to duplicate 


    'The reference to the new created control itself so i can work with it within this sub. 
    Dim newControl As Control 


    Set newControl = Form1.Controls.Add _ 
    ("Project.Project1", "new" & lblControl.Name, lblControl.Container) 

    newControl.Visible = True 

End Sub 

,如果我想複製其不被索引 怎麼過,我無法控制它的偉大工程複製一個數組中的控件作爲lblControl.Name只需取其名稱,而不是它的索引,並用索引名稱替換名稱(lblControl.Name &「(」lblControl.Index &「)」不起作用。

另外,創建一個控件並更改其索引值val你在創建後不工作。

所以我的問題是,我如何使用上述方法創建一個數組?

回答

1

如果控制已經是一個控件數組,然後使用Load創建控件的新實例。

假設您在設計時將標籤lblControlIndex設置爲0(使其成爲控件數組),您可以使用以下代碼添加其另一個實例。

Dim newControl As Control 
Load lblLabelInControlArray(1) '1 is the Index value that will be sued 
Set newControl = lblLabelInControlArray(1) 

顯然,你會希望跟蹤你所使用的指標,如VB6確實允許的差距,從而你加載和卸載他們來說,這可能會比較混亂。