By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,249 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,249 IT Pros & Developers. It's quick & easy.

A question regarding the Name property

P: n/a
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

May 13 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
"Rob" <ro***@yahoo.comschreef in bericht
news:HY******************************@comcast.com. ..
>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

May 14 '07 #2

P: n/a
On May 13, 6:56 pm, "Rob" <r...@yahoo.comwrote:
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

May 14 '07 #3

P: n/a
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

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ud**************@TK2MSFTNGP05.phx.gbl...
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
"Rob" <ro***@yahoo.comschreef in bericht
news:HY******************************@comcast.com. ..
>>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


May 14 '07 #4

P: n/a
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

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11*********************@w5g2000hsg.googlegrou ps.com...
On May 13, 6:56 pm, "Rob" <r...@yahoo.comwrote:
>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

May 14 '07 #5

P: n/a
On May 14, 8:22 am, "Rob" <r...@yahoo.comwrote:
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

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11*********************@w5g2000hsg.googlegrou ps.com...
On May 13, 6:56 pm, "Rob" <r...@yahoo.comwrote:
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
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

May 14 '07 #6

P: n/a
On May 14, 7:41 am, "Rob" <r...@yahoo.comwrote:
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

"Cor Ligthert [MVP]" <notmyfirstn...@planet.nlwrote in messagenews:ud**************@TK2MSFTNGP05.phx.gbl. ..
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
"Rob" <r...@yahoo.comschreef in bericht
news:HY******************************@comcast.com. ..
>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
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

May 14 '07 #7

P: n/a
Rob
Microsoft .Net Framework 2.0 Windows-based Client Development
Self paced Training kit for Exam 70-526
Page 53

Thanks,
Rob

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
On May 14, 8:22 am, "Rob" <r...@yahoo.comwrote:
>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

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11*********************@w5g2000hsg.googlegro ups.com...
On May 13, 6:56 pm, "Rob" <r...@yahoo.comwrote:
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

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

May 14 '07 #8

P: n/a
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


"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
On May 14, 8:22 am, "Rob" <r...@yahoo.comwrote:
>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

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11*********************@w5g2000hsg.googlegro ups.com...
On May 13, 6:56 pm, "Rob" <r...@yahoo.comwrote:
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

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

May 14 '07 #9

P: n/a
On May 14, 9:36 am, "Rob" <r...@yahoo.comwrote:
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

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@w5g2000hsg.googlegro ups.com...
On May 14, 8:22 am, "Rob" <r...@yahoo.comwrote:
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
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message
>news:11*********************@w5g2000hsg.googlegro ups.com...
On May 13, 6:56 pm, "Rob" <r...@yahoo.comwrote:
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
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
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

May 14 '07 #10

P: n/a
On May 14, 8:22 am, "Rob" <r...@yahoo.comwrote:
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

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11*********************@w5g2000hsg.googlegrou ps.com...
On May 13, 6:56 pm, "Rob" <r...@yahoo.comwrote:
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
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

May 14 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.