如何修改VB.net中动态生成的标签中的label.text [英] How to modify label.text in a dynamically generated label in VB.net

查看:139
本文介绍了如何修改VB.net中动态生成的标签中的label.text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问VB2005中动态生成的

对象的属性时遇到问题。有人可以帮忙吗?

简而言之:

我的应用创建了相同数量的复选框和标签,共享

相同的标签号。 (我认为这可能会有所帮助)

复选框名称是chkCancel的串联。和
代表它们的创建顺序:

chkCancel0(Tag = 0)

chkCancel1(Tag = 1)

chkCancel2(标签= 2)

等...


标签也一样

lblLabel0(Tag = 0)

lblLabel1(Tag = 1)

lblLabel2(Tag = 2)

等。 。

创建标签时,其文本属性值为

(lblLabel.text = value)。

我想要什么当我点击

对应的复选框时,显然是文本属性。

我想用一个公共子,处理程序,无论什么,这将工作

所有复选框。

我的问题是如何访问动态

创建标签的标签文本属性。

伪代码:

lableTag = sender.tag

如果sender.checked = true则

lblLabel(labelTag).text ="" (这是我遇到麻烦的部分)


我似乎无法引用任何动态创建的对象...

必须对于一个经验丰富的程序员来说非常简单,但不是为了给b
vbnewbie ......这让我疯了!

谢谢

解决方案

您的事件处理程序将是这样的:

Private Sub DynamicCheckBox_CheckChanged(ByVal sender As Object,ByVal e

As EventArgs)


Dim _tag As Object = CType(sender,Control).Tag


如果_tag IsNot Nothing Then

Dim _c As Control = FindControl(Me,GetType(Label),_ tag)

如果_c IsNot Nothing则CType(_c,Label).Text = String.Empty

结束如果


结束子


私有函数FindControl(ByVal start as Control,ByVal type as Type,

标签作为对象)作为控制


为每个_c作为控制在start.Controls

如果_c.GetType()是类型则

如果_c.Tag IsNot Nothing AndAlso _c.Tag = tag则返回_c

否则

如果_c.HasChildren然后

Dim _cc As Control = FindControl(_c,type,tag)

如果_cc IsNot Nothing则返回_cc

结束If

结束如果

下一页


不返回


结束功能

当你动态生成你的CheckBox控件时,订阅

事件处理程序,例如:


AddHandler chkCancel0.CheckChanged,AddressOf

DynamicCheckBox_CheckChanged

AddHandler chkCancel1.CheckChanged,AddressOf

DynamicCheckBox_CheckChanged


请注意,使用相同的处理程序。


另请注意,事件处理程序没有Handles子句。


当任何一个你单击动态CheckBoxes,事件处理程序是

调用,发件人是一个引用单击的CheckBox。然后我们

尝试在表单上的任何地方获得对Label控件的引用,该控件具有匹配的Tag属性。如果我们得到一个,那么我们将它的文本属性设置为

空字符串。


让我们知道你是如何上场的。

" vbnewbie" < bi ***** @ gmail.com在留言中写道

news:11 ********************** @ n51g2000cwc。 googlegr oups.com ...


>我在访问VB2005中动态生成的

对象的属性时遇到问题。有人可以帮忙吗?

简而言之:

我的应用创建了相同数量的复选框和标签,共享

相同的标签号。 (我认为这可能会有所帮助)

复选框名称是chkCancel的串联。和
代表它们的创建顺序:

chkCancel0(Tag = 0)

chkCancel1(Tag = 1)

chkCancel2(标签= 2)

等...


标签也一样


lblLabel0(Tag = 0)

lblLabel1(Tag = 1)

lblLabel2(Tag = 2)

等...


创建标签时,其文本属性值为

(lblLabel.text = value)。


我想做什么,当我点击

对应的复选框时,显然是文本属性。

我想用一个所有复选框的公共子,处理程序,等等,



我的问题是如何动态访问标签文本属性

创建标签。


伪代码:


lableTag = sender.tag

如果sender.checked = true然后

lblLabel(labelTag).text ="" (这是我遇到麻烦的部分)


我似乎无法引用任何动态创建的对象...


对于有经验的程序员来说,这一定非常简单,但不是为了b $ b bb bbnewbie ......而且这让我疯了!

谢谢


我的代码比Stephany Young的代码短:


Private Sub ChkCancel_CheckChanged(ByVal sender As Object,ByVal e as

EventArgs)处理chkCancel0.CheckChanged和chkCancel1.CheckChanged

和chkCancel2.CheckChanged和chkCancel3.CheckChanged

选择Case CType(发送者,控制).Tag

案例0

lblLabel0.Text =""

案例1

lblLabel1.Text =""

案例2

lblLabel2.Text =""

案例3

lblLabel3 .Text =""

结束选择

结束子


当然它会更长,具体取决于你有多少个复选框。

你也可以添加一个错误处理程序,以防CType(发件人,控制).Tag

返回一些不应该的东西。

错误处理程序代码如下所示:

Case Else


MessageBox.Show(" Error!"," Error) !,MessageBoxButton s.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.B utton1)

此代码将在Select和End内的最后一行之后显示

Select


mediocrevbprogrammer.freed


马修,


我的代码比Stephany Young的代码短:


$ b $我很高兴你吞下了一个:-)


它是一个不同的方法,有自己的专业和骗局(与所有

aproaches :-)


然而我有一个小mod


你最好制作label.Text ="" to label.Text = string.empty


因为string.empty是一个常量,并且不需要扫描字符串

表(它更快,更安全的"和oops


问候


米歇尔


" mediocrevbprogrammer.freed" < a ************************* @ gmail.comschreef

bericht新闻:11 ***** *****************@a3g2000cwd.googlegro ups.com ...


我的代码比Stephany Young短's:


Private Sub ChkCancel_CheckChanged(ByVal sender As Object,ByVal e As

EventArgs)处理chkCancel0.CheckChanged和chkCancel1.CheckChanged

和chkCancel2.CheckChanged和chkCancel3.CheckChanged

选择案例CType(发件人,控制).Tag

案例0

lblLabel0。文字=""

案例1

lblLabel1.Text =""

案例2

lblLabel2.Text =""

案例3

lblLabel3.Text =""

结束选择

结束子


当然,根据您拥有的复选框数量,它会更长。

您还可以dd一个错误处理程序,如果CType(发件人,控制).Tag

返回的东西它不应该。

错误处理程序代码如下所示:

Case Else


MessageBox.Show(" Error!"," Error!",MessageBoxButton s.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.B utton1 )

此代码将在选择和结束内的最后一行之后发生

选择


mediocrevbprogrammer.freed



I am having problems accessing properties of dynamically generated
objects in VB2005. Can someone please help?
In a nutshell:
My app creates an equal number of checkboxes and labels that share the
same Tag number. (I thought it might help)
The checkboxes name is a concatenation of "chkCancel" and a number that

represents the order in which they were created:
chkCancel0 (Tag = 0)
chkCancel1 (Tag = 1)
chkCancel2 (Tag = 2)
etc...

The same goes for the labels
lblLabel0 (Tag = 0)
lblLabel1 (Tag = 1)
lblLabel2 (Tag = 2)
etc...
When the label is created, its text property is given a value
(lblLabel.text = value).
What I want to do, is clear that text property when I click on the
corresponding checkbox.
I want to do it with a common sub, handler, whatever, that will work
for all checkboxes.
My problem is how to acces the label text property of a dynamically
created label.
Pseudo code:
lableTag = sender.tag
if sender.checked=true then
lblLabel(labelTag).text="" (this is the part I have trouble with)

I don''t seem to be able to reference any object created dynamically...
It must be very simple for an experienced programmer, but not to
vbnewbie... and it is driving me nuts!
Thanks

解决方案

Your event handler will go something like this:

Private Sub DynamicCheckBox_CheckChanged(ByVal sender As Object, ByVal e
As EventArgs)

Dim _tag As Object = CType(sender, Control).Tag

If _tag IsNot Nothing Then
Dim _c As Control = FindControl(Me, GetType(Label), _tag)
If _c IsNot Nothing Then CType(_c, Label).Text = String.Empty
End If

End Sub

Private Function FindControl(ByVal start As Control, ByVal type as Type,
tag As Object) As Control

For Each _c as Control In start.Controls
If _c.GetType() Is type Then
If _c.Tag IsNot Nothing AndAlso _c.Tag = tag Then Return _c
Else
If _c.HasChildren Then
Dim _cc As Control = FindControl(_c, type, tag)
If _cc IsNot Nothing Then Return _cc
End If
End If
Next

Return Nothing

End Function

When you ''dynamically generate'' your CheckBox controls, subscribe to the
event handler with something like:

AddHandler chkCancel0.CheckChanged, AddressOf
DynamicCheckBox_CheckChanged
AddHandler chkCancel1.CheckChanged, AddressOf
DynamicCheckBox_CheckChanged

etc. Note that the same handler is used.

Note also that the event handler does not have a Handles clause.

When any one of you dynamic CheckBoxes is clicked, the event handler is
called and sender is a reference to the CheckBox that was clicked. We then
attempt to get a reference to a Label control anywhere on the form that has
a matching Tag property. If we get one then we set it''s Text Property to an
empty string.

Let us konw how you get on.
"vbnewbie" <bi*****@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...

>I am having problems accessing properties of dynamically generated
objects in VB2005. Can someone please help?
In a nutshell:
My app creates an equal number of checkboxes and labels that share the
same Tag number. (I thought it might help)
The checkboxes name is a concatenation of "chkCancel" and a number that

represents the order in which they were created:
chkCancel0 (Tag = 0)
chkCancel1 (Tag = 1)
chkCancel2 (Tag = 2)
etc...

The same goes for the labels
lblLabel0 (Tag = 0)
lblLabel1 (Tag = 1)
lblLabel2 (Tag = 2)
etc...
When the label is created, its text property is given a value
(lblLabel.text = value).
What I want to do, is clear that text property when I click on the
corresponding checkbox.
I want to do it with a common sub, handler, whatever, that will work
for all checkboxes.
My problem is how to acces the label text property of a dynamically
created label.
Pseudo code:
lableTag = sender.tag
if sender.checked=true then
lblLabel(labelTag).text="" (this is the part I have trouble with)

I don''t seem to be able to reference any object created dynamically...
It must be very simple for an experienced programmer, but not to
vbnewbie... and it is driving me nuts!
Thanks



My code is shorter than Stephany Young''s:

Private Sub ChkCancel_CheckChanged(ByVal sender As Object, ByVal e As
EventArgs) Handles chkCancel0.CheckChanged And chkCancel1.CheckChanged
And chkCancel2.CheckChanged And chkCancel3.CheckChanged
Select Case CType(sender, Control).Tag
Case 0
lblLabel0.Text=""
Case 1
lblLabel1.Text=""
Case 2
lblLabel2.Text=""
Case 3
lblLabel3.Text=""
End Select
End Sub

Of course it will be longer depending on how many checkboxes you have.
You could also add an error handler in case CType(sender, Control).Tag
returned something it shouldn''t.
The error handler code would look like this:
Case Else

MessageBox.Show("Error!","Error!",MessageBoxButton s.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.B utton1)
This code would go betwwen the last line inside the Select and End
Select

mediocrevbprogrammer.freed


Well Matthew ,

My code is shorter than Stephany Young''s:

i am happy you swallowed bether :-)

It is a different aproach , with its own pro`s and con`s ( as with all
aproaches :-)

However i have one small mod

you would better make label.Text="" to label.Text=string.empty

as string.empty is a constant and doesn`t requir a scan through the string
table ( it is faster , and safer "" vs " " oops )

regards

Michel

"mediocrevbprogrammer.freed" <an*************************@gmail.comschreef
in bericht news:11**********************@a3g2000cwd.googlegro ups.com...

My code is shorter than Stephany Young''s:

Private Sub ChkCancel_CheckChanged(ByVal sender As Object, ByVal e As
EventArgs) Handles chkCancel0.CheckChanged And chkCancel1.CheckChanged
And chkCancel2.CheckChanged And chkCancel3.CheckChanged
Select Case CType(sender, Control).Tag
Case 0
lblLabel0.Text=""
Case 1
lblLabel1.Text=""
Case 2
lblLabel2.Text=""
Case 3
lblLabel3.Text=""
End Select
End Sub

Of course it will be longer depending on how many checkboxes you have.
You could also add an error handler in case CType(sender, Control).Tag
returned something it shouldn''t.
The error handler code would look like this:
Case Else

MessageBox.Show("Error!","Error!",MessageBoxButton s.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.B utton1)
This code would go betwwen the last line inside the Select and End
Select

mediocrevbprogrammer.freed



这篇关于如何修改VB.net中动态生成的标签中的label.text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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