Moritain Posted May 11, 2006 Posted May 11, 2006 Hello im a college student learning VB.net and im trying to optimize a bit of code and could use some help. I created 81 textboxes in my form named textbox0, textbox1, . . . , textbox80. I also have a 2D array called Game(8,8) as Textbox. Heres my current code: Game(0,0) = Textbox0 Game(0,1) = Textbox1 Game(0,2) = Textbox2 . . . Game(8,8) = Textbox80 I would like a loop that automatically increments the numeric digits at the end of Textbox object name. Something along the lines of: Dim Row As Integer Dim Column As Integer Dim intCount As Integer = 0 For Row = 0 To 8 For Column = 0 To 8 Game(Row, Column) = "txt" & intCount Count += 1 Next Next Copy and paste got me through the first 81 textboxes of this project, but i want to expand the number of textboxes to a 16 x 16 grid, giving 256 total. Ive tried to research this problem, but feel like a a kid that doesn't know that the word "pharmacy" starts with a "P" and his teacher says to look it up in a dictionary, lol. I have found several threads on creating dynamic control instances. All my instances where created and placed in the design view, i just need to bind them to my array. For context, I'm trying to make a very generic Sudoku game ap. Thanks for any help offered. Quote
Cags Posted May 11, 2006 Posted May 11, 2006 Doing what you describe is not actually possible as far as I know. You say you have looked at dynamically creating controls and in my opinion that the answer to your problem. Since reference's to each Textbox is being stored in an array I would be assuming you will never need to actually access the object without accessing it through the array, as such the name of the object is kind of irrelevant, however something like this my work for you. Dim colCount As Integer = 9 Dim rowCount As Integer = 9 Dim index As Integer = 0 Dim grid(colCount, rowCount) As TextBox For i As Integer = 0 To colCount For j As Integer = 0 To rowCount Dim txtTemp As New TextBox txtTemp.Name = "txtBox" & index.ToString() grid(i, j) = txtTemp index += 1 Next Next Quote Anybody looking for a graduate programmer (Midlands, England)?
Moritain Posted May 11, 2006 Author Posted May 11, 2006 Thanks Cags. I had tried a similar set of code before with no success. Your code creates new instances that are not bound to the array. Way data in the array is changed it is not reflected in the 81 textboxes i have displayed on the form. I guess some things just have to be typed out. Quote
Administrators PlausiblyDamp Posted May 11, 2006 Administrators Posted May 11, 2006 The code Cags posted will work fine for creating an array of textboxes - it might help if you show us how you are using this code in respect to your form as there is no reason why you would need to type out all 80+ textboxes (and many, many reasons why you shouldn't). Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Moritain Posted May 16, 2006 Author Posted May 16, 2006 Sorry for the untimely reply, life has been a little crazy lately. Here is the section of code I�m trying to simplify. Private Sub frmSudoku_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Binds each cell of the Game Array to its corresponding Textbox on the form. Game(0, 0) = txt0 Game(0, 1) = txt1 Game(0, 2) = txt2 Game(0, 3) = txt3 Game(0, 4) = txt4 Game(0, 5) = txt5 Game(0, 6) = txt6 Game(0, 7) = txt7 Game(0, 8) = txt8 Game(1, 0) = txt9 Game(1, 1) = txt10 Game(1, 2) = txt11 Game(1, 3) = txt12 Game(1, 4) = txt13 Game(1, 5) = txt14 Game(1, 6) = txt15 Game(1, 7) = txt16 Game(1, 8) = txt17 Game(2, 0) = txt18 Game(2, 1) = txt19 Game(2, 2) = txt20 Game(2, 3) = txt21 Game(2, 4) = txt22 Game(2, 5) = txt23 Game(2, 6) = txt24 Game(2, 7) = txt25 Game(2, 8) = txt26 Game(3, 0) = txt27 Game(3, 1) = txt28 Game(3, 2) = txt29 Game(3, 3) = txt30 Game(3, 4) = txt31 Game(3, 5) = txt32 Game(3, 6) = txt33 Game(3, 7) = txt34 Game(3, 8) = txt35 Game(4, 0) = txt36 Game(4, 1) = txt37 Game(4, 2) = txt38 Game(4, 3) = txt39 Game(4, 4) = txt40 Game(4, 5) = txt41 Game(4, 6) = txt42 Game(4, 7) = txt43 Game(4, 8) = txt44 Game(5, 0) = txt45 Game(5, 1) = txt46 Game(5, 2) = txt47 Game(5, 3) = txt48 Game(5, 4) = txt49 Game(5, 5) = txt50 Game(5, 6) = txt51 Game(5, 7) = txt52 Game(5, 8) = txt53 Game(6, 0) = txt54 Game(6, 1) = txt55 Game(6, 2) = txt56 Game(6, 3) = txt57 Game(6, 4) = txt58 Game(6, 5) = txt59 Game(6, 6) = txt60 Game(6, 7) = txt61 Game(6, 8) = txt62 Game(7, 0) = txt63 Game(7, 1) = txt64 Game(7, 2) = txt65 Game(7, 3) = txt66 Game(7, 4) = txt67 Game(7, 5) = txt68 Game(7, 6) = txt69 Game(7, 7) = txt70 Game(7, 8) = txt71 Game(8, 0) = txt72 Game(8, 1) = txt73 Game(8, 2) = txt74 Game(8, 3) = txt75 Game(8, 4) = txt76 Game(8, 5) = txt77 Game(8, 6) = txt78 Game(8, 7) = txt79 Game(8, 8) = txt80 'Sets the .tag and .tabindex properties for each Textbox and displays a message to the user. For Row = 0 To 8 For Column = 0 To 8 Game(Row, Column).Tag = intNumber Game(Row, Column).TabIndex = intNumber intNumber = intNumber + 1 Next Next sbDisplay.Text = "Please press the Start button." End Sub Like I said before, the code I have works. It just seems to me that there should be some kind of loop for getting the same thing done without manually typing it all out. Cags� code and all other examples I have found to this point create new instances of textboxes with incrementing names. My problem is that I already have the textboxes created in my form and just want to bind each to a location in the array. Thanks again for any help. Moritain Quote
Cags Posted May 17, 2006 Posted May 17, 2006 The example I gave does create new instances but the point is with something of this nature you shouldn't have to create the instances yourself. Its a relatively easy task of creating the entire grid from code, I guess we just don't understand why you would want to sit there and manually position 81+ textboxes when it can be dont through code. I've created a doku based game myself (i say doku as it's dynamic allowing for grids of any size not just 9x9 sudoku), and I personally use GDI+ to render the grid to the user. It can take some time perfecting the algorithm to position each square (especially if you intend to leave a bigger gap every 3 etc.). Even so this is by far a better option in my opinion. Quote Anybody looking for a graduate programmer (Midlands, England)?
Moritain Posted May 17, 2006 Author Posted May 17, 2006 You nailed it on the head Cags. I tried for some time to dynamically created and place my textboxes into the grid leaving a larger space in between each 3x3 grid. At some point quantum physics became necessary and I gave up on that approach, lol. I should mention at this point that I have only taken a single "Intro to VB� course at a junior college. This Sudoku program isn�t even a graded assignment; I just wanted to challenge myself, and get experience that could be used in my upcoming classes. On your suggestion Cags, I will revisit dynamic placement of my textboxes. Thanks. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.