Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button '' ''Button1 '' Me.Button1.Location = New System.Drawing.Point(65,63) Me。 Button1.Name =" Button1" Me.Button1.Size = New System.Drawing.Size(75,23) Me.Button1.TabIndex = 0 Me.Button1.Text =" Button1" Me.Button1.UseVisualStyleBackColor = True End Sub Friend WithEvents Button1 As System.Windows.Forms.Button 这声明了一个按钮,然后在InitialiazeComponent方法中它 设置它'' s属性 - 包括名称。并且由于这种方法应该在表单实例化时运行,这证明运行时 分配Name属性是完全正常的。 谢谢, Seth Rowe I am reading a book that says that the "name" property can be altered onlyat design time and cannot be modified at runtime. Please explain this given the code below... If you click Button3... fred will appear as the Name of Button1, however,the handler for Button1 works still works as well...Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.ClickMsgBox("HELP") End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button3.ClickMe.Button1.Name = "fred"Dim i As Integer = 0Dim str As String While i < Me.Controls.CountDim c As Control = Me.Controls(i)str = c.NameMsgBox(str)i = i + 1 End WhileEnd Sub 解决方案 What book is this? I would like to have a conversation with the authoras you can change control names at runtime with no problems. This is because it''s handling the Click event for the control Button1,not the control named "Button1." If you look at the designer generatedcode, you will see that when you add a button to the designer itgenerates something like this: Private Sub InitializeComponent()Me.Button1 = New System.Windows.Forms.Button ''''Button1''Me.Button1.Location = New System.Drawing.Point(65, 63)Me.Button1.Name = "Button1"Me.Button1.Size = New System.Drawing.Size(75, 23)Me.Button1.TabIndex = 0Me.Button1.Text = "Button1"Me.Button1.UseVisualStyleBackColor = TrueEnd Sub Friend WithEvents Button1 As System.Windows.Forms.Button This declares a button, and then in the InitialiazeComponent method itsets it''s properties - including Name. And since this method shouldrun whenever a form is instantiated, this proves that runtimeassigning of the Name property is perfectly fine. Thanks, Seth Rowe 这篇关于关于Name属性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 11:41