Oh Control Arrays where art thou?

B

Bernie

Sorry, but this ia another whine about VB.Net's lack of
Control Arrays. I am new to VB.Net and I'm building an
application that uses variable number of Label controls
that are created at run time. The number of label
controls will vary between 50 and 200. I have created an
array of labels and placed them on the form. It works
great, BUT I have not been able to raise any events for
these controls. I made a small demo with 9 labels
without using a control array and was able to write an
single event handler for the controls. I am including
code snippets of the code I wrote to generate the array
and the code for the event handler. Any suggestions on
how to create a variable number of controls with events
at run time would be most appreciated.

Bernie

Sub BuildBoard(ByVal N As Integer)
'Create an array of labels, Dot(i)
'N number of dots in a row and the number of rows
Dim i As Integer
Dim X0 As Integer, Y0 As Integer
X0 = 30
Y0 = 60
Dim Dot((N + 1) ^ 2 - 1) As Label
For i = 0 To (N + 1) ^ 2 - 1
Dot(i) = New Label
Controls.Add(Dot(i))
Dot(i).Location = New Point(X0+35*(i Mod _
(N+1)),Y0+35*(i\(N + 1)))
Dot(i).Size = New Size(5, 5)
Dot(i).ImageList = imgDot
Dot(i).ImageIndex = 0
Next
End Sub

Sub ProcessClick(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Dot2.Click, Dot3.Click, _
Dot4.Click, Dot5.Click
'Event Handler swaps images in labels Dot2, Dot3, etc.
If DirectCast(sender, Label).ImageIndex = 1 Then
DirectCast(sender, Label).ImageIndex = 0
Else
DirectCast(sender, Label).ImageIndex = 1
End If
End Sub
 
G

Guest

Hi Bernie

Good try

You can try with AddHandler, It is not a disadvantage of having control arrays, Only restriction is you can create control arrays in the design time. However you can create the control arrays through code. Any way i am not going to talk about that.

By removing control array doesn't mean that the advantages of using the control arrays are lost in VB.NET. Also those things can be done in VB.NET

Even you don't need to define the object using With Event. Instead you can declare the object as you normally do for all other class. and use addhandler to assign the event to the object.

For example

Dim lab as new Label(

Addhandler lab.EventName, Addressof <procedure with the same signature

---------------------

In your case, you have the ProcessClick procedure which is going to handle your click event of dot array elements

What you can do is

after you creating a new array element you need to write the following code

AddHandler Dot(i).Click, Addressof ProcessClic

When you click the dot(i) then the processclick will be fired

Where the sender will contain an instance of dot(i)

I hope this could help you up to some extent

Sadha Sivam
Malleable Minds Software Pvt Ltd (India
 
A

Alan Questell

If I understand what you're doing, you can create your own label class
(inheriting from the original label); add an event to this class that will
either do the behavior you want or call another routine like you're doing
below. Then create instances of this class instead of the original label. IF
I understand you correctly.
 
B

Bernie

Alan,
Thanks for the reply. This is also the approach suggested
by Herfried K. Wagner. I have built the project he
referred to and I'm in the process of understanding it.
Bernie
 
G

Guest

Herfried,
Thanks for your reply. The article you suggested was much
more helpful than the one I had found. I have built the
project, it is working and I'm in the process of
understanding it.
Bernie
 
H

Herfried K. Wagner [MVP]

* said:
Thanks for your reply. The article you suggested was much
more helpful than the one I had found. I have built the
project, it is working and I'm in the process of
understanding it.

Glad to hear that. In the next release of VB, control arrays will be
supported in a better way than in the current version.
 
B

Bernie

Sivam, thanks for your reply. In learning VB.Net I'm
trying all the suggested solutions. Based on my
understanding of your reply, I wrote the following code:

+ Windows Form Designer generated code "
Public labX() As Label

Private Sub Form1_Load(ByVal sender As _
System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim i As Integer
ReDim labX(2)
For i = 0 To 2
labX(i) = New Label
Controls.Add(labX(i))
AddHandler labX(i).Click, _
AddressOf Processclick
With labX(i)
.Location = New Point(50, 50 + i * 40)
.Size = New Size(100, 30)
.BackColor = Color.FromArgb(0, 0, 255)
.BorderStyle = BorderStyle.Fixed3D
End With
Next
End Sub

Unfortunately, the AddHandler statement produces an
error. The popup message says: "Name 'Processclick is not
decclared." If I comment-out this statement the code
works fine. I cann't seem to make the event handler work.
Any further thoughts would be most appreciated.
Thanks Bernie
 
C

Chris Dunaway

Unfortunately, the AddHandler statement produces an
error. The popup message says: "Name 'Processclick is not
decclared." If I comment-out this statement the code

How is the Processclick method declared?
 
B

Bernie

Chris,

Perhaps, that is my problem. The only code I wrote was
listed in my reply to Sivam. It was based on a suggestion
in his reply to my original question. I thought that the
Processclick was a built-in method. A more detailed
explanation would be appreciated. I'm continuing my
Control Array quest.

Bernie
 
B

Bernie

Herfried,

I built and have been playing with the ButtonArray
project you you referred me to. My attempts to use the
Item property or the ClickHandler method have been
unsuccessful. I'd really like the event handler to be
available from the test application. That way the
ButtonArray.vb class would be a generic way to add and
remove buttons and the application code would determine
how they were to be used. So far, my book, VB.Net for
Newbies, has not been much assistance. Any further
assistance you could offer would be most appreciated.

Thanks, Bernie
 
B

Bernie

I want to thank everyone for their input and suggestions.
I've gone from stumped to learning and new understanding.
for those who follow in my foot steps looking for Control
Arrays here is the URL for another excellent
article, "Getting Back Your Visual Basic 6.0 Goodies," in
the MSDN library:
http://msdn.microsoft.com/vbasic/using/columns/adventures/
default.aspx?pull=/library/en-
us/dnadvnet/html/vbnet05132003.asp

Bernie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top