473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Oh Control Arrays where art thou?

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
Nov 20 '05 #1
13 1571
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

Nov 20 '05 #2
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.

"Bernie" <an*******@discussions.microsoft.com> wrote in message
news:27****************************@phx.gbl...
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

Nov 20 '05 #3
* "Bernie" <an*******@discussions.microsoft.com> scripsit:
Sorry, but this ia another whine about VB.Net's lack of
Control Arrays.


Creating Control Arrays in Visual Basic .NET and Visual C# .NET:
<URL:http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchCreatingControlArraysInVisualBasicNETVisualCN ET.asp>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
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
Nov 20 '05 #5
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
-----Original Message-----
* "Bernie" <an*******@discussions.microsoft.com> scripsit:
Sorry, but this ia another whine about VB.Net's lack of Control Arrays.


Creating Control Arrays in Visual Basic .NET and Visual

C# .NET:<URL:http://msdn.microsoft.com/library/en- us/dv_vstechart/html/vbtchCreatingControlArraysInVisualBas
icNETVisualCNET.asp>
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
.

Nov 20 '05 #6
* <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7
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
Nov 20 '05 #8
On Thu, 22 Apr 2004 17:38:45 -0700, Bernie wrote:

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?

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #9
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
-----Original Message-----
On Thu, 22 Apr 2004 17:38:45 -0700, Bernie wrote:

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?

--
Chris

To send me an E-mail, remove the underscores and

lunchmeat from my E-Mailaddress.
.

Nov 20 '05 #10

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
Glad to hear that. In the next release of VB, control arrays will besupported in a better way than in the current version.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
.

Nov 20 '05 #11
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/usi...ns/adventures/
default.aspx?pull=/library/en-
us/dnadvnet/html/vbnet05132003.asp

Bernie
Nov 20 '05 #12
Just a question, but did you use the WithEvents Keyword?

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #13
* Ravichandran J.V. <jv************@yahoo.com> scripsit:
Just a question, but did you use the WithEvents Keyword?


???

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RBohannon | last post by:
Is it possible to create a control array on an unbound form? I would like to be able to loop through a series of unbound text boxes. Thanks.
2
by: Merlin | last post by:
Hi I have a control that allows embeddable editors, so for example I can set a property of controlsEmbeddableEditor =me.TextBox1 on my form, no problem here - what I want to do is the same thing...
3
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
3
by: Robert | last post by:
How can I declare in VB .NET an array of labels for example and afterwards using a FOR structure load every component of the array? I've used this code but it doesn't work: dim x(10) as label...
20
by: samean | last post by:
Hello, Could you explain me,In VB6 using control array,and how about VB.net. Thanks
8
by: Greg | last post by:
In VB6 I made heavy use of control arrays I see they have been 'deprecated' in vb.Net, with a questionable explanation that they are no longer necessary which just addresses the event issue!...
9
by: Michael D. Ober | last post by:
In VB 6, you can create control arrays for your option groups and scan with the following code dim opt as OptionButton for each opt in OptionGroup ' Do something next opt I know VB 2005...
4
by: Arne Beruldsen | last post by:
I'm a recent convert to VB.net from VB6...and I can't believe they don't support control arrays. I have an app that uses a ton of Control Arrays...mostly labels and text boxes. I need some...
13
by: Just_a_fan | last post by:
I am adding a bunch of controls with the code below. Problem 1: When program flow passes to "UpperChanged" when I click it, the control name is undefined. When I enter: If udUpperLim1.Value 1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.