1 Attachment(s)
[2005] Control Arrays - a twist
Hi all,
I went through the control arrays threads in this forum to find a solution for control arrays in the VB project i converted from VB 6.0 to VB 2005.
I Created a sample application to understand the Control Arrays. I had two buttons and two text boxes. I want to group the text boxes alone in to a array.
Code:
Dim fields As TextBox() = {Me.TextBox1, Me.TextBox2}
When i used the above declaration with in a function it worked perfectly. But when i moved to this declaration to the general declaration of the form, it throwed a error. I've attached the screen shot of the error.
So is there a way to declare this grouping in the form declaration, instead of doing it in each and every function in the form.
Pls help in this regard.
Thanks,
Re: [2005] Control Arrays - a twist
The line of code that creates your array is executed before the class constructor, therefore before all the controls on the form are created. At that time those variables are null references. You have to create your array after the controls on the form are create, which means doing it at the end of the constructor or in the Load event handler:
VB Code:
Public Class Form1
Private fields As TextBox()
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.fields = New TextBox() {Me.TextBox1, Me.TextBox2}
End Sub
End Class
Re: [2005] Control Arrays - a twist
Quote:
Originally Posted by jmcilhinney
Me.fields = New TextBox() {Me.TextBox1, Me.TextBox2}
This worked percfectly. i have a doubt now. How many text boxes are created when this application is executed.
I think it creates double the number of text boxes we tried to group. Is it so.
Thanks,
Re: [2005] Control Arrays - a twist
No it isn't so. Your variables are just references to objects, not actual objects themselves. After the line above, Me.TextBox1 and fields(0) are simply two references to the same object. In an analogous situation, "your mother" and "your father's wife" are two ways to refer to the same person, assuming your parents are still a couple of course. ;)
Re:[2005] Control Arrays - a twist
Ok. I have one more question now. I have a function in a module file which is part of the same project. How to access the grouped text boxes in the same manner.
For Ex:
Code:
Module Module1
Sub SetText(ByRef frm As System.Windows.Forms.Form)
frm.Controls("fields(0)").Text = "Sugan Mod 1"
End Sub
End Module
I get the same error as before, when i try to access it from here. How can i do it here.
Thanks,
Re: [2005] Control Arrays - a twist
There is no control on the form with the name "fields(0)" is there. You need to get the name of the control that fields(0) refers to:
VB Code:
frm.Controls(fields(0).Name).Text = "Sugan Mod 1"
Having said that, if you can get the control from the fields array then there's no point getting a reference to the same control from the Controls collection.
Re: [2005] Control Arrays - a twist
I get error when used the code below in Module 1.
Code:
frm.Controls(fields(0).Name).Text = "Sugan Mod 1"
The error is "Name 'fields' is not declared".
Quote:
Originally Posted by jmcilhinney
Having said that, if you can get the control from the fields array then there's no point getting a reference to the same control from the Controls collection.
So can you please suggest me, how to refer the controls in the similar manner (i.e. frm.fields(0).text = "sdgfsdf").
Thanks,