Re: How to modify label.text in a dynamically generated label in VB.net
- From: "Tom Leylan" <tleylan@xxxxxxxxxx>
- Date: Sun, 31 Dec 2006 12:29:00 -0500
Personally I would (probably) go one step further. Considering they are
dynamic I wouldn't create a checkbox with a label but rather generate a
hybrid control that contains a checkbox and a label. That frees up the tag
property and permits me to leverage all the stuff available to a newly
defined class.
Additionally it means we don't walk the controls collection (which may
include non-dynamic checkboxes) trying to recognize the checkboxes we want.
We know we are looking for the hybrid objects. A simple (and separate)
collection of the hybrids makes adding and deleting of the objects easy and
insures we have access to each of them through the collection class.
Rather than writing this in the application:
CType(CType(sender, Control).Tag, Label).Text = String.Empty
one should be able to write:
Control.ClearLabel()
"Stephany Young" <noone@localhost> wrote in message
news:%23n6dLwKLHHA.4928@xxxxxxxxxxxxxxxxxxxxxxx
Good call Tom!
I had been thinking that a better way to do it would have been to use a
Dictionary(Of String, Label) where the key was the name of the relevant
CheckBox control and the value was the Label control.
But that way it still would have required a 'get' of the item of the
dictionay before you could access the Label control.
Your way, the code inside the event handler simply becomes:
CType(CType(sender, Control).Tag, Label).Text = String.Empty
assuming of course that the only controls that are subscribed to the event
handler are the CheckBox controls of interest and that the Tag property
for all of them contains a reference to the appropriate Label.
Just goes to show how easy it is for any of us to get into a mind-set and
to not think outside the square.
"Tom Leylan" <tleylan@xxxxxxxxxx> wrote in message
news:OzPurZKLHHA.3552@xxxxxxxxxxxxxxxxxxxxxxx
Stephany... you've done it according to the requirements but I have a
suggestion.
Avoid tagging the checkbox with an arbitrary numeric value and simply use
the .Tag property to reference the label that is associated with it.
Create the label first, tag it with a number (if these numbers are
needed) then create the checkbox and assign the label reference as the
tag. All done.
Tom
"Stephany Young" <noone@localhost> wrote in message
news:uPql$%23%23KHHA.4912@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- Prev by Date: Reading XML Documents
- Next by Date: Re: ExecuteNonQuery - problem
- Previous by thread: Reading XML Documents
- Next by thread: checking for enabled cookies?
- Index(es):
Relevant Pages
|