2013-05-30 16 views
1

我想创建一个wysiwyg android布局xml编辑器。我有一个类button(),它包含可放置在布局(textcolour,size,position等)上的按钮的所有relavent属性。 当用户单击表单上的按钮时,会在编辑器视图中添加一个新按钮。我希望能够为布局中的每个按钮创建此类的新实例,以便它们可以写入xml文件。然而,声明实例时,我希望能够将它们命名为Button1的,BUTTON2等也许一些代码会更有意义...:你可以在vb.net的声明语句中的新变量的标识符中包含其他变量的值吗?

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 

我想知道这甚至有可能,和如果是这样做,或者甚至更好的一种更有效的方法来创建类的新实例而不使用硬编码名称(我对类的概念相对陌生,所以我不太了解如何使用它们)。 谢谢。

+0

这是可以做到,但我必须要问,为什么? – Brian

回答

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 
+0

我得到的错误:“类型'字符串'的值不能转换为'xml_editor.Button'(在ctype行上)&”类型'xml_editor.Button'的值不能转换为'System.Windows.Forms.Control' “(controls.add)你能解释一下这段代码的意思吗?因为据我所知,ctype正在将”newButton1“或其他任何东西转换成Button类,这并没有什么帮助,我希望能够将btnclassno的值合并到newButton变量的标识符中(对不起,我没有投票,因为代码实际上没有工作) – user2437970

+0

它被更改了..尝试它 – matzone

+0

@ matzone好了.name位现在可以工作,但我仍然得到Controls.add error。Me.controls是否只包含程序的窗体(界面)上的对象?为什么要将该类添加到该帮助中?我现在听起来非常关键,谢谢你试图帮助。 – user2437970

相关问题