PC Review


Reply
Thread Tools Rate Thread

Clicking labels in an array created at run-time

 
 
C
Guest
Posts: n/a
 
      31st Aug 2010
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.
 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      31st Aug 2010
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.


--
Armin
 
Reply With Quote
 
C
Guest
Posts: n/a
 
      31st Aug 2010
On 31 elo, 20:14, Armin Zingler <az.nos...@freenet.de> wrote:
> 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.


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.
>
> --
> Armin


Thanks for your help. I am too primitive a beginner, so it will take
me some effort to understand how to do this.
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      31st Aug 2010
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.

--
Armin
 
Reply With Quote
 
C
Guest
Posts: n/a
 
      1st Sep 2010
On 31 elo, 22:22, Armin Zingler <az.nos...@freenet.de> wrote:
> 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.
>
> --
> Armin


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.
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      1st Sep 2010
Am 01.09.2010 12:24, schrieb C:
>> 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.... ?


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.)


--
Armin
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clicking an array of labels in VB.net vs VB 6 C Microsoft VB .NET 14 1st Aug 2010 11:13 AM
Clicking on created shortcut triggers reinstallation =?Utf-8?B?QmVuamFtaW4=?= Microsoft Dot NET 4 20th Sep 2007 07:50 AM
Clicking on a URL created in Word =?Utf-8?B?RGVrZQ==?= Microsoft Word Document Management 1 8th Mar 2006 12:29 AM
I Created a new Power Scheme, and it vanished after clicking OK Paul James Chatman Windows XP Help 2 17th Aug 2005 05:18 AM
Comparing ARRAY elements with RANGE data and created a 2nd array JimP Microsoft Excel Worksheet Functions 2 10th Mar 2004 07:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 AM.