Re: How to modify label.text in a dynamically generated label in VB.net
- From: "Stephany Young" <noone@localhost>
- Date: Sat, 30 Dec 2006 21:51:49 +1300
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" <biolley@xxxxxxxxx> wrote in message
news:1167466759.111733.270610@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- References:
- Prev by Date: Re: complie Errors
- Next by Date: Re: complie Errors
- Previous by thread: How to modify label.text in a dynamically generated label in VB.net
- Next by thread: Re: How to modify label.text in a dynamically generated label in VB.net
- Index(es):
Relevant Pages
|