Results 1 to 12 of 12

Thread: [RESOLVED] Simple Textbox Arrays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    145

    Resolved [RESOLVED] Simple Textbox Arrays

    is there a way of creating an array of textboxes EASILY. iv read some other posts and they all are really complicated. all i need is something simple like this:
    textbox(1)
    textbox(2)
    and then to be able to refer to the numbers through a variable like this:
    i=1
    textbox(i).text="hello world"
    is there an easy way of doing this?

    thanks in advance

  2. #2
    Member
    Join Date
    Oct 2008
    Posts
    40

    Re: Simple Textbox Arrays

    try this:

    Dim textboxes(#) as textbox

    I = (#

    textbox(i).text="hello world"




    (# = What ever number you want. (Dim textboxes(AS many as you want)

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Simple Textbox Arrays

    What specifically have you found to be complicated about TextBox arrays?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    145

    Re: Simple Textbox Arrays

    i just realised i made a big mistake. will the same array work with labels:
    dim variable(#)as labels
    i=(#
    label(i).text="hello world"
    will that work also?
    thanks a lot
    ill try it and get back to u anyway

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    145

    Re: Simple Textbox Arrays

    actually im using v2008 express and the code doesn't work this is what iv got:
    at the very top:
    dim bank(50) as label
    then in a seperate piece of code in a button press i use this:
    dim i as integer =0
    for i =1 to 95
    bank(i).text="hello world"
    next

    it executes but an error occurs when i press the button to run the code
    please help me why wont it work

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Simple Textbox Arrays

    You're specifying 50 as upper bound when you declare the label array, yet your for-loop goes all the way up to 95.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    145

    Re: Simple Textbox Arrays

    its still not working

    should i do this:
    dim var(3) as label = {var1, var2, var3}

    dim i as integer =0
    for i=1 to 3
    var(i).text="hello world"
    next

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Simple Textbox Arrays

    try this:

    vb Code:
    1. Dim lbl(49) As Label
    2. For x As Integer = 0 To 49
    3.      lbl(x) = New Label
    4.      Me.Controls.Add(lbl(x))
    5.      lbl(x).Location = New Point(0, x * lbl(x).Height)
    6.      lbl(x).Text = x.ToString
    7. Next

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Simple Textbox Arrays

    Hey,

    As another alternative, you could use a Generic List of Type Label as follows:

    Code:
            Dim labels As New List(Of Label)
            For x As Integer = 0 To 49
                Dim label As New Label
                label.Location = New Point(0, x * label.Height)
                label.Text = x.ToString
                Me.Controls.Add(label)
            Next
    Hope this helps!!

    Gary

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Simple Textbox Arrays

    Quote Originally Posted by gep13
    Hey,

    As another alternative, you could use a Generic List of Type Label as follows:

    Code:
            Dim labels As New List(Of Label)
            For x As Integer = 0 To 49
                Dim label As New Label
                label.Location = New Point(0, x * label.Height)
                label.Text = x.ToString
                Me.Controls.Add(label)
            Next
    Hope this helps!!

    Gary
    you've declared a list + not used it

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Simple Textbox Arrays

    Ha Ha!!!

    Good point!!! I took the code that you gave and modified it to use a List of TextBox, but forgot to add the label into the List. I was just trying to provide another means of collecting the labels into a List, which doesn't require defines an upperbound to an array.

    Here is the corrected code:

    Code:
            Dim labels As New List(Of Label)
            For x As Integer = 0 To 49
                Dim label As New Label
                label.Location = New Point(0, x * label.Height)
                label.Text = x.ToString
                labels.Add(label)
                Me.Controls.Add(label)
            Next
    Once you have created the list you can enumerate through them as follows:

    Code:
            For Each label As Label In labels
                MessageBox.Show(label.Text)
            Next
    Gary

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    145

    Re: Simple Textbox Arrays

    thanks everyone for your help

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