Re: How to modify label.text in a dynamically generated label in VB.net



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



.



Relevant Pages

  • RE: Unbound Checkboxes need to trigger event
    ... The event doesn't happen because the checkboxes aren't ... Every control has a tag. ... Now look for the option 'Tag'. ... an Option Group can only have one thing selected in it. ...
    (microsoft.public.access.formscoding)
  • Re: Dynamically created controls not accessible on postback
    ... With this type of approach as I have here (it just uses control ... phase.And you probably want the info in Page_Load or event handler at the ... > private void calMonth_DayRender(object sender, ... > to access the dynamically created checkboxes on postback. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Controlarray in VBA
    ... For all the similar controls, set the index of the control in its Tag ... Create event handler for the controls that call function of with the ... > A controlarray with indexed event-handlers would be ideal in a situation ...
    (microsoft.public.office.developer.vba)
  • Re: Setting FaceID on Custom Button
    ... You can, for example, apply a Tag to your control. ... the same way as you set the caption). ... "MyControl Off". ...
    (microsoft.public.word.docmanagement)
  • Re: Function executed twice
    ... I would also like to say, just in case you don't do it already, that using prefixes for your control ids can make it easier when working in code. ... You do NOT need to add anything to the tag in the *.aspx file ... > code-behid I usually click twice in the link/button and that will ADD the ...
    (microsoft.public.dotnet.framework.aspnet)