A question regarding the Name property

R

Rob

I am reading a book that says that the "name" property can be altered only
at 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 As
System.EventArgs) Handles Button1.Click
MsgBox("HELP")

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Me.Button1.Name = "fred"
Dim i As Integer = 0
Dim str As String

While i < Me.Controls.Count
Dim c As Control = Me.Controls(i)
str = c.Name
MsgBox(str)
i = i + 1

End While
End Sub
 
C

Cor Ligthert [MVP]

Rob,

I am afraid that I don't understand your question complete, but ever control
has a collection of controls. (So that one has that again). That collection
has nothing to do with indiviuel names of collection items.

Cor
 
R

rowe_newsgroups

I am reading a book that says that the "name" property can be altered only
at 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 As
System.EventArgs) Handles Button1.Click
MsgBox("HELP")

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Me.Button1.Name = "fred"
Dim i As Integer = 0
Dim str As String

While i < Me.Controls.Count
Dim c As Control = Me.Controls(i)
str = c.Name
MsgBox(str)
i = i + 1

End While
End Sub

I am reading a book that says that the "name" property can be altered only
at design time and cannot be modified at runtime.

What book is this? I would like to have a conversation with the author
as you can change control names at runtime with no problems.
the handler for Button1 works still works as well...

This is because it's handling the Click event for the control Button1,
not the control named "Button1." If you look at the designer generated
code, you will see that when you add a button to the designer it
generates 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 = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
End Sub

Friend WithEvents Button1 As System.Windows.Forms.Button

This declares a button, and then in the InitialiazeComponent method it
sets it's properties - including Name. And since this method should
run whenever a form is instantiated, this proves that runtime
assigning of the Name property is perfectly fine.

Thanks,

Seth Rowe
 
R

Rob

Hi Cor,

Well, the book states that you cannot alter the "name" property of a control
at runtime...

Isn't the clicking of a Button considered "run-time" ?

If so, look at the code for Button3... It changes the name of Button1 to
"fred", then reports that the "name" of Button1 is "fred".

However, the code for Button1 still executes (Handles Button1.click)... if
the name were really changed to fred, then it should not execute.

Just a little confused...

Thanks,
Rob
 
R

Rob

Seth,

I want to make sure I have stated correctly and in the proper context...

From the book...

Regarding common properties of controls (to quote)...

Name - "Represents the name used to refer to the control in code. This
property can be altered only at design time and cannot be modified at run
time."

So I guess the control has some "unique attribute" other than the "name"
property that uniquely identifies it. Maybe that is the attribute that
cannot be changed ?

Anyway, I created 2 buttons on a form (used VS2003 for this), then modified
the code as follows... I expected it to crash due to the duplicate Button1
instantiation, instead, it appeared to apply a "last line of code wins"
logic set... as the sole button that appeared had its text property set to
"Button2".


Me.Button1 = New System.Windows.Forms.Button
' Formerly Button 2 below
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(88, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Button2
' Changed the following....
Me.Button1.Location = New System.Drawing.Point(80, 56)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button2"

Thanks,
Rob
 
R

rowe_newsgroups

Seth,

I want to make sure I have stated correctly and in the proper context...

From the book...

Regarding common properties of controls (to quote)...

Name - "Represents the name used to refer to the control in code. This
property can be altered only at design time and cannot be modified at run
time."

So I guess the control has some "unique attribute" other than the "name"
property that uniquely identifies it. Maybe that is the attribute that
cannot be changed ?

Anyway, I created 2 buttons on a form (used VS2003 for this), then modified
the code as follows... I expected it to crash due to the duplicate Button1
instantiation, instead, it appeared to apply a "last line of code wins"
logic set... as the sole button that appeared had its text property set to
"Button2".

Me.Button1 = New System.Windows.Forms.Button
' Formerly Button 2 below
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(88, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Button2
' Changed the following....
Me.Button1.Location = New System.Drawing.Point(80, 56)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button2"

Thanks,
Rob

Again, what book are you reading? I'm starting to think you are
reading a VB6 book - not one for Visual Basic .Net.

Thanks,

Seth Rowe
 
R

rowe_newsgroups

Hi Cor,

Well, the book states that you cannot alter the "name" property of a control
at runtime...

Isn't the clicking of a Button considered "run-time" ?

If so, look at the code for Button3... It changes the name of Button1 to
"fred", then reports that the "name" of Button1 is "fred".

However, the code for Button1 still executes (Handles Button1.click)... if
the name were really changed to fred, then it should not execute.

Just a little confused...

Thanks,
Rob




However, the code for Button1 still executes (Handles Button1.click)... if
the name were really changed to fred, then it should not execute.

Read my earlier post. The handles clause refers to the object, not the
object's name.

Thanks,

Seth Rowe
 
R

Rob

Microsoft .Net Framework 2.0 Windows-based Client Development
Self paced Training kit for Exam 70-526
Page 53

Thanks,
Rob
 
R

Rob

As far as a unique control identifier...

I understand what you are saying....

' The following does invoke an error...
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button
 
R

rowe_newsgroups

As far as a unique control identifier...

I understand what you are saying....

' The following does invoke an error...
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button





As far as a unique control identifier...

I understand what you are saying....

Good! The only time I use the Name property is when I am using the
FindControl method on dynamically created controls.

Thanks,

Seth Rowe
 
R

rowe_newsgroups

Seth,

I want to make sure I have stated correctly and in the proper context...

From the book...

Regarding common properties of controls (to quote)...

Name - "Represents the name used to refer to the control in code. This
property can be altered only at design time and cannot be modified at run
time."

So I guess the control has some "unique attribute" other than the "name"
property that uniquely identifies it. Maybe that is the attribute that
cannot be changed ?

Anyway, I created 2 buttons on a form (used VS2003 for this), then modified
the code as follows... I expected it to crash due to the duplicate Button1
instantiation, instead, it appeared to apply a "last line of code wins"
logic set... as the sole button that appeared had its text property set to
"Button2".

Me.Button1 = New System.Windows.Forms.Button
' Formerly Button 2 below
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(88, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Button2
' Changed the following....
Me.Button1.Location = New System.Drawing.Point(80, 56)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button2"

Thanks,
Rob










I expected it to crash due to the duplicate Button1
instantiation, instead, it appeared to apply a "last line of code wins"

Setting Button1 to a new button just "resets" the button - it won't
cause a crash.

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top