以编程方式添加控件的问题 - 无法获取属性,甚至无法引用控件! [英] Problems with programmatically added control - can't get properties, can't even reference control!

查看:87
本文介绍了以编程方式添加控件的问题 - 无法获取属性,甚至无法引用控件!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码添加一堆控件。


问题1:当程序流传递给UpperChanged时当我点击它时,

控件名称未定义。当我进入:


如果udUpperLim1.Value 1那么


我收到一个错误,udUpperLim1是未定义所以我无法获得控件中的

值。


问题2:当我使用sender.tostring时,它返回

控制,而不是其指定的名称。我得到了

" System.Windows.Forms.NumericUpDown"而不是udUpperLim1而且我不知道
知道谁被改变了以及前面的问题,即使我是b $ b,我也不能引用它! br />

那些决定删除控制数组的人不要做任何事情

FAVORS !!!!!!!!!!

这一切都直接来自一本书,并没有做任何有用的事情

我。


帮助,拜托!


Mike Morrow

MSDN:MMSA00E62537


iTop = 84


Dim udUpperLim1作为New NumericUpDown

udUpperLim1.SetBounds(228,iTop,45,20)

iTop + = 20

Me.Controls.Add(udUpperLim1 )

AddHandler udUpperLim1.ValueChanged,AddressOf UpperChanged

udUpperLim1.Value = My.Settings.Alarm1


Dim udUpperLim3 As New NumericUpDown

udUpperLim3.SetBounds(228,iTop,45,20)

iTop + = 20

Me.Controls.Add(udUpperLim3)

AddHandler udUpperLim3.ValueChanged ,AddressOf UpperChanged

udUpperLim3.Value = My.Settings.Alarm3


Dim udUpperLim5 As New NumericUpDown

udUpperLim5.SetBounds(228 ,iTop,45,20)

iTop + = 20

Me.Controls.Add(udUpperLim5)

AddHandler udUpperLim5.ValueChanged,AddressOf UpperChanged

udUpperLim5.Value = My.Settings.Alarm5

I am adding a bunch of controls with the code below.

Problem 1: When program flow passes to "UpperChanged" when I click it,
the control name is undefined. When I enter:

If udUpperLim1.Value 1 Then

I get an error that udUpperLim1 is "Not Defined" so I cannot get the
value in the control.

Problem 2: When I use sender.tostring, it returns the class name of the
control, not its assigned name. I get
"System.Windows.Forms.NumericUpDown" and not udUpperLim1 and I don''t
know who was changed and along with the preceding problem, even if I
did, I can''t reference it!

The folks who decided to remove control arrays DID NOT DO US ANY
FAVORS!!!!!!!!!!

This is all straight out of a book and is not doing anything useful for
me.

Help, Please!

Mike Morrow
MSDN:MMSA00E62537

iTop = 84

Dim udUpperLim1 As New NumericUpDown
udUpperLim1.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim1)
AddHandler udUpperLim1.ValueChanged, AddressOf UpperChanged
udUpperLim1.Value = My.Settings.Alarm1

Dim udUpperLim3 As New NumericUpDown
udUpperLim3.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim3)
AddHandler udUpperLim3.ValueChanged, AddressOf UpperChanged
udUpperLim3.Value = My.Settings.Alarm3

Dim udUpperLim5 As New NumericUpDown
udUpperLim5.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim5)
AddHandler udUpperLim5.ValueChanged, AddressOf UpperChanged
udUpperLim5.Value = My.Settings.Alarm5

推荐答案

Ju ******** @ home.net 写道:

我正在添加一堆具有以下代码的控件。


问题1:当程序流程传递给UpperChanged时当我点击它时,

控件名称未定义。当我进入:


如果udUpperLim1.Value 1那么


我收到一个错误,udUpperLim1是未定义因此我无法获得控件中的

值。


问题2:当我使用sender.tostring时,它返回类名

控件,而不是其指定的名称。我得到了

" System.Windows.Forms.NumericUpDown"而不是udUpperLim1而且我不知道
知道谁被改变了以及前面的问题,即使我是b $ b,我也不能引用它! br />
I am adding a bunch of controls with the code below.

Problem 1: When program flow passes to "UpperChanged" when I click it,
the control name is undefined. When I enter:

If udUpperLim1.Value 1 Then

I get an error that udUpperLim1 is "Not Defined" so I cannot get the
value in the control.

Problem 2: When I use sender.tostring, it returns the class name of
the control, not its assigned name. I get
"System.Windows.Forms.NumericUpDown" and not udUpperLim1 and I don''t
know who was changed and along with the preceding problem, even if I
did, I can''t reference it!



您需要发现控件的Name属性。 :)


看起来很傻,但事实并非如此。你的udUpperLim1变量是一个临时的

引用,很快就超出了范围。您需要命名实际对象。像这样添加

控件:

Dim udUpperLim1 As New NumericUpDown

udUpperLim1.Name =" udUpperLim1"

''剩下的所有......


如果你以后需要掌握它,你可以做

Dim udUL As NumericUpDown

udUL = TryCast(Me.Controls.Item(" udUpperLim1"),NumericUpDown)

if udUL IsNot Nothing Then

''有它...


在一个事件处理程序中,你可以做同样的事情:

Dim udUL As NumericUpDown

udUL = TryCast(发件人,NumericUpDown)

如果udUL IsNot Nothing那么

''发件人是NumericUpDown

如果udUL.Name =" udUpperLim1"然后

''就是那个...


You need to discover the Name property of a control. :)

It may seem silly, but it is not. Your udUpperLim1 variable is a temporary
reference, and soon out of scope. You need to name the actual object. Add the
control like this:

Dim udUpperLim1 As New NumericUpDown
udUpperLim1.Name = "udUpperLim1"
'' all the rest...

If you need to get hold of it later, you can do
Dim udUL As NumericUpDown
udUL = TryCast(Me.Controls.Item("udUpperLim1"), NumericUpDown)
if udUL IsNot Nothing Then
'' got it...

In an event handler, you can do the same thing:
Dim udUL As NumericUpDown
udUL = TryCast(sender, NumericUpDown)
if udUL IsNot Nothing Then
'' sender is a NumericUpDown
If udUL.Name = "udUpperLim1" Then
'' it is that one...



问题1:当程序流程转到 ; UpperChanged"当我点击它时,
Problem 1: When program flow passes to "UpperChanged" when I click it,

控件名称未定义。当我进入:


如果udUpperLim1.Value 1那么


我收到一个错误,udUpperLim1是未定义所以我无法获得控件中的

值。
the control name is undefined. When I enter:

If udUpperLim1.Value 1 Then

I get an error that udUpperLim1 is "Not Defined" so I cannot get the
value in the control.



这是因为在你的代码中你没有给控件一个ID。你的命名

持有控件但没有设置ID属性的变量。


Dim udUpperLim1 As New NumericUpDown

' '**************************

''设置控件的ID属性

udUpperLim1.ID =" udUpperLim1"

''**************************

udUpperLim1.SetBounds(228,iTop,45,20)

iTop + = 20

Me.Controls.Add(udUpperLim1)

AddHandler udUpperLim1.ValueChanged,AddressOf UpperChanged

udUpperLim1.Value = My.Settings.Alarm1

This is because in your code your not giving the control an ID. Your naming
the variable that is holding the control but not setting the ID property.

Dim udUpperLim1 As New NumericUpDown
''**************************
''Set the ID property of the control
udUpperLim1.ID = "udUpperLim1"
''**************************
udUpperLim1.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim1)
AddHandler udUpperLim1.ValueChanged, AddressOf UpperChanged
udUpperLim1.Value = My.Settings.Alarm1


问题2:当我使用发件人时。 tostring,它返回

控件的类名,而不是其指定的名称。我得到了

" System.Windows.Forms.NumericUpDown"而不是udUpperLim1而且我不知道
知道谁被改变了以及前面的问题,即使我确实没有b $ b,我也不能参考它!
Problem 2: When I use sender.tostring, it returns the class name of the
control, not its assigned name. I get
"System.Windows.Forms.NumericUpDown" and not udUpperLim1 and I don''t
know who was changed and along with the preceding problem, even if I
did, I can''t reference it!



这是因为你正在调用.ToString(),它返回字符串中的对象类型

...尝试使用sender.ID


- Adam

This is because you are calling .ToString() which returns the objects type
in a string ... try using sender.ID

- Adam




" SneeKeeFahk" < NO **** @ DONTSPAM.com写信息

新闻:uz ************** @ TK2MSFTNGP03.phx.gbl ...

"SneeKeeFahk" <NO****@DONTSPAM.comwrote in message
news:uz**************@TK2MSFTNGP03.phx.gbl...

>问题1:当程序流程传递给UpperChanged时当我点击它时,
控件名称是未定义的。当我进入:

如果udUpperLim1.Value 1那么

我收到一个错误,udUpperLim1是Not Defined所以我无法在控件中获得
值。
>Problem 1: When program flow passes to "UpperChanged" when I click it,
the control name is undefined. When I enter:

If udUpperLim1.Value 1 Then

I get an error that udUpperLim1 is "Not Defined" so I cannot get the
value in the control.



这是因为在你的代码中你不给控件一个ID。你的

命名持有控件的变量,但没有设置ID

属性。


Dim udUpperLim1 As New NumericUpDown

''**************************

''设置ID属性控制

udUpperLim1.ID =" udUpperLim1"

''********************** ****

udUpperLim1.SetBounds(228,iTop,45,20)

iTop + = 20

Me.Controls.Add( udUpperLim1)

AddHandler udUpperLim1.ValueChanged,AddressOf UpperChanged

udUpperLim1.Value = My.Settings.Alarm1


This is because in your code your not giving the control an ID. Your
naming the variable that is holding the control but not setting the ID
property.

Dim udUpperLim1 As New NumericUpDown
''**************************
''Set the ID property of the control
udUpperLim1.ID = "udUpperLim1"
''**************************
udUpperLim1.SetBounds(228, iTop, 45, 20)
iTop += 20
Me.Controls.Add(udUpperLim1)
AddHandler udUpperLim1.ValueChanged, AddressOf UpperChanged
udUpperLim1.Value = My.Settings.Alarm1


>问题2:当我使用sender.tostring时,它返回
控件的类名,而不是其指定的名称。我得到
System.Windows.Forms.NumericUpDown而不是udUpperLim1而且我不知道谁被改变了以及前面的问题,即使我做了,我也不能参考它!
>Problem 2: When I use sender.tostring, it returns the class name of the
control, not its assigned name. I get
"System.Windows.Forms.NumericUpDown" and not udUpperLim1 and I don''t
know who was changed and along with the preceding problem, even if I
did, I can''t reference it!



这是因为你正在调用.ToString(),它返回字符串中的对象类型

...尝试使用sender.ID


- Adam


This is because you are calling .ToString() which returns the objects type
in a string ... try using sender.ID

- Adam



如果您正在使用ASP,请使用ID属性。如果您使用的是winforms,请使用

Name属性。


LS

If you are doing ASP use the ID property. If you are using winforms use the
Name property.

LS


这篇关于以编程方式添加控件的问题 - 无法获取属性,甚至无法引用控件!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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