Results 1 to 9 of 9

Thread: [RESOLVED] VB and control arrays.

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    Resolved [RESOLVED] VB and control arrays.

    I know control arrays are gone. I don't like that fact, but am choosing to live with it and learn VB 2005. Anyway, I have a program from VB6 that I need to convert. It has 128 control array labels and 128 control array buttons.

    I DO NOT want to create these controls at run time. They're already on the form.

    1. Do I have to list each of the HANDLES separately, like ....HANDLES Label1.click, Label2.click, Label3.click, .... Label128.click?

    2. I figure the easiest way to reference them by an index is to Dim MyLabel() as label = {me.Label1, me.Label2, ..., me.Label128}. Again, do I need to list all 128 individually or is there an easier way?

    3. When I try to create a label array like this, even if I make it public and put it in the declarations section of the form, I still can't access it from each SUB procedure inside that form. Do I need to declare it inside each SUB?

    Greg

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: VB and control arrays.

    If there are only those 128 labels on your form, you can loop trhu the form controls and check for the type of the control. If it is a label then add it to the array.

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: VB and control arrays.

    this is an example of looping through all controls on a form and adding the labels to an array
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim labels As Label()
    3.         Dim i As Integer = -1
    4.  
    5.         Dim ctl As Control = Me.GetNextControl(Me, True)
    6.         Do Until ctl Is Nothing
    7.             If TypeOf (ctl) Is Label Then
    8.                 i += 1
    9.                 ReDim Preserve labels(i)
    10.                 labels(i) = DirectCast(ctl, Label)
    11.             End If
    12.             ctl = Me.GetNextControl(ctl, True)
    13.         Loop
    14.  
    15.     End Sub
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

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

    Re: VB and control arrays.

    bmahler, are you aware that using your code will create 128 distinct array objects? Use a collection or start with a bigger array and do NOT simply resize it one element at a time.

    If you want to create one event handler for multiple controls then simply select all the controls in the designer, click the Events button in the Properties window, double click the desired event and voila: one method with every selected control in its Handles clause.

    You declare the array at the class level and it is then available throughout the class, whether its public, private or whatever.
    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
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    Re: VB and control arrays.

    Quote Originally Posted by ghall426
    I know control arrays are gone. I don't like that fact, but am choosing to live with it and learn VB 2005. Anyway, I have a program from VB6 that I need to convert. It has 128 control array labels and 128 control array buttons.

    I DO NOT want to create these controls at run time. They're already on the form.

    1. Do I have to list each of the HANDLES separately, like ....HANDLES Label1.click, Label2.click, Label3.click, .... Label128.click?

    2. I figure the easiest way to reference them by an index is to Dim MyLabel() as label = {me.Label1, me.Label2, ..., me.Label128}. Again, do I need to list all 128 individually or is there an easier way?

    3. When I try to create a label array like this, even if I make it public and put it in the declarations section of the form, I still can't access it from each SUB procedure inside that form. Do I need to declare it inside each SUB?

    Greg
    Do EACH label has different event (ex. label1 will do A, label2 will do B) ? I assumed it's not, since you have control array there.

    You should use AddHandler Method for your buttons. Iterate all controls in your form, and add a handler to each of them.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: VB and control arrays.

    Quote Originally Posted by jmcilhinney
    You declare the array at the class level and it is then available throughout the class, whether its public, private or whatever.
    So how can I make this work? I get "Object reference not set to an instance of an object."


    Public Class Form1
    Public MyArray() As Button = {Me.Button1, Me.Button2, Me.Button3}

    Private Sub AnyButton_Click(.........) Handles Button1.Click, Button2.Click, Button3.Click
    Debug.Print(MyArray(1).Text)
    End Sub
    End Class

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

    Re: VB and control arrays.

    That's because when that array is created the Buttons don't exist, so you're populating your array with three null references. The Buttons aren't created until the InitializeComponent method is called in the constructor, which occurs after the declarations of the member variables. In short, you must declare the array at the class level but you cannot populate it until after the InitializeComponent method has been executed. That means writing your own constructor and populating the array at the end of that or, more likely, doing it in the Load event handler:
    VB Code:
    1. Private buttons As Button()
    2.  
    3. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.     Me.buttons = New Button() {Me.Button1, Me.Button2, Me.Button3}
    5. End Sub
    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

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: [RESOLVED] VB and control arrays.

    Thanks to all who responded and answered my questions. Great help as always. Keep it up.

    Greg

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] VB and control arrays.

    Also:

    Quote Originally Posted by ghall426
    I DO NOT want to create these controls at run time. They're already on the form.
    Technicality, but they're actually still created at run-time. The Form Designer simply generates the code to create them, and hides it in the .designer file.

    Enumerating the Controls collection at run-time just to add event handlers is, thus, somewhat perverse.

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