PC Review


Reply
Thread Tools Rate Thread

Clicking an array of labels in VB.net vs VB 6

 
 
C
Guest
Posts: n/a
 
      30th Jul 2010
In VB6 I could click an array of labels with Label1_Click (Index). How
do I do this in VB.net?

In VB.net, I created an array of labels by Dim Label1 as Label, and
then Label1(1) = Label101, Label1(2) = Label102, etc.

I can write Label101_Click, but it would be too messy to copy the code
to 32 labels.

Thanks.
 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      30th Jul 2010
C expressed precisely :
> In VB6 I could click an array of labels with Label1_Click (Index). How
> do I do this in VB.net?
>
> In VB.net, I created an array of labels by Dim Label1 as Label, and
> then Label1(1) = Label101, Label1(2) = Label102, etc.
>
> I can write Label101_Click, but it would be too messy to copy the code
> to 32 labels.
>
> Thanks.


A single event handler can handle multiple controls. Assuming you have
VS2008

1. In the designer, select all of the lables you want to be assigned
to the event.

2. In the properties window, select the little lighting bolt icon.
This will show the events of the controls. In the event you want to
share, type the name of the procedure - such as LabelClick. Hit Enter.

VS will create a sub that looks something like this (I did mine with
buttons - but it works the same for all controls, in fact, as long as
the event shares the same signature - they don't even have to be the
same type of control):

Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click

End Sub

Now, if any of the 3 buttons are clicked, this sub will be called. You
can differentiate which control was clicked using the sender argument.

Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click
MessageBox.Show(DirectCast(sender, Control).Text)
End Sub

And here's another example using different types of controls:

Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Label1.Click
MessageBox.Show(DirectCast(sender, Control).Text)
End Sub

..NET events are much more powerful then VB6 control arrays once they
are understood.

--
Tom Shelton


 
Reply With Quote
 
C
Guest
Posts: n/a
 
      30th Jul 2010
On 30 heinä, 17:57, Tom Shelton <tom_shel...@comcast.invalid> wrote:
> C expressed precisely :
>
> > In VB6 I could click an array of labels with Label1_Click (Index). How
> > do I do this in VB.net?

>
> > In VB.net, I created an array of labels by Dim Label1 as Label, and
> > then Label1(1) = Label101, Label1(2) = Label102, etc.

>
> > I can write Label101_Click, but it would be too messy to copy the code
> > to 32 labels.

>
> > Thanks.

>
> A single event handler can handle multiple controls. *Assuming you have
> VS2008
>
> 1. *In the designer, select all of the lables you want to be assigned
> to the event.
>
> 2. *In the properties window, select the little lighting bolt icon. *
> This will show the events of the controls. *In the event you want to
> share, type the name of the procedure - such as LabelClick. *Hit Enter.
>
> VS will create a sub that looks something like this (I did mine with
> buttons - but it works the same for all controls, in fact, as long as
> the event shares the same signature - they don't even have to be the
> same type of control):
>
> Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click
>
> End Sub


Thanks. This will be less messy, but Handles Label101.Click,
Label102.Click.... upto Label132 will not be too nice. May be I can
put it in 4 subroutines, since the code is going to be hardly about 5
lines.

>
> Now, if any of the 3 buttons are clicked, this sub will be called. *You
> can differentiate which control was clicked using the sender argument.
>
> Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click
> * * * * MessageBox.Show(DirectCast(sender, Control).Text)
> End Sub
>
> And here's another example using different types of controls:
>
> Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click, Label1.Click
> * * * * MessageBox.Show(DirectCast(sender, Control).Text)
> End Sub
>
> .NET events are much more powerful then VB6 control arrays once they
> are understood.


I have started to realise that VB.net in general is quite powerful,
but the learning curve is steep.

What makes this worse is that logic is not always visible. I
particularly have difficultes understanding Graphics objects and how
they are stuck to Forms, etc.

>
> --
> Tom Shelton


 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      30th Jul 2010

>
> What makes this worse is that logic is not always visible. I
> particularly have difficultes understanding Graphics objects and how
> they are stuck to Forms, etc.
>

In my opinion much more then in VB6.

Have a look in Solution Explorer in the top and click that little button
Show all files.
All logic becomes now visible in the YourForm.designer.vb

In VB6 I only saw this in Source Safe as a kind of clumsy resource
incorporated in the code.

Cor

 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      30th Jul 2010
C brought next idea :
> On 30 heinä, 17:57, Tom Shelton <tom_shel...@comcast.invalid> wrote:
>> C expressed precisely :
>>
>>> In VB6 I could click an array of labels with Label1_Click (Index). How
>>> do I do this in VB.net?

>>
>>> In VB.net, I created an array of labels by Dim Label1 as Label, and
>>> then Label1(1) = Label101, Label1(2) = Label102, etc.
>>> I can write Label101_Click, but it would be too messy to copy the code
>>> to 32 labels.

>>
>>> Thanks.

>>
>> A single event handler can handle multiple controls. *Assuming you have
>> VS2008
>>
>> 1. *In the designer, select all of the lables you want to be assigned
>> to the event.
>>
>> 2. *In the properties window, select the little lighting bolt icon. *
>> This will show the events of the controls. *In the event you want to
>> share, type the name of the procedure - such as LabelClick. *Hit Enter.
>>
>> VS will create a sub that looks something like this (I did mine with
>> buttons - but it works the same for all controls, in fact, as long as
>> the event shares the same signature - they don't even have to be the
>> same type of control):
>>
>> Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
>> System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click
>>
>> End Sub

>
> Thanks. This will be less messy, but Handles Label101.Click,
> Label102.Click.... upto Label132 will not be too nice. May be I can
> put it in 4 subroutines, since the code is going to be hardly about 5
> lines.
>


You can always put the labels in a list and then dynamically attach the
handler using AddHandler if you think the other way is to messy...

--
Tom Shelton


 
Reply With Quote
 
C
Guest
Posts: n/a
 
      30th Jul 2010
On 30 heinä, 21:29, "Cor" <n...@none.non> wrote:
> > What makes this worse is that logic is not always visible. I
> > particularly have difficultes understanding Graphics objects and how
> > they are stuck to Forms, etc.

>
> In my opinion much more then in VB6.
>
> Have a look in Solution Explorer in the top and click that little button
> Show all files.
> All logic becomes now visible in the YourForm.designer.vb
>
> In VB6 I only saw this in Source Safe as a kind of clumsy resource
> incorporated in the code.
>
> Cor


I was referring to something else. When trying to learn how to use the
graphics objects, I am not able to figure out how I should Dim them
and where to have e.Graphics.***. I think there is little sense or
logic in that.

g.DrawLine, g.DrawRectangle, etc. is easy to understand, but the rest
does not sound very consistent to me.
 
Reply With Quote
 
C
Guest
Posts: n/a
 
      30th Jul 2010
On 30 heinä, 21:36, Tom Shelton <tom_shel...@comcast.invalid> wrote:
> C brought next idea :
>
>
>
>
>
> > On 30 heinä, 17:57, Tom Shelton <tom_shel...@comcast.invalid> wrote:
> >> C expressed precisely :

>
> >>> In VB6 I could click an array of labels with Label1_Click (Index). How
> >>> do I do this in VB.net?

>
> >>> In VB.net, I created an array of labels by Dim Label1 as Label, and
> >>> then Label1(1) = Label101, Label1(2) = Label102, etc.
> >>> I can write Label101_Click, but it would be too messy to copy the code
> >>> to 32 labels.

>
> >>> Thanks.

>
> >> A single event handler can handle multiple controls. *Assuming you have
> >> VS2008

>
> >> 1. *In the designer, select all of the lables you want to be assigned
> >> to the event.

>
> >> 2. *In the properties window, select the little lighting bolt icon. *
> >> This will show the events of the controls. *In the event you want to
> >> share, type the name of the procedure - such as LabelClick. *Hit Enter.

>
> >> VS will create a sub that looks something like this (I did mine with
> >> buttons - but it works the same for all controls, in fact, as long as
> >> the event shares the same signature - they don't even have to be the
> >> same type of control):

>
> >> Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
> >> System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click

>
> >> End Sub

>
> > Thanks. This will be less messy, but Handles Label101.Click,
> > Label102.Click.... upto Label132 will not be too nice. May be I can
> > put it in 4 subroutines, since the code is going to be hardly about 5
> > lines.

>
> You can always put the labels in a list and then dynamically attach the
> handler using AddHandler if you think the other way is to messy...
>


Sorry I am a beginner, and still do not know what a handler is, what
dynamic attachment is, or how to put labels in lists. I will
appreciate a couple of lines of explanation.

For the moment, I have put 8 labels at a time for each subroutine.

Thanks.

> --
> Tom Shelton- Piilota siteerattu teksti -
>
> - Näytä siteerattu teksti -


 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      30th Jul 2010
After serious thinking C wrote :
> On 30 heinä, 21:36, Tom Shelton <tom_shel...@comcast.invalid> wrote:
>> C brought next idea :
>>
>>
>>
>>
>>
>>> On 30 heinä, 17:57, Tom Shelton <tom_shel...@comcast.invalid> wrote:
>>>> C expressed precisely :

>>
>>>>> In VB6 I could click an array of labels with Label1_Click (Index). How
>>>>> do I do this in VB.net?

>>
>>>>> In VB.net, I created an array of labels by Dim Label1 as Label, and
>>>>> then Label1(1) = Label101, Label1(2) = Label102, etc.
>>>>> I can write Label101_Click, but it would be too messy to copy the code
>>>>> to 32 labels.

>>
>>>>> Thanks.

>>
>>>> A single event handler can handle multiple controls. *Assuming you have
>>>> VS2008

>>
>>>> 1. *In the designer, select all of the lables you want to be assigned
>>>> to the event.

>>
>>>> 2. *In the properties window, select the little lighting bolt icon. *
>>>> This will show the events of the controls. *In the event you want to
>>>> share, type the name of the procedure - such as LabelClick. *Hit Enter.
>>>> VS will create a sub that looks something like this (I did mine with
>>>> buttons - but it works the same for all controls, in fact, as long as
>>>> the event shares the same signature - they don't even have to be the
>>>> same type of control):

>>
>>>> Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
>>>> System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click
>>>> End Sub

>>
>>> Thanks. This will be less messy, but Handles Label101.Click,
>>> Label102.Click.... upto Label132 will not be too nice. May be I can
>>> put it in 4 subroutines, since the code is going to be hardly about 5
>>> lines.

>>
>> You can always put the labels in a list and then dynamically attach the
>> handler using AddHandler if you think the other way is to messy...
>>

>
> Sorry I am a beginner, and still do not know what a handler is, what
> dynamic attachment is, or how to put labels in lists. I will
> appreciate a couple of lines of explanation.
>
> For the moment, I have put 8 labels at a time for each subroutine.
>
> Thanks.



Option Strict On
Option Explicit On

Imports System
' form with 11 lables...
Public Class Form1

Private Sub LabelClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
MessageBox.Show(DirectCast(sender, Control).Text)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
For i As Integer = 1 To 11
Dim lbl As Label = DirectCast(Me.Controls("Label" & i),
Label)
AddHandler lbl.Click, AddressOf LabelClick
Next
End Sub
End Class

--
Tom Shelton


 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      31st Jul 2010
the documentation on MSDN and the according samples about graphics are in my
idea very confusing. It seems that you have to do everything in the method
which handles the paint event. (I know it sounds confusing).

Be aware that you can create a graphic in a form with CreateGraphic while
there are also methods where you can create it from the handler.

If you have created a graphic (an image or a bitmap) then suddenly it
becomes suddenly less difficult.

What the newer VB more confusing are all those classes which you can use and
are in the Net framework. Net. That are not anymore only Microsoft Visual
Basic namespaces, but also many others.

Have a look a this.

http://msdn.microsoft.com/en-us/library/ms229335.aspx

(In fact most is what you called API's in VB6.)

Cor



"C" <(E-Mail Removed)> wrote in message
news:9ce0aee1-4ed4-44db-84bd-(E-Mail Removed)...
> On 30 heinä, 21:29, "Cor" <n...@none.non> wrote:
>> > What makes this worse is that logic is not always visible. I
>> > particularly have difficultes understanding Graphics objects and how
>> > they are stuck to Forms, etc.

>>
>> In my opinion much more then in VB6.
>>
>> Have a look in Solution Explorer in the top and click that little button
>> Show all files.
>> All logic becomes now visible in the YourForm.designer.vb
>>
>> In VB6 I only saw this in Source Safe as a kind of clumsy resource
>> incorporated in the code.
>>
>> Cor

>
> I was referring to something else. When trying to learn how to use the
> graphics objects, I am not able to figure out how I should Dim them
> and where to have e.Graphics.***. I think there is little sense or
> logic in that.
>
> g.DrawLine, g.DrawRectangle, etc. is easy to understand, but the rest
> does not sound very consistent to me.


 
Reply With Quote
 
C
Guest
Posts: n/a
 
      31st Jul 2010
On 31 heinä, 01:16, Tom Shelton <tom_shel...@comcast.invalid> wrote:
> After serious thinking C wrote :
>
>
>
>
>
> > On 30 heinä, 21:36, Tom Shelton <tom_shel...@comcast.invalid> wrote:
> >> C brought next idea :

>
> >>> On 30 heinä, 17:57, Tom Shelton <tom_shel...@comcast.invalid> wrote:
> >>>> C expressed precisely :

>
> >>>>> In VB6 I could click an array of labels with Label1_Click (Index). How
> >>>>> do I do this in VB.net?

>
> >>>>> In VB.net, I created an array of labels by Dim Label1 as Label, and
> >>>>> then Label1(1) = Label101, Label1(2) = Label102, etc.
> >>>>> I can write Label101_Click, but it would be too messy to copy the code
> >>>>> to 32 labels.

>
> >>>>> Thanks.

>
> >>>> A single event handler can handle multiple controls. *Assuming youhave
> >>>> VS2008

>
> >>>> 1. *In the designer, select all of the lables you want to be assigned
> >>>> to the event.

>
> >>>> 2. *In the properties window, select the little lighting bolt icon.. *
> >>>> This will show the events of the controls. *In the event you want to
> >>>> share, type the name of the procedure - such as LabelClick. *Hit Enter.
> >>>> VS will create a sub that looks something like this (I did mine with
> >>>> buttons - but it works the same for all controls, in fact, as long as
> >>>> the event shares the same signature - they don't even have to be the
> >>>> same type of control):

>
> >>>> Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As
> >>>> System.EventArgs) Handles Button3.Click, Button2.Click, Button1.Click
> >>>> End Sub

>
> >>> Thanks. This will be less messy, but Handles Label101.Click,
> >>> Label102.Click.... upto Label132 will not be too nice. May be I can
> >>> put it in 4 subroutines, since the code is going to be hardly about 5
> >>> lines.

>
> >> You can always put the labels in a list and then dynamically attach the
> >> handler using AddHandler if you think the other way is to messy...

>
> > Sorry I am a beginner, and still do not know what a handler is, what
> > dynamic attachment is, or how to put labels in lists. I will
> > appreciate a couple of lines of explanation.

>
> > For the moment, I have put 8 labels at a time for each subroutine.

>
> > Thanks.

>
> Option Strict On
> Option Explicit On
>
> Imports System
> ' form with 11 lables...
> Public Class Form1
>
> * * Private Sub LabelClick(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> * * * * MessageBox.Show(DirectCast(sender, Control).Text)
> * * End Sub
>
> * * Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> * * * * For i As Integer = 1 To 11
> * * * * * * Dim lbl As Label = DirectCast(Me.Controls("Label" & i),
> Label)
> * * * * * * AddHandler lbl.Click, AddressOf LabelClick
> * * * * Next
> * * End Sub
> End Class
>
> --
> Tom Shelton- Piilota siteerattu teksti -
>
> - Näytä siteerattu teksti -


This is too advanced for me. To understand this I will have to figure
four new things. I hope I will do it some day, but for now, it looks
complicated. Thanks.
 
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 labels in an array created at run-time C Microsoft VB .NET 5 1st Sep 2010 12:04 PM
array of labels Wiley Post Microsoft Excel Programming 3 19th Apr 2008 07:31 PM
Re: Sorting a datasheet by clicking column labels Van T. Dinh Microsoft Access 3 5th Jan 2007 07:19 PM
Array of labels?? jaxrpc Microsoft Excel Misc 2 3rd Jan 2004 04:03 PM
Array of Labels Jan Pit Microsoft Access Reports 2 2nd Nov 2003 09:24 AM


Features
 

Advertising
 

Newsgroups
 


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