Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.net Control Arrays

Status
Not open for further replies.

rctaubert

Programmer
Aug 30, 2004
4
US
I have move an application from VB6 to VB.net. In VB6 I used Control Arrays (an array of Labels). Through articles I have found on various Forums I have been able to re-create control arrays in VB.net.

I have successfully recoded everything in VB.net but have two questions:

1) Can I add custom properties to a class based on Labels? How?

2) I cannot find a way to have the Label.Click event return the Index of the Label clicked. Any suggestions.

My thanks to anyone who can help me with this.
 
1) Inherit the Label Class
2) Make the "index" the last part of the name of the control then call a function to get the number.

public static int GetIndex(Control ctl)
{
return GetIndex(ctl.Name);
}

public static int GetIndex(object obj)
{
return GetIndex(obj as Control);
}

public static int GetIndex(string name)
{
int i, Index=0;

for (i = name.Length - 1; i >= 0; i--)
{
string sw = name.Substring(i,1);
if (!char.IsDigit(sw[0])) break;
Index = 10 * Index + Int32.Parse(sw);
}
return Index;
}

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
1. If you want to Inerit label and make a custom class, I would just create a new public variable or property of type Integer to contain the index:

Code:
Public Class ExtendedLabel
Inherits Label

Public Index As Integer

End Class

2. In your Click() event, you will get an object called sender passed into your event. You can get the index off of that.
Code:
Dim LocalLabel As ExtendedLabel
LocalLabel = sender

Dim MyIndex As Integer
MyIndex = LocalLabel.Index

 
I want to thank John YingLing and RiverGuy for answering my questions. Unfortunately, neither answer seems to solve my problem.

There are three items below!

1) Can I add custom property to a class based on the Label Control? How??

I already have a class based on Labels that works. I have used the following code to attempt to create a custom property:
********************************************************
' The following property does not show up in the list of properties (ex. lblAvailLetters(i).ColoeIndex)
Private mColorIndex() As Color
Public Property ColorIndex(ByVal Index As Integer) As Color

Get
Return ColorIndex(Index)
End Get

Set(ByVal Value As Color)
mColorIndex(Index) = (Value)
End Set
End Property
***********************************************************
Any suggestions??


2) I cannot find a way to have the Label.Click event return the Index of the Label clicked.

I have found a way to accomplish this using the following code:

CType(CType(sender, System.Windows.Forms.Label).Tag, Short)

3) Now it seems my problem is calling an external subroutine. The following code does not work.

frmCryptogram.AvailLettersClick(CType(CType(sender, System.Windows.Forms.Label).Tag, Short))

The line frmCryptogram.AvailLettersClick is underlined with the following explanation:
Reference to a non-shared member requires an object reference' The subroutine AvailLettersClick is public. Cryptogram.frmCryptogram.AvailLettersClick gives the same error. How do you call an external subroutine from a class????
 
John,

I believe we are talking about the same thing. In VB6 they were created by VB simply by placing multiple controls on your form with the same name.

In VB.net they are no longer available. However, a MS whitepaper described how to create a class that will 'Duplicate' control arrays. This is what I am using.
 
What is a 'Duplicate' control array? I believe there is something in the Visual Basic compatibility libraries for controls arrays, however, I have never used that. I have made arrays of type textbox, or type label, etc.

Can you post a link to the whitepaper so we can see exactly what you are using to accomplish this?
 
Not quite. I just used arrays of controls, AddHandler, RemoveHandler, and names that ended in a number. If I needed the index, I got it by using the trailing digits of the name of the control pased as "sender" in the event handler.
Private rtfPrint() as RichTextBox
Private Sub MyInitialize()
Dim a() As RichTextBox = {rtfPrint0, rtfPrint1, rtfPrint2, rtfPrint3}
rtfPrint = a
End Sub

Private Sub rtfPrint_MouseDown(ByVal sender As System.Object, _
ByVal e As MouseEventArgs) _
Handles rtfPrint0.MouseDown, _
rtfPrint3.MouseDown, _
rtfPrint2.MouseDown, _
rtfPrint1.MouseDown, _
rtfPrint0.MouseDown
Dim index = GetIndex(Sender)
..........
End Sub

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
To John Yingling and RiverGuy

John first:

I need to use For...Next loops to go through the array. I don't think you method will work for me. Thanks for your time and trying to help me. Aside from the index issue do you know how I can add custom properties to my LabelArray?

RiverGuy:

I have posted the URLs for the Whitepaper and the forum where I first heard about it. Control arrays in VB.net have become guite a topic there.

How about you, would you know how to add custom properties to my LabelArray.

Thanks for the time you have invested in this.

To both of you, if I find out how to get the index I'll post it here.



(About the middle of the page.
 
I glanced through that first link. They are building a Collection Class. So really, if you use that first link, you aren't creating an array in your local form, but adding to a collection. If you use their code, then this snippet in their article will do the trick
Public Sub ClickHandler(ByVal sender As Object, ByVal e As _
System.EventArgs)
MessageBox.Show("you have clicked button " & CType(CType(sender, _
System.Windows.Forms.Button).Tag, String))
End Sub


So to manipulate a certain item in the collection, it looks like you could do:
Code:
Dim MyLabel As Label
MyLabel = MyCollectionClass.Item(MyIndex)
MyLabel.Color = Color.Blue

But I'm not sure I would go thorugh all this trouble if it is a one-off application.
 
For...Next"?? Sure.
I use arrays. What makes you think I can not use For/Next or For/Each on arrays? I have had great success with For/Next and arrays. It seems like they were made for each other.

Private rtfPrint() as RichTextBox
Private Sub MyInitialize()
' Construct array of references to cntrols.
Dim a() As RichTextBox = {rtfPrint0, rtfPrint1, rtfPrint2, rtfPrint3}
rtfPrint = a
End Sub

...
Dim I as Integer
For I = 0 To rtfPrint.Length - 1
rtfPrint(I).Enabled = False
Next

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top