Visual Basic Question

Discussion in 'Miscellaneous' started by will_iamd, Aug 28, 2012.

  1. I'm trying to write a program (in VB Express 2010), which moves 400 textboxes into position. I'm trying to use this:
    (I left the variable declaration out, but they are declared)
     While n <= 400
                r = 20 * ( n - 1 )
                g = 0
                    While r >= 20
                          g = g + 1
                          r = r - 20
                    End While
                n = n + 1
    End While
    "n" is the textbox number (eg textbox1, textbox2... textboxn)
    "g" is the row the textbox should end up in
    "r" is the column the textbox should end up in

    The textboxes are 20x20, and are being aligned in a 400x400 grid in 20 rows and columns. All the code is in Form1_Load.

    My issue comes, that VB doesn't like me putting "textbox(n)" (I've tried "textboxn" too), as it is trying to read it as a variable called "Textboxn". Is there a way to do this without angering the programming language?

    I'm assuming there'll be somebody who knows Visual Basic on here...
    Thanks :)

    Edit: There may be other minor mistakes in there, because it isn't in a state to test yet, but hopefully it gets the idea across.
  2. Ehm, I'm lost, but I hope someone else will help you. ;)
  3. I haven't tried control arrays with 2010, but here's an article that I think will help:

    The short story is that control arrays aren't as simple in VB.NET as they were with VB6. I'm on my lunch break, but I'll try to go through it more thoroughly and see if I can get it to work.
    will_iamd likes this.
  4. That sounds great, thanks, I'll have a look at the article now.
  5. I've found another way of doing it, which involves a loop that creates the textbox in the code (instead of just creating them in the designer, like I was before), and then positions them before repeating. It works, I hope I haven't wasted any of your lunch break?

    Thanks for the help! :)
  6. Learning is never a waste. Generating the controls at runtime then linking them to your array seems a like good way to do this.

    I'm curious what your code is supposed to do.
  7. Ah, good, and yeah it works much better.

    The code's the start of a program which will kind of be simulating a game of "Tag". ("Tag" being the children's game where they run around where one of them is "It", until the person who is "It" taps somebody else and the tapped person becomes "It" instead etc).

    Each textbox is standing for 1 square metre, and if the person who is "It" is standing in it, the textbox's colour will be red. If another player is standing in it, the colour will be blue.

    The idea is that each player is controlled by a few simple rules (eg "If the person who is "It" comes near me, I need to move away from them"), and therefore it should produce some interesting patterns. I'm seeing how realistic I can make the simulation, from a minimal number of iterated rules.

    So, in theory, the simulation should be driven by the same kind of maths that makes it possible for flocks of birds to fly in formations and change direction etc, which is a kind of maths that I find quite interesting.

    I'm not sure if you were asking for a lecture, but ah well, haha.
  8. That's interesting. If you finish it and feel like sharing, let me know.

    A long time ago I based a similar program on a brain teaser: You have four bugs, each on the corner of a table. Each bug turns towards the one to its right and starts walking towards it. What will be the shape of their paths?

    To solve the problem, I wrote a program that had the "bugs" drop markers. Once I was done, I started experimenting with different starting locations and speeds. It made some interesting designs.
  9. That sounds like it's based on the same maths as what I'm doing. It is really interesting how weird patterns can be formed by the iteration of really simple rules. I don;t know if you've heard of "Chaos Theory", but that's what this branch of maths is called, basically how systems which feedback on themselves behave.
  10. will_iamd likes this.
  11. I haven't even attempted visual basic since this time last year. I've been distracted with JAVA alot more recently.
    will_iamd likes this.
  12. VB is a great starting language(altough more a fan of the c family syntax instead of the vb syntax) loved it for a while hope your program works(out) :)
    will_iamd likes this.
  13. That's really clever, thanks for sharing it.

    Yeah, it's ideal for just messing around on small projects, as opposed to trying to seriously program.