1

我正在尝试创建一个所见即所得的 android 布局 xml 编辑器。我有一个类 button(),它包含一个按钮的所有相关属性,可以放置在布局上(文本颜色、大小、位置等)。当用户单击表单上的按钮时,会在编辑器视图中添加一个新按钮。我希望能够为布局中的每个按钮创建此类的新实例,以便将它们写入 xml 文件。但是,在声明实例时,我希望能够将它们命名为 button1、button2 等。也许一些代码会更有意义......:

Public Class button 'The class where all the properties for the button are defined
    ...
End Class

Public Partial Class MainForm 'The mainform class
    Dim btnclassno As Integer = 0 'The number of button() classes made
    ...
    Sub btnAddButtonClick(sender As Object, e As EventArgs)
        btnclassno += 1 'Changes it by 1
        Dim (newbutton & btnclassno) As button = New button()'Defines a new instance of the class called newbutton and then the value of btnclassno (e.g. newbutton1, newbutton2 etc.)
        ...
    End Sub
End Class

我想知道这是否可能,如果可以,该怎么做,或者更好的方法是在没有硬编码名称的情况下创建类的新实例(我对类的概念相对较新,所以我不太明白如何使用它们)。谢谢。

4

1 回答 1

0

你可以用这个

Sub btnAddButtonClick(sender As Object, e As EventArgs)
    static btnclassno = 0
    Dim newButton As new Button

    btnclassno += 1 'Changes it by 1

    newButton.Name = "newButton" & trim(str(btnClassno))

    'another newButton properties here 

    Me.Controls.Add(newButton)

    'Dim (newbutton & btnclassno) As button = New button()'Defines a new instance of 

    'class called newbutton and then the value of btnclassno (e.g. newbutton1, newbutton2 etc.)

  End Sub
于 2013-05-31T06:36:28.427 回答