ar81

I am a VB5 programmer.

When I import VB5 code, control arrays do not work.

I have several controls referring to other controls, so not having control arays would make the coding too long.



Re: Visual Basic Express Edition How do I create control arrays in VB2005?

Karl Davies-Barrett - MCT

You don't have these any more, but rejoice you don't need them.

the handles clause at the end of the event can now be multicast, meaning it can hook up to multiple events, like handles button1.click, button2.click, etc

now the sender parameter will tell you who clicked the button.






Re: Visual Basic Express Edition How do I create control arrays in VB2005?

Karl Davies-Barrett - MCT

Thought I'd give you some code tooSmile

Code Snippet

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

If TypeOf sender Is Button Then

Dim btn As Button = CType(sender, Button)

Select Case btn.Name

Case Is = "Button1"

Case Is = "Button2"

End Select

End If

End Sub






Re: Visual Basic Express Edition How do I create control arrays in VB2005?

spotty

More information about using the controls collection

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1018221&SiteID=1

Whereas control arrays enabled you to code something in a event for an array of the same control with the same name but different index values - .NET controls collection allows you to hookup different control names and events to the same method making it very powerful ie. you can hookup a textchnage, checkbox checked and button click all to the same method at design time.

That said - sometimes you want to do something like clear all textboxes or something like that and you can do this by using a loop to generate the control name and then get a reference to the control using the controls collection.

Loads of examples on the forums if you search for control arrays.





Re: Visual Basic Express Edition How do I create control arrays in VB2005?

ar81

Geez, I have a program written in VB5 long ago that has about 70 buttons organized in an array.

I just needed to make a few changes to fit a specific need.

Importing VB5 code brought be problems because of this.

It looks like rewriting code and renaming is going to take forever, given what you say about control arrays.

So I wonder wher could I get a very cheap license of VB5.

I do not trust web retailers so I would like to see if there is any option to get a cheap license of VB5 directly from Microsoft. I know it must be unsupported.

To me it is more cost effective to get a cheap copy than a free VB2005 with all the rewriting I need.





Re: Visual Basic Express Edition How do I create control arrays in VB2005?

Solitaire

Here is a sample making a control array of 4 labels. If you have only a few controls, the commented code listing each control one by one may be used.

For a large number of controls, you may use the For Each loop. The numbered order of the controls is reversed so you may need to start with the upperbound value + 1 and count backwards. The sample code below works as written if you have no more than 4 labels.

============================================================

Public Class Form1

Private LabelArray() As Label = New Label(4) {}

'declare an array of labels

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SetControlArray()

End Sub

Sub SetControlArray()

Dim count As Integer, lbl As Label

count = 5

For Each ctl As Control In Controls

If TypeOf ctl Is Label Then

lbl = CType(ctl, Label)

count -= 1

LabelArray(count) = lbl

End If

Next

'LabelArray(1) = Label1 'This code may be used instead of the For Each block

'LabelArray(2) = Label2

'LabelArray(3) = Label3

'LabelArray(4) = Label4

End Sub

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

'Button1 clears the text in the array of labels

Dim a As Integer

For a = 1 To 4

LabelArray(a).Text = ""

Next

End Sub

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

'Button2 restores the text in the array of labels

Dim a As Integer

For a = 1 To 4

LabelArray(a).Text = "Label" & a.ToString

Next

End Sub

End Class

============================================================

If you have 70 buttons in an array and additional buttons not in the array, then you can add a Tag property to some of them and either include or exclude the ones you need. For example, in the For Each block, you can add a fifth label and exclude it from the array by tagging it and using this code:

count = 5

For Each ctl As Control In Controls

If TypeOf ctl Is Label And CType(ctl.Tag, String) <> "n" Then

lbl = CType(ctl, Label)

count -= 1

LabelArray(count) = lbl

End If

Next






Re: Visual Basic Express Edition How do I create control arrays in VB2005?

JohnWein

Have you tried upgrading the VB5 program I placed a Command button with index (0) on a VB5 form and then loaded 69 more buttons with index(I). I saved and then upgraded the VB5 program to VB2005. This is what I got with a little clean-up.

Code Snippet

Friend Class Form1

Inherits System.Windows.Forms.Form

Private Sub Button_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button.Click

Dim Index As Short = Button.GetIndex(CType(eventSender, Button))

MsgBox("Button " & Index)

End Sub

Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

Dim I As Int16

Dim L, T, W, H As Int32

W = Button(0).Width

H = Button(0).Height

For I = 1 To 69

If I Mod 10 = 0 Then

L = L + W

T = 0

Else

T = T + H

End If

Button.Load(I)

With Button(I)

.Left = L

.Top = T

.Text = "Button(" & I & ")"

.Visible = True

End With

Me.Width = L + W + 6

Me.Height = T + H + 22

Next I

End Sub

End Class

Looks and works the same in both versions of VB.