Results 1 to 7 of 7

Thread: [RESOLVED] Simple Array Question

  1. #1

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Resolved [RESOLVED] Simple Array Question

    Okay, I've looked but I can't work out arrays for textboxes and labels. So, using 2 textboxes, how do I do the following in VB.Net?
    Code:
    For x = 1 to 2
    	if x = 1 then
    	txtbox(x).text = "One"
    	else
    	txtbox(x).text = "Two"
    	end if
    next x
    Any links, help out there? Thanks.

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

    Re: Simple Array Question

    try this:

    vb Code:
    1. dim txtbox() as textbox = {textbox1, textbox2}
    2. For x = 1 to 2
    3.     if x = 1 then
    4.         txtbox(x - 1).text = "One" 'arrays are zero based
    5.     else
    6.         txtbox(x - 1).text = "Two"
    7.     end if
    8. next x

  3. #3

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Re: Simple Array Question

    Yep that works. Thanks a lot! And another question, is this the correct way of doing it in vb.net? I read something about arrays not being offered, or we don't need to use arrays, in vb.net. Something about the controls collection. So, if there is another better way, or more correct way, what is it?

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

    Re: Simple Array Question

    before vb.net you could copy a control, say textbox1 + paste it, then the original textbox would be textbox1(0) + the new textbox would be textbox1(1), but it doesn't work that way in vb.net

  5. #5

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Re: Simple Array Question

    That's true. So my question is, how does it work in vb.net? When I create a textbox and then another textbox I get "TextBox1" and "TextBox2." Is there a way I can reference those numbers that are attached to the name of the textboxes? Or am I suppossed to do it another way? Overall I'd like to learn how to code it the proper way. Thanks for the help.

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

    Re: Simple Array Question

    you can reference them with an array as i showed you in post #2

  7. #7

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Re: Simple Array Question

    you can reference them with an array as i showed you in post #2
    Yeah sorry, that was a poorly worded question on my part. Well ok, I'll do it that way then. Problem solved. Cheers.

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