Page 1 of 1

Simple Control Array A easy implementation of a VB 6.0 style control array Rate Topic: -----

#1 ohiggins  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 10-April 08

Posted 21 January 2010 - 03:48 AM

For those of you who used VB 6.0 you would remember Control Arrays. For those that did not, it allowed a simple way of creating a control like a text box and then you could use a loop go through all the items to read or write data. This could all be done from the designer interface. On a recent project i needed exactly this function as amazed to find that .net did not support it and when i googled possible solutions they were not that helpful. In amongst this i created a simple yet elegant way to create a control array at run time. Let me point out that this code does not include event handlers. It is designed to show how to implement a basic control array and loop through it. I have included the source project complete (VS 2008) but you will find the form1.vb file in there.

Start a new project and add one button to the form (button1) open the form code window and add
Dim rb_() As RadioButton

above the form1_load sub

put this in the form_load sub
		Dim Array_Size As Integer = 5
		ReDim rb_(Array_Size)
		For i = 0 To Array_Size
			rb_(i) = New RadioButton
			rb_(i).Text = "Radio Button " + i.ToString
			rb_(i).Top = 20 * i
			Me.Controls.Add(rb_(i))
		Next


and this in button 1
MsgBox(rb_(1).Text)


complete code
Public Class Form1
	Dim rb_() As RadioButton
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim Array_Size As Integer = 5
		ReDim rb_(Array_Size)
		For i = 0 To Array_Size
			rb_(i) = New RadioButton
			rb_(i).Text = "Radio Button " + i.ToString
			rb_(i).Top = 20 * i
			Me.Controls.Add(rb_(i))
		Next
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		MsgBox(rb_(1).Text)
	End Sub
End Class



What does it do? The rb_() is "global" to the form. When the form starts it re sizes the rb_() array to the amount of controls we need. It then creates a loop which declares a new control object and assigned it basic values then adds it to the form, loops and repeats. The controls are now part of the form and can be accessed by other controls and objects such as button1. Click_button1 reads the text value of rb_(1) which is the second array item and displays a msgbox. I hopes this helps!
Attached File  Simple_Control_Array.zip (57.82K)
Number of downloads: 1474

This post has been edited by ohiggins: 27 January 2010 - 01:12 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Simple Control Array

#2 zedth2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 121
  • Joined: 14-September 09

Posted 15 November 2010 - 03:07 AM

this is a nice little way to do this but how would we know which radiobutton is checked? I tried doing this...

Dim rb_() As RadioButton
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        [b]MsgBox(rb_().Text)[/b]
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim array_size As Integer = 5
        ReDim rb_(array_size)
        For i = 0 To array_size
            rb_(i) = New RadioButton
            rb_(i).Text = "Radio Button " & i.ToString
            rb_(i).Top = 20 * i
            [b]AddHandler rb_(i).CheckedChanged, AddressOf rb_CheckedChanged[/b]
            Me.Controls.Add(rb_(i))

        Next
    End Sub

    [b]Private Sub rb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        rb_ = sender
    End Sub[/b]



the bold stuff is what i've added in. but in the button click event i'm getting an error "Number of indices is less than the number of dimensions of the indexed array." most obviosly because i don't know the indexed number of the selected checkbox. So how would I fix this?
Was This Post Helpful? 0
  • +
  • -

#3 ohiggins  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 10-April 08

Posted 18 November 2010 - 04:36 AM

You need to use a loop to iterate through the checkboxes to see which on is checked

(I don't have Vstudio on this machine so it is pseudo code sorry)

	For i = 0 To 5 (can't remember the code but it is something like rb_().arraylength or size to get the size dynamiclly)
	    if rb_(i).checked=true then
msgbox("Check box "+i.tostring+" is selected")
end if

	Next


If you have no luck i will try and get to a pc with vs on it.

Cheers
Was This Post Helpful? 0
  • +
  • -

#4 rellison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-August 13

Posted 17 August 2013 - 08:42 PM

View Postohiggins, on 21 January 2010 - 03:48 AM, said:

For those of you who used VB 6.0 you would remember Control Arrays. For those that did not, it allowed a simple way of creating a control like a text box and then you could use a loop go through all the items to read or write data. This could all be done from the designer interface. On a recent project i needed exactly this function as amazed to find that .net did not support it and when i googled possible solutions they were not that helpful. In amongst this i created a simple yet elegant way to create a control array at run time. Let me point out that this code does not include event handlers. It is designed to show how to implement a basic control array and loop through it. I have included the source project complete (VS 2008) but you will find the form1.vb file in there.

Start a new project and add one button to the form (button1) open the form code window and add
Dim rb_() As RadioButton

above the form1_load sub

put this in the form_load sub
		Dim Array_Size As Integer = 5
		ReDim rb_(Array_Size)
		For i = 0 To Array_Size
			rb_(i) = New RadioButton
			rb_(i).Text = "Radio Button " + i.ToString
			rb_(i).Top = 20 * i
			Me.Controls.Add(rb_(i))
		Next


and this in button 1
MsgBox(rb_(1).Text)


complete code
Public Class Form1
	Dim rb_() As RadioButton
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim Array_Size As Integer = 5
		ReDim rb_(Array_Size)
		For i = 0 To Array_Size
			rb_(i) = New RadioButton
			rb_(i).Text = "Radio Button " + i.ToString
			rb_(i).Top = 20 * i
			Me.Controls.Add(rb_(i))
		Next
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		MsgBox(rb_(1).Text)
	End Sub
End Class



What does it do? The rb_() is "global" to the form. When the form starts it re sizes the rb_() array to the amount of controls we need. It then creates a loop which declares a new control object and assigned it basic values then adds it to the form, loops and repeats. The controls are now part of the form and can be accessed by other controls and objects such as button1. Click_button1 reads the text value of rb_(1) which is the second array item and displays a msgbox. I hopes this helps!
Attachment attachment



I have added this to my form and it works fine. The problem I am having is in a Class I have something like this
Public Sub PrintToRxWindow(ByVal sMsg As String, ByVal Index As Integer, Optional ByVal Hostform As Object = Nothing)
        Dim lMaxLen As Long
        If Unloading Then Exit Sub
If Len(Hostform.rtfRX(Index).Text) > lMaxLen Then
            LockWindowUpdate(Hostform.rtfRX(Index).Handle)
            Hostform.rtfRX(Index).Selectionstart = 0
            Hostform.rtfRX(Index).SelectionLength = 500
            Hostform.rtfRX(Index).ReadOnly = False
            Hostform.rtfRX(Index).SelectedText = ""
            Hostform.rtfRX(Index).ReadOnly = True
            LockWindowUpdate(0)
        End If



I keep getting an error that the rtfRX does not exist in the hostform. The hostform is the correct form name of the calling form. I have tried this with 2 other arrays I created of RichTextBoxes but all of them say the same thing.. In the calling form I can see the richtextbox on the form so I know it's getting created. I just need to read it's text in the class file..
Also if I hover over the hostform and and look at the number of controls are on the form it only shows 1 and that is a menu. the other 8 windows that I have created do not show up..

Anyone know what I can do to be able to see it?

Rick
Was This Post Helpful? 0
  • +
  • -

#5 rellison  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-August 13

Posted 17 August 2013 - 09:09 PM

I should have mentioned and shown the code that I am using. I'm using the code to create multiple RichtextBoxes

I dim the RTB's like this
 Public rtfRX() As RichTextBox


and then in a routine I have this:

ReDim rtfRX(NumberofChannels)
        For i = 1 To NumberofChannels
            rtfRX(i) = New RichTextBox
            With rtfRX(i)
                .Text = i
                .Width = 176
                .Height = 19
                .Multiline = False
                .BorderStyle = 2
                .Font = fBold9
                If i = 1 Then
                    .Top = Me.Top - 25
                Else
                    .Top = rtfRX(i - 1).Top + rtfRX(i - 1).Height + 1
                End If
                .Left = ChannelNumber(i).Left + ChannelNumber(i).Width + 2 + ChannelFrequency(i).Width + 2

            End With
            Me.Controls.Add(rtfRX(i))
        Next




Rick
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2886
  • View blog
  • Posts: 9,587
  • Joined: 12-December 12

Posted 22 August 2013 - 01:30 PM

Put
Option Strict On

at the top of your modules so that you can see all errors and warnings.

In particular:
Hostform As Object = Nothing

Are you passing a reference to a Form or, as your post seems to indicate, the name of a form? If you are passing a form reference then I would declare Hostform as a Form rather than an object.

You should also post the full error message you receive.

This post has been edited by andrewsw: 22 August 2013 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2886
  • View blog
  • Posts: 9,587
  • Joined: 12-December 12

Posted 22 August 2013 - 01:37 PM

Post deleted

This post has been edited by andrewsw: 22 August 2013 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1