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



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









.



Relevant Pages

  • Re: how to have a checkbox change color and text of a label control?
    ... So could you make the label control box change color instead of the font? ... states "Private Sub Form_Current is highlighted yellow ... Name the checkbox and the label with similar names. ...
    (microsoft.public.access.forms)
  • Inconsistent CheckBox Color -- Anyone?
    ... Hi...sorry, over 30 days, so I've included the original thread below, ... but when I move from the control - ... I set vb code so a checkbox on my form enables its label to change ... CheckBox control changes. ...
    (microsoft.public.access.forms)
  • Re: how to have a checkbox change color and text of a label control?
    ... It should not mater if the control is bound or not, you just have to use the ... states "Private Sub Form_Current is highlighted yellow ... Name the checkbox and the label with similar names. ... The name of my label connected to the yes/no checkbox is "CanPlay". ...
    (microsoft.public.access.forms)
  • Re: How to modify label.text in a dynamically generated label in VB.net
    ... Avoid tagging the checkbox with an arbitrary numeric value and simply use ... the .Tag property to reference the label that is associated with it. ... Private Function FindControl(ByVal start As Control, ByVal type as Type, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Is it possible to attach a label to a checkbox for coding purp
    ... Thanks for the additional suggestion - I've read a little about using the tag ... and then you label caption could be anything. ... The purpose of the form is to allow the user to create a custom query ... I'm looking for code that will look at the first checkbox control on the ...
    (microsoft.public.access.forms)