Home All Groups Group Topic Archive Search About

control array with code added controls

Author
16 Oct 2006 3:27 AM
David Pick
I've a bunch of labels that i haved added to a form using this code
for i = 1 to 12
     for j = 1 to 12
     dim lbl as new label
     me.controls.add(lbl)
    with lbl
    ...
   end with
   next
next

I would like to have it so that if one of these labels were click it
would change color. Any help would be appreciated. Thanks

- David

Author
16 Oct 2006 5:26 AM
Josip Medved
> I would like to have it so that if one of these labels were click it
> would change color. Any help would be appreciated. Thanks

First create sub:

  Sub SomeClick(sender as object, e as System.EventArgs)
    DirectCast(sender, Label).BackColor = Color.Red
  End Sub


If your for use AddHandler like this:

  for i = 1 to 12
    for j = 1 to 12
      dim lbl as new label
      AddHandler lbl.Click, AddressOf SomeClick
      me.controls.add(lbl)
      with lbl
        ...
      end with
    next
  next


This should do it.


--
Pozdrav,
  Josip Medved
  http://www.jmedved.com
Author
16 Oct 2006 8:32 PM
David Pick
Thanks that worked great. Is there any way to figure out which label
was being click?
Josip Medved wrote:
Show quoteHide quote
> > I would like to have it so that if one of these labels were click it
> > would change color. Any help would be appreciated. Thanks
>
> First create sub:
>
>   Sub SomeClick(sender as object, e as System.EventArgs)
>     DirectCast(sender, Label).BackColor = Color.Red
>   End Sub
>
>
> If your for use AddHandler like this:
>
>   for i = 1 to 12
>     for j = 1 to 12
>       dim lbl as new label
>       AddHandler lbl.Click, AddressOf SomeClick
>       me.controls.add(lbl)
>       with lbl
>         ...
>       end with
>     next
>   next
>
>
> This should do it.
>
>
> --
> Pozdrav,
>   Josip Medved
>   http://www.jmedved.com
Author
17 Oct 2006 11:39 AM
Jay B. Harlow
David,
As the example shows: the "sender" parameter is the label that was clicked.

You could assign the label to a locally typed variable

    ' VS 2005 syntax
>>   Sub SomeClick(sender as object, e as System.EventArgs)
            Dim theLabel As Label = DirectCast(sender, Label)
            If theLabel.BackColor = Color.Black Then
                theLabel.BackColor = Color.Red
            Else
                theLabel.BackColor = Color.Black
            End If
>>   End Sub

When you created the label you could use the Label.Tag (inherited from
Control) property to track extra information about the Label. In addition to
Label.Tag I normally use Inheritance to add additional info, via new fields
& properties to the Label control.

    Public Class CustomLabel
        Inherits Label

        Private m_row As Integer
        Private m_column As Integer

        Public Property Row() As Integer
            Get
                Return m_row
            End Get
            Set(ByVal value As Integer)
                m_row = value
            End Set
        End Property

        Public Property Column() As Integer
            Get
                Return m_column
            End Get
            Set(ByVal value As Integer)
                m_column = value
            End Set
        End Property

    End Class

>>   for i = 1 to 12
>>     for j = 1 to 12
>>       dim lbl as new CustomLabel
>>       AddHandler lbl.Click, AddressOf SomeClick
>>       me.controls.add(lbl)
>>       with lbl
                .Row = i
                .Column = j
>>         ...
>>       end with
>>     next
>>   next


--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"David Pick" <pickda***@gmail.com> wrote in message
news:1161030764.768724.91680@i3g2000cwc.googlegroups.com...
> Thanks that worked great. Is there any way to figure out which label
> was being click?
> Josip Medved wrote:
>> > I would like to have it so that if one of these labels were click it
>> > would change color. Any help would be appreciated. Thanks
>>
>> First create sub:
>>
>>   Sub SomeClick(sender as object, e as System.EventArgs)
>>     DirectCast(sender, Label).BackColor = Color.Red
>>   End Sub
>>
>>
>> If your for use AddHandler like this:
>>
>>   for i = 1 to 12
>>     for j = 1 to 12
>>       dim lbl as new label
>>       AddHandler lbl.Click, AddressOf SomeClick
>>       me.controls.add(lbl)
>>       with lbl
>>         ...
>>       end with
>>     next
>>   next
>>
>>
>> This should do it.
>>
>>
>> --
>> Pozdrav,
>>   Josip Medved
>>   http://www.jmedved.com
>
Author
17 Oct 2006 2:36 PM
David Pick
Thanks!
Jay B. Harlow wrote:
Show quoteHide quote
> David,
> As the example shows: the "sender" parameter is the label that was clicked.
>
> You could assign the label to a locally typed variable
>
>     ' VS 2005 syntax
> >>   Sub SomeClick(sender as object, e as System.EventArgs)
>             Dim theLabel As Label = DirectCast(sender, Label)
>             If theLabel.BackColor = Color.Black Then
>                 theLabel.BackColor = Color.Red
>             Else
>                 theLabel.BackColor = Color.Black
>             End If
> >>   End Sub
>
> When you created the label you could use the Label.Tag (inherited from
> Control) property to track extra information about the Label. In addition to
> Label.Tag I normally use Inheritance to add additional info, via new fields
> & properties to the Label control.
>
>     Public Class CustomLabel
>         Inherits Label
>
>         Private m_row As Integer
>         Private m_column As Integer
>
>         Public Property Row() As Integer
>             Get
>                 Return m_row
>             End Get
>             Set(ByVal value As Integer)
>                 m_row = value
>             End Set
>         End Property
>
>         Public Property Column() As Integer
>             Get
>                 Return m_column
>             End Get
>             Set(ByVal value As Integer)
>                 m_column = value
>             End Set
>         End Property
>
>     End Class
>
> >>   for i = 1 to 12
> >>     for j = 1 to 12
> >>       dim lbl as new CustomLabel
> >>       AddHandler lbl.Click, AddressOf SomeClick
> >>       me.controls.add(lbl)
> >>       with lbl
>                 .Row = i
>                 .Column = j
> >>         ...
> >>       end with
> >>     next
> >>   next
>
>
> --
> Hope this helps
> Jay B. Harlow
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "David Pick" <pickda***@gmail.com> wrote in message
> news:1161030764.768724.91680@i3g2000cwc.googlegroups.com...
> > Thanks that worked great. Is there any way to figure out which label
> > was being click?
> > Josip Medved wrote:
> >> > I would like to have it so that if one of these labels were click it
> >> > would change color. Any help would be appreciated. Thanks
> >>
> >> First create sub:
> >>
> >>   Sub SomeClick(sender as object, e as System.EventArgs)
> >>     DirectCast(sender, Label).BackColor = Color.Red
> >>   End Sub
> >>
> >>
> >> If your for use AddHandler like this:
> >>
> >>   for i = 1 to 12
> >>     for j = 1 to 12
> >>       dim lbl as new label
> >>       AddHandler lbl.Click, AddressOf SomeClick
> >>       me.controls.add(lbl)
> >>       with lbl
> >>         ...
> >>       end with
> >>     next
> >>   next
> >>
> >>
> >> This should do it.
> >>
> >>
> >> --
> >> Pozdrav,
> >>   Josip Medved
> >>   http://www.jmedved.com
> >