|
-
Jul 26th, 2006, 04:26 AM
#1
Thread Starter
Member
Last edited by vsugan_work; Jul 26th, 2006 at 05:16 AM.
Sugan
Chennai, INDIA.
-
Jul 26th, 2006, 04:34 AM
#2
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
-
Jul 26th, 2006, 04:40 AM
#3
Thread Starter
Member
Re: [2005] Control Arrays - a twist
 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,
-
Jul 26th, 2006, 04:48 AM
#4
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.
-
Jul 26th, 2006, 05:15 AM
#5
Thread Starter
Member
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,
Last edited by vsugan_work; Jul 26th, 2006 at 05:32 AM.
Sugan
Chennai, INDIA.
-
Jul 26th, 2006, 05:43 AM
#6
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.
-
Jul 26th, 2006, 05:52 AM
#7
Thread Starter
Member
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".
 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,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|