Clicking labels in an array created at run-time

  • Thread starter Thread starter C
  • Start date Start date
C

C

I create an array of 15 labels lblA(15) which are nicely placed in a
column.

I would like to be able to double click them to allow the user to
change the text. Is it possible in VB.net to have lblA_DoubleClick?
Can I also get the index of the array?

This was working very nicely in VB6, and the code was simple and
clean.
 
Am 31.08.2010 18:48, schrieb C:
I create an array of 15 labels lblA(15) which are nicely placed in a
column.

I would like to be able to double click them to allow the user to
change the text. Is it possible in VB.net to have lblA_DoubleClick?
Can I also get the index of the array?

This was working very nicely in VB6, and the code was simple and
clean.

Moreover it was limited. In VB.Net, you can create any number of
controls at runtime without being forced to create a control
array at design time. By using the Addhandler/Removehandler statement,
this is possible. You can attach/detach event handlers dynamically.
You can have one event handler handle the events of any control and
event (as long as signatures match). You can name each control
individually giving them meaningful names. You can still put all
controls into an array, or none or some of them. You can name the
event handlers like you want.

As to your question: Either use the "Handles" clause or the Addhandler
statement to make a method handle an event. Addhandler can be used
in a loop for all labels. Within the event handler, the 'sender'
argument points to the object raising the event. If you, additionally,
want to get the index, make use of the arrays IndexOf method.
 
Am 31.08.2010 18:48, schrieb C:

Thanks for your quick reply.
Moreover it was limited. In VB.Net, you can create any number of
controls at runtime without being forced to create a control
array at design time. By using the Addhandler/Removehandler statement,
this is possible. You can attach/detach event handlers dynamically.
You can have one event handler handle the events of any control and
event  (as long as signatures match). You can name each control
individually giving them meaningful names. You can still put all
controls into an array, or none or some of them. You can name the
event handlers like you want.

Yes, no one doubts that VB.net is a lot more powerful. But, all this
comes at the cost of more complexity. You are speaking in favour of a
50 kW motorised saw for cutting a cucumber, where people have been
using a simple knife. Your argument is that now I can cut two
cucumbers at a time, may be even two thousand, or something like that.
For engineering uses, this feels like a lot of new things to learn.
Today, I don't even understand this terminology. What are handlers I
still don't know, even though I might be using them.
As to your question: Either use the "Handles" clause or the Addhandler
statement to make a method handle an event.

But the compiler does not know of an event like lblA(3).DoubleClick.
Where do I add this stuff? Can I just create a subroutine
lblA_DoubleClick (sender, e) handles lblA.DoubleClick or something
like that, and write some code in it?
Addhandler can be used
in a loop for all labels. Within the event handler, the 'sender'
argument points to the object raising the event. If you, additionally,
want to get the index, make use of the arrays IndexOf method.

Thanks for your help. I am too primitive a beginner, so it will take
me some effort to understand how to do this.
 
I should have given an example:

Public Class Form1
Private lbl(3) As Label

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)

lbl(0) = Label1
lbl(1) = Label2
lbl(2) = Label3
lbl(3) = Label4

For i = 0 To 3
AddHandler lbl(i).DoubleClick, AddressOf OnLabelDoubleClick
Next
End Sub

Private Sub OnLabelDoubleClick(ByVal sender As Object, ByVal e As EventArgs)

Dim ClickedLabel = DirectCast(sender, Label)
Dim Index = Array.IndexOf(lbl, ClickedLabel)

MsgBox("Text: " & ClickedLabel.Text)
MsgBox("Index: " & Index)


End Sub

End Class

In OnLoad (or in the Load event), I use AddHandler to make sub
OnLabelDoubleClick handle the DoubleClick event of the four labels.
That makes Sub OnLabelDoubleClick an event handler.


As an alternative, if you've placed the labels on the Form using the
designer (like I did), you can drop the For-Next loop, and instead
add the Handles clause:

Private Sub OnLabelDoubleClick(ByVal sender As Object, ByVal e As EventArgs) _
Handles Label1.DoubleClick, Label2.DoubleClick, Label3.DoubleClick, _
Label4.DoubleClick


But with 15 labels, the loop is the quicker alternative, as you seem to
have the labels already in an array.
 
I should have given an example:

   Public Class Form1
      Private lbl(3) As Label

      Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
         MyBase.OnLoad(e)

         lbl(0) = Label1
         lbl(1) = Label2
         lbl(2) = Label3
         lbl(3) = Label4

         For i = 0 To 3
            AddHandler lbl(i).DoubleClick, AddressOf OnLabelDoubleClick
         Next

OK. So this was the way.
      End Sub

      Private Sub OnLabelDoubleClick(ByVal sender As Object, ByVal e As EventArgs)

         Dim ClickedLabel = DirectCast(sender, Label)
         Dim Index = Array.IndexOf(lbl, ClickedLabel)

Does this have to be Dim Index As Short = Array.IndexOf.... ?
         MsgBox("Text: " & ClickedLabel.Text)
         MsgBox("Index: " & Index)

      End Sub

   End Class

In OnLoad (or in the Load event), I use AddHandler to make sub
OnLabelDoubleClick handle the DoubleClick event of the four labels.
That makes Sub OnLabelDoubleClick an event handler.

As an alternative, if you've placed the labels on the Form using the
designer (like I did), you can drop the For-Next loop, and instead

I have this situation also on one form. There I can find the event and
the object from the compiler/IDE, but when the labels are created at
run time, I had no idea of what to do.
add the Handles clause:

   Private Sub OnLabelDoubleClick(ByVal sender As Object, ByVal e As EventArgs) _
      Handles Label1.DoubleClick, Label2.DoubleClick, Label3.DoubleClick, _
      Label4.DoubleClick

But with 15 labels, the loop is the quicker alternative, as you seem to
have the labels already in an array.

Thanks a lot. I think now I will most likely manage to make it work.
Now I also know what else to read up: DirectCast, Protected,
Overrides, MyBase, and other words which will come up while reading
about the former.
 
Am 01.09.2010 12:24, schrieb C:
Does this have to be Dim Index As Short = Array.IndexOf.... ?

No, if you have Option Infer On. And, no, if you have Option Infer Off,
because IndexOf returns an Integer, not Short. As I wrote in a previous
reply, forget Short (16 bits) unless it's really required.
(Option Infer implicitly infers the type of the variable from the
expression assigned. So it's still strongly typed.)
 
Back
Top