Results 1 to 18 of 18

Thread: [RESOLVED] To use arrays in VB.NET

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Resolved [RESOLVED] To use arrays in VB.NET

    CIAO

    It was possible and easy to declare arrays in VB6
    For example if I am using a textbox "Text1"
    I copy paste it and an array is "Text1" textbox is declared
    having
    Text1(0)
    Text1(1)
    etc


    I cannot figure out how to use this in VB.NET

    Thanks in advance

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: To use arrays in VB.NET

    AH... what you are asking about are CONTROL ARRAYS (as opposed to simple arrays).

    While control arays are no longer supported in VB.NET... depending on your reason for them, there are ways around it.

    What did you want to be able to get out of using a control array?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: To use arrays in VB.NET

    There are plenty of examples if you search the VB.NET forum for "control array", as this question has came up plenty of times before. The results should have examples of what techgnome has stated.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: To use arrays in VB.NET

    I need insert records in Text box from another file
    I want to use them in a loop
    As the loop goes on
    for example

    Text(a).Text=scr.Readline()
    a++

    Where scr.Readline() is a method of file system and that may not be very concern.
    As the loop goes on the value of variable 'a' increments and then the textbox changes from 1 to 2 and so on.
    If the control array is not possible then I have to explicitly state for every insertion of text with every line and why should I do that
    Last edited by Abbas Haider; Feb 16th, 2006 at 12:05 AM.

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

    Re: To use arrays in VB.NET

    I think that the easiest way is just to add all your TextBoxes to the form in the designer then add a member variable in the code window for the array:
    VB Code:
    1. Private textBoxes() As TextBox = {Me.TextBox1, Me.TextBox2, Me.TextBox3}
    Then you can simply refer to them by their index in that array. Note that arrays zre zero-based in .NET so TextBox1 is at index 0, etc.
    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: To use arrays in VB.NET

    dim textBoxes() as TextBox

    Is textBoxes a keyword or can I use any other word
    I have rename my TextBox1 as tx1

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

    Re: To use arrays in VB.NET

    'textBoxes' is just the identifier I used for the array. You can use whatever name you like. Same for the names of the TextBoxes themselves. I just used what would be the default names assigned by the IDE.
    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
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: To use arrays in VB.NET

    Jim
    Can you give me the whole code?
    I cant figure out how to produce it

    Also it is generating errors :
    Dim tx1() As TextBox=(Me.TextBox1,Me.TextBox2,Me.TextBox3)
    Me.TextBox1, On the comma it is saying: ) expected
    Last edited by Abbas Haider; Feb 16th, 2006 at 01:34 AM.

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

    Re: To use arrays in VB.NET

    You have used parentheses (round brackets) instead of braces {curly brackets} around the three TextBoxes.
    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: To use arrays in VB.NET

    ok
    jim
    But on the design should I put a textbox
    Since I put only one textbox on form named tx1 then used your statement
    Dim tx1() As TextBox={Me.TextBox1,Me.TextBox2,Me.TextBox3}
    but then the error is generated that tx1 is already declared
    Should I use textbox with other name or should I do not put a form on design The code will do all the work?

  11. #11
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: To use arrays in VB.NET

    me.textbox1, me.textbox2, me.textbox3 are referring to three different textbox controls on the form....

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

    Re: To use arrays in VB.NET

    Not like that. In my code 'textBoxes' is the name of the array and 'TextBox1', 'TextBox2' and 'TextBox3' are the names of the TextBoxes that you've added to the form in the designer. If you've added one TextBox named tx1 in the designer then you would have to change my code to look like this:
    VB Code:
    1. Private textBoxes() As TextBox = {Me.tx1}
    Get it? Each TextBox has to have a different name, and those names are what goes inside the braces because that's what you're adding to the array. The array itself must have a different name again. Forget VB6 and ControlArrays. They are a thing of the past. This is VB.NET and this is an array of TextBoxes following the standard rules for .NET arrays.
    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

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: To use arrays in VB.NET

    ok that resolved
    now to use them in loop
    suppose 'i' a variable
    so My code will look like this?
    Private txtBx() As TextBox = {Me.TextBox1, Me.TextBox2, Me.TextBox3}
    loop
    Me.TextBox(i)
    loop

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

    Re: To use arrays in VB.NET

    No. Like I said, forget VB6 and ControlArrays. This is a standard .NET array, which is basically the same as a standard VB6 array as far as I'm aware, other than it's zero-based. The name of the array in your example is 'txtBx', so you need to iterate over the elements of 'txtBx'.
    VB Code:
    1. For i As Integer = 0 To Me.txtBx.GetUpperBound(0) Step 1
    2.     MessageBox.Show(Me.txtBx(i).Name)
    3. Next i
    This code will display MessageBoxes containing the names of the TextBoxes, i.e. TextBox1, TextBox2 and TextBox3.

    Once you've defined that array you can use txtBx(0) and TextBox1 interchangeably, as they are simply two different references to the same object. It's like if you had two children you could refer to 'my first child' and 'my second child' or you could call them by name, but you'd still be referring to the same person, just in two different ways.
    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

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

    Re: To use arrays in VB.NET

    As I said, an array is an array in .NET. If you don't understand arrays then here's something you should read: http://www.homeandlearn.co.uk/NET/nets6p1.html
    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

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: To use arrays in VB.NET

    Quote Originally Posted by jmcilhinney
    As I said, an array is an array in .NET. If you don't understand arrays then here's something you should read: http://www.homeandlearn.co.uk/NET/nets6p1.html
    Its not arrays its control arrays
    I ahve played alot on arrays and your link does not provide info abt control arrays and if u say that arrays and control arrays are same

    I have put three textboxes on my form deign

    and the code is throwing me Nullexception :-

    Public Class Form1

    Inherits System.Windows.Forms.Form
    Private textBoxes() As TextBox = {Me.TextBox1, Me.TextBox2, Me.TextBox3}

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    textBoxes(0).Text = "1"
    textBoxes(1).Text = "2"
    textBoxes(2).Text = "3"
    End Sub
    End Class

    Please give me info abt control arrays

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

    Re: To use arrays in VB.NET

    I will say it again: forget VB6 and ControlArrays. ControlArrays DO NOT EXIST in VB.NET. There is no design time support for control arrays in VB.NET and controls have no Index property. That's what made a ControlArray in VB6 and it does not exist in VB.NET. All you can do is make a standard array of controls, as we're trying to do here, and access the elements by index.

    As for the exception, that's my mistake. You can't initialise the array like that because each of the TextBox variables is Nothing at that point. You would need to populate the array in the constructor at the earliest, but later would be OK too. This is from VB 2005 Express:
    VB Code:
    1. Private textBoxes() As TextBox
    2.  
    3.     Public Sub New()
    4.  
    5.         ' This call is required by the Windows Form Designer.
    6.         InitializeComponent()
    7.  
    8.         ' Add any initialization after the InitializeComponent() call.
    9.         textBoxes = New TextBox() {Me.TextBox1, Me.TextBox2, Me.TextBox3}
    10.     End Sub
    11.  
    12.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    13.         Me.textBoxes(0).Text = "first"
    14.         Me.textBoxes(1).Text = "second"
    15.         Me.textBoxes(2).Text = "third"
    16.     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

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: To use arrays in VB.NET

    Thanks bro
    its resolved

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