Results 1 to 12 of 12

Thread: HELP putting an integer in the middle of a string

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    4

    HELP putting an integer in the middle of a string

    So I have a bunch of text boxes on a windows form named, "XB1TextBox", "XB2TextBox", "XB3TextBox", etc. all the way up to 30.

    I want to populate them with values from an array, called T, that is 30 members long. So in the form load even I have something like this

    Dim Count As Integer = 1
    For Count = 1 to 30
    XB & Count & TextBox.Text = T(Count - 1 )
    Next

    Line 3 of this code is obviously incorrect... Does anyone have any ideads??? Thank you so much for your help!!

    Kevin

  2. #2
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: HELP putting an integer in the middle of a string

    so what you're trying to do is dynamically assign the textbox you want to populate?

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Re: HELP putting an integer in the middle of a string

    Yes, exactly

  4. #4
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: HELP putting an integer in the middle of a string

    are those textboxes the only textboxes on the form? If so I'm pretty sure you can loop through all the controls you have on the form.

    Now don't quote me on this but you could probably write a nested loop. The outer loop being the "count" loop with the inner being the controls. Then test if the control is blank or not, if it's blank fill it in with the appropriate value and exit the inner loop to move onto the next outer loop iteration. If it isn't blank iterate through the inner loop until you find a control that is blank. This is also assuming everything is in numerical ascending order and that all text boxes are clear before you begin.

    Don't know firsthand if it would work or not because i've personally never done something like that. Maybe someone who has can chime in as well.

    **edit** also, for the control loop, you should make sure to specify you only want textboxes so you don't try and assign a value to something like a button lol

  5. #5
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: HELP putting an integer in the middle of a string

    http://www.vbforums.com/showthread.p...=Loop+controls

    here you go, this was the thread i was thinking of. See if you can make some sense of it and get it to work.

  6. #6
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: HELP putting an integer in the middle of a string

    vb Code:
    1. Dim Count as Integer = 1
    2. Dim currentIndex as Integer = 0
    3. For Count = 1 to 31 'You need to go to 31 because indexes are 0-based
    4.      DirectCast(Me.Controls("XB" & Count.ToString & "TextBox"), TextBox).Text = T(currentIndex.ToString)
    5.      currentIndex+=1
    6. Next

    See if that works. The syntax on the DirectCast may be off a bit, but should be very close. I'm unable to test at the moment

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Re: HELP putting an integer in the middle of a string

    That worked great. Thanks so much for your help!!!

  8. #8
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: HELP putting an integer in the middle of a string

    Good deal. Rate the posts if you'd like and mark thread resolved.

    Glad you got it workin!

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Re: HELP putting an integer in the middle of a string

    Sorry, first time to this forum. Where do you mark thread resolved?

  10. #10
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: HELP putting an integer in the middle of a string

    Oh yea! Welcome!

    Right above the first post, it says "Thread Tools". Click that, and on the dropdown, select "Mark Thread Resolved"

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: HELP putting an integer in the middle of a string

    Quote Originally Posted by stateofidleness View Post
    vb Code:
    1. Dim Count as Integer = 1
    2. Dim currentIndex as Integer = 0
    3. For Count = 1 to 31 'You need to go to 31 because indexes are 0-based
    4.      DirectCast(Me.Controls("XB" & Count.ToString & "TextBox"), TextBox).Text = T(currentIndex.ToString)
    5.      currentIndex+=1
    6. Next

    See if that works. The syntax on the DirectCast may be off a bit, but should be very close. I'm unable to test at the moment
    Just as a side note here, you don't need to cast a control type to a textbox type just to access its .text property. The text property is inherited from the base control class anyway. You only need to cast when you need to access something specific to the given class, that is not inherited from the base class you already have an instance of.

    Also, 1 to 31 is 31 iterations, not 30... 0 to 29 or 1 to 30 is 30 iterations of a loop.

    I would code this out something like this:

    Code:
            For Count As Integer = 1 To 30
                Me.Controls(String.Format("XB{0}TextBox", Count.ToString)).Text = T(Count - 1)
            Next
    where I am assuming T is some 30 element array of strings?

  12. #12
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: HELP putting an integer in the middle of a string

    AH! very cool. Not too good with the String format stuff yet.
    Also, good tip on the casting.

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