Results 1 to 7 of 7

Thread: [2005] Control Arrays - a twist

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Location
    Chennai, INDIA
    Posts
    58

    Question [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,
    Attached Images Attached Images  
    Last edited by vsugan_work; Jul 26th, 2006 at 05:16 AM.
    Sugan
    Chennai, INDIA.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class Form1
    2.  
    3.     Private fields As TextBox()
    4.  
    5.     Public Sub New()
    6.         ' This call is required by the Windows Form Designer.
    7.         InitializeComponent()
    8.  
    9.         ' Add any initialization after the InitializeComponent() call.
    10.  
    11.         Me.fields = New TextBox() {Me.TextBox1, Me.TextBox2}
    12.     End Sub
    13.  
    14. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Location
    Chennai, INDIA
    Posts
    58

    Question 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,
    Sugan
    Chennai, INDIA.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2006
    Location
    Chennai, INDIA
    Posts
    58

    Question 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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2006
    Location
    Chennai, INDIA
    Posts
    58

    Question 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,
    Sugan
    Chennai, INDIA.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width