关于Name属性的问题 [英] A question regarding the Name property

查看:47
本文介绍了关于Name属性的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一本书,上面写着名字。属性只能在设计时更改

并且不能在运行时修改。


请根据下面的代码解释一下......


如果单击Button3 ... fred将显示为Button1的名称,但是,

Button1的处理程序仍可正常工作...

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

MsgBox(" HELP")


End Sub


Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button3.Click

Me.Button1.Name =" fred"

Dim i As Integer = 0

Dim str As String


虽然我< Me.Controls.Count

Dim c As Control = Me.Controls(i)

str = c.Name

MsgBox(str)

i = i + 1


结束时

结束子

解决方案

Rob,


恐怕我不明白你的问题是完整的,但是永远无法控制

有一系列控件。 (所以那个人又有了)。该系列

与收集物品的indiviuel名称无关。


Cor

" Rob" < ro *** @ yahoo.comschreef在bericht

新闻:HY *************************** ***@comcast.com。 ..


>我正在读一本书,上面写着名字。属性只能在设计时更改
并且不能在运行时修改。


请根据下面的代码解释一下......


如果单击Button3 ... fred将显示为Button1的名称,但是,

Button1的处理程序仍可正常工作...


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

MsgBox(" HELP")


End Sub


Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button3.Click

Me.Button1.Name =" fred"

Dim i As Integer = 0

Dim str As String


虽然我< Me.Controls.Count

Dim c As Control = Me.Controls(i)

str = c.Name

MsgBox(str)< br $>
i = i + 1


结束时

End Sub



5月13日下午6:56,Rob < r ... @ yahoo.comwrote:


我正在读一本书,上面写着名字。属性只能在设计时更改

并且不能在运行时修改。


请根据下面的代码解释一下......


如果单击Button3 ... fred将显示为Button1的名称,但是,

Button1的处理程序仍可正常工作...


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

MsgBox(" HELP" ;)


End Sub


Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button3.Click

Me.Button1.Name =" fred"

Dim i As Integer = 0

Dim str作为字符串


虽然我< Me.Controls.Count

Dim c As Control = Me.Controls(i)

str = c.Name

MsgBox(str)

i = i + 1


结束时

结束Sub


我正在读一本书,上面写着名字。属性只能在设计时更改

,并且不能在运行时修改。



这是什么书?我想与作者进行对话

,因为你可以在运行时更改控件名称而没有任何问题。


Button1的处理程序作品仍然有效...



这是因为它正在处理控件Button1的Click事件,

而不是控件名为Button1。如果你看看设计师生成的

代码,你会看到当你向设计师添加一个按钮时它会产生这样的东西:

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


Hi Cor,


好​​吧,这本书指出你不能改变名字。控件的属性

在运行时...


点击按钮不是运行时 ?


如果是这样,请查看Button3的代码......它将Button1的名称更改为

" fred",然后报告& ;名称" Button1的内容是fred。


但是,Button1的代码仍然执行(Handles Button1.click)...如果

这个名字真的是真的变成了弗雷德,那就不应该执行了。


只是有点困惑......


谢谢,

Rob


" Cor Ligthert [MVP]" < no ************ @ planet.nlwrote in message

news:ud ************** @ TK2MSFTNGP05.phx .gbl ...


Rob,


恐怕我不明白你的问题是完整的,但是曾经

控件有一系列控件。 (所以那个人又有了)。那个

集合与收集物品的indiviuel名称无关。


Cor


" Rob" < ro *** @ yahoo.comschreef在bericht

新闻:HY *************************** ***@comcast.com。 ..


>>我正在读一本书,上面写着名字。属性只能在设计时更改
并且不能在运行时修改。

请根据下面的代码解释一下......

如果单击Button3。 ..fred将显示为Button1的名称,但是,Button1的处理程序仍然有效...

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e正如
System.EventArgs)处理Button1.Click
MsgBox(帮助)

结束子

私人子按钮3_Click(ByVal发送者作为系统.Object,ByVal e As
System.EventArgs)处理Button3.Click
Me.Button1.Name =" fred"
Dim i As Integer = 0
Dim str As String

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

结束时
结束子




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

解决方案

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



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


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




这篇关于关于Name属性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆