|
-
Feb 11th, 2008, 07:18 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Object Arrays?
In the old VB6 you used to be able to create an object, and make that object apart of an array. At run time you could then create more of that same object, but with different indexes (But still the same name).
So in the end you could end up with an array of tiles if you wanted, by timesing the index by the width of the object for example and creating the object at a specific place.
In .Net object arrays appear to not be supported anymore.
How would one go about creating objects at run time and using some type of integer to refer to them?
I'm attempting to create some sort of Sudoku game, but am stumped at designing the map. The map will not have a static dimension.
-
Feb 11th, 2008, 09:26 AM
#2
Re: Object Arrays?
Dim ob() as Object
Viola, an array of objects.
Take a look at ArrayLists and HashTables though, they might be more of what you're looking for. They're Arrays on steroids.
-
Feb 11th, 2008, 10:00 AM
#3
Re: Object Arrays?
If you're using .Net 2.0 or greater you should use a generic collection like List(Of T) rather than an ArrayList.
-
Feb 11th, 2008, 06:57 PM
#4
Re: Object Arrays?
What you're talking about are "control arrays", not object arrays. Arrays are supported in code just the same as they were in VB6, and probably more so because the Array class provides quite a few useful members. What no longer exists is design-time support for control arrays. This topic has been discussed at great length on this forum, on the MSDN Web site and elsewhere on the Web. The design-time support is no longer required because of the way VB, VSand .NET handles events. You can create arrays of controls in code just as you can create arrays of anything in code so, from that point of view, control arrays are still supported.
If you want to layout multiple controls on a form then you should be using a TableLayoutPanel anyway. That's another example of why control array design-time support is obsolete. What you were going to use it for is simply not needed.
-
Feb 12th, 2008, 03:22 AM
#5
Thread Starter
Fanatic Member
Re: Object Arrays?
Well of course there's going to be an array with 2 separate dimensions, to hold all the data.
The text boxes are just to display the data and so that the user can input data (to win the game).
Does anyone have examples? or specific keywords to search for?
-
Feb 12th, 2008, 03:36 AM
#6
Re: Object Arrays?
If you want a 2D array of TextBoxes then create a 2D array of TextBoxes. Add a TableLayoutPanel to your form and use it to lay out all your TextBoxes. You can then refer to each TextBox by its coordinates within the TLP, using the GetControlFromPosition and GetPositionFromControl methods, or you can create a 2D array in code, e.g.
vb.net Code:
Private textBoxes As TextBox(,) Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.textBoxes = New TextBox(,) {{Me.TextBox0_0, Me.TextBox0_1}, _ {Me.TextBox1_0, Me.TextBox1_1}} For row As Integer = 0 To Me.textBoxes.GetUpperBound(0) For column As Integer = 0 To Me.textBoxes.GetUpperBound(1) MessageBox.Show(Me.textBoxes(row, column).Name, row & " " & column) Next column Next row End Sub
-
Mar 21st, 2008, 12:17 PM
#7
Thread Starter
Fanatic Member
Re: Object Arrays?
Hmmm, when trying this code, i get a syntax error "'TextBox0_0' is not a member of 'WindowsApplication1.Form1'."
It's inside the { on the word: Me.TextBoxx_x
-
Mar 21st, 2008, 02:02 PM
#8
Re: Object Arrays?
Then you simply havent got a TextBox by that name on your form.
-
Mar 21st, 2008, 02:23 PM
#9
Thread Starter
Fanatic Member
Re: Object Arrays?
Doesn't that defeat the purpose?
Why bother writing up this code, if i have to draw all the objects any way?
No name that i've tried works. I've created a textbox and tried the following names:
"TextBox", "TextBox0_0", "TextBox0", "TextBox0_1".
Same result with all of them, unless i use one of the names in the array's declaration.
If i take the "Me." out, it just says it's not declared...
Why should i have to declare ALL the objects i'm going to use? This is a bit silly if i don't know how many there will be.
Also, when i do get this working, how can i get the Click event, or TextChanged event triggered, and the index of which Textbox into an integer (or 2 integers in this case, one for each of the dimensions)?
Thanks =).
Last edited by Slyke; Mar 21st, 2008 at 02:30 PM.
-
Mar 21st, 2008, 04:23 PM
#10
Re: Object Arrays?
Why not generate a whole bunch of textboxes, add it to your container control? Then you can either store all the names or just store the textboxes themselves in your collection.
I may have gotten this wrong though as I didn't fully understand your last post - you don't want to declare all the objects you want to use. I don't see how you would have done it any other way even in VB6. You still need an instance of the control to work with.
-
Mar 22nd, 2008, 03:20 AM
#11
Thread Starter
Fanatic Member
Re: Object Arrays?
Well, in VB6, i could add a new instance of an object into the array with code, while the app is running. I just set the Index to 0 of a particular object, then i could design code that adds more of that object (It inherits the same properties as the original object), the only difference is that it has a different index, which is used to tell the difference between the objects. This is also how events are handled.
Code:
' There's one textbox on the form called "TextBox", it's index value is set to "0".
' There's one command button on the form called "Command1".
Dim TxtBox As Integer
Private Sub Command1_Click()
TxtBox = TxtBox + 1
Load TextBox(TxtBox)
TextBox(TxtBox).Left = TextBox(TxtBox - 1).Left + 250
TextBox(TxtBox).Top = TextBox(TxtBox - 1).Top + 250
TextBox(TxtBox).Visible = True
End Sub
Private Sub TextBox_Click(Index As Integer)
MsgBox Index
End Sub
In VB.Net it appears you have to declare every instance of the object in code before its running. Also it seems that in the event, you need to specify ALL the objects that that event will handle, if say i had an array with 1000 textboxes, that line of code will get very long. Also what i could do in 3 lines of code in VB6, now it appears to be about 10 lines minimum. I'm all for this, it means more control, but maybe i don't need more control sometimes, and it can't be done the old way any more.
In VB 6 one event handled every object in the array, and more objects could be added at run time.
Last edited by Slyke; Mar 22nd, 2008 at 03:43 AM.
-
Mar 22nd, 2008, 03:54 AM
#12
Re: Object Arrays?
VB.Net is superior to VB6, no questions. The question is that in VB.Net there are many ways to achieve the goal. I don’t know exactly what is that you want to do, but JM’s suggestion using TableLayoutPanel is more efficient I think; it solves the design issue automatically.
Now, in VB.Net you can declare a control in runtime and add it to an array or to a list. Also you can add a handler for what ever event you want. I don’t know why you need the second array or two dimensional array but what I understand you have some data that is related to the text box than why not use the “Tag” property of the textbox control and save there whatever object /data you want.
As you can see there are so many ways of doing this but I am not sure which way is the best since I don’t have the clear picture of what you want to do.
-
Mar 22nd, 2008, 04:56 AM
#13
Re: Object Arrays?
 Originally Posted by Slyke
Doesn't that defeat the purpose?
Why bother writing up this code, if i have to draw all the objects any way?
No name that i've tried works. I've created a textbox and tried the following names:
"TextBox", "TextBox0_0", "TextBox0", "TextBox0_1".
Same result with all of them, unless i use one of the names in the array's declaration.
If i take the "Me." out, it just says it's not declared...
Why should i have to declare ALL the objects i'm going to use? This is a bit silly if i don't know how many there will be.
Also, when i do get this working, how can i get the Click event, or TextChanged event triggered, and the index of which Textbox into an integer (or 2 integers in this case, one for each of the dimensions)?
Thanks =).
The whole point is that YOU add the TextBoxes to the form and then YOU create the array and add YOUR TextBoxes to it. If you don't have a TextBox on your form named TextBox0_0 then if course you can't add a TextBox named TextBox0_0 to your array.
If I post EXAMPLE code it's not meant to be copied and pasted into your project and work as is. Your supposed to READ it, UNDERSTAND the principles it's intended to illustrate, then WRITE your own code suitable for your own project. This is why I try not to post code a lot of the time. People don't think when they can just cut and paste. If I provide instructions rather than code then people have no choice but to engage their mind and write their own code.
-
Mar 22nd, 2008, 05:59 AM
#14
Thread Starter
Fanatic Member
Re: Object Arrays?
Ok...
If i could get this translated into VB.Net, then i can do what i'm trying to achieve.
Code:
' There's one textbox on the form called "TextBox1", it's index value is set to "0".
' There's one command button on the form called "Command1".
Dim TxtBox As Integer
Private Sub Command1_Click()
TxtBox = TxtBox + 1
Load TextBox1(TxtBox)
TextBox1(TxtBox).Left = TextBox1(TxtBox - 1).Left + 250
TextBox1(TxtBox).Top = TextBox1(TxtBox - 1).Top + 250
TextBox1(TxtBox).Visible = True
End Sub
Private Sub TextBox1_Click(Index As Integer)
MsgBox Index
End Sub
Once i see how this works, i can then change it to a Picturebox, or any other control that i need something like this for.
-
Mar 22nd, 2008, 02:00 PM
#15
Thread Starter
Fanatic Member
Re: Object Arrays?
I have provided sufficient information to solve this puzzle?
-
Mar 22nd, 2008, 02:43 PM
#16
Fanatic Member
Re: Object Arrays?
vb Code:
Option Strict On
Option Explicit On
Public Class Form1
Private _index As Integer = 2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tb As TextBox = New TextBox
tb.Name = "TextBox" & Me._index.ToString
Dim ctrls() As Control = Me.Controls.Find("TextBox" & (Me._index - 1).ToString, True)
Dim previoustb As TextBox = CType(ctrls(0), TextBox)
tb.Left = previoustb.Left + 20
tb.Top = previoustb.Top + 20
tb.Visible = True
Me.Controls.Add(tb)
Me._index += 1
End Sub
End Class
-
Mar 22nd, 2008, 07:41 PM
#17
Frenzied Member
Re: Object Arrays?
This is the bare basics of what you wanted to accomplish, I don't have the time tonight to build on this anymore. I would suggest that you use the TableLayoutPanel method of arranging the textboxes as jm mentioned. But if not, I think by studying what little code I've shown below you'll be able to complete the thought based on your previous experiences.
This code shows the basics of adding a control at runtime, note that I made no effort to adjust the coordinates of successive controls; therefore, they'll all be added to the exact same location.
You'll need to read up on Tag, AddHandler, AddressOf and Handling the Objects in the sub named TextBoxesClicked
Code:
Public Class Form1
Private listOfTextBoxes As New List(Of TextBox)
Private x As Integer = 0
Private y As Integer = 0
Private someNumber As Integer = 10
Private someOtherNumber As Integer = 20
Private Sub ButtonAddTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAddTextBox.Click
Dim tb As New TextBox
listOfTextBoxes.Add(tb)
AddHandler tb.Click, AddressOf TextBoxesClicked
tb.Tag = listOfTextBoxes.Count
'x and y can be used to position the controls as you suggested in your first post
x = CType(tb.Tag, Integer) * someNumber
y = CType(tb.Tag, Integer) * someOtherNumber
Me.Controls.Add(tb)
End Sub
Private Sub TextBoxesClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("A TextBox was clicked")
End Sub
End Class
The listOfTextBoxes can be used to iterate over the whole list of TextBoxes that you've added to the form, if desired.
Last edited by FourBlades; Mar 22nd, 2008 at 07:46 PM.
-
Mar 23rd, 2008, 03:27 AM
#18
Thread Starter
Fanatic Member
Re: Object Arrays?
The bolded lines are the lines that i have changed.
Code:
Private listOfTextBoxes As New List(Of TextBox)
Private x As Integer = 0
Private y As Integer = 0
Private someNumber As Integer = 10
Private someOtherNumber As Integer = 20
Private Sub ButtonAddTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAddTextBox.Click
Dim tb As New TextBox
listOfTextBoxes.Add(tb)
AddHandler tb.Click, AddressOf TextBoxesClicked
tb.Tag = listOfTextBoxes.Count
tb.Name = "Txtbox" & Format(listOfTextBoxes.Count, "000")
'x and y can be used to position the controls as you suggested in your first post
x = CType(tb.Tag, Integer) * someNumber
y = CType(tb.Tag, Integer) * someOtherNumber
tb.Left = x
tb.Top = y
Me.Controls.Add(tb)
End Sub
Private Sub TextBoxesClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("A TextBox was clicked " & sender.name & " Index = " & sender.tag)
End Sub
Thank you =D, this works wonderfully!
-
Mar 23rd, 2008, 05:43 PM
#19
Frenzied Member
Re: [RESOLVED] Object Arrays?
You should turn on Option Strict by going to:
Tools-Options-Projects and Solutions-VB Defaults and selecting On.
No doubt you'll discover all kinds of errors that indicate non-compliance with excplicit casts, but you'll also learn to program more carefully with a more specific purpose in mind; as well as avoid all kinds of errors lurking in the background waiting for their chance to strike.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|