|
-
Nov 12th, 2008, 09:04 PM
#1
Thread Starter
Junior Member
Similar Game
I'm trying to make a memory game, using pictureboxes.
I've alredy succeded in VB6, now, 3 years have passed and i'm trying to do so in vb 2008 express edition.
The problem is that i'm not able to make arrays with pictureboxes. I can't either use the picturebox.location to reposition the pictureboxes or the picturebox.size to resize the picturebox.
How can i acheive these three goals? can anyone help me?
chacpt
-
Nov 13th, 2008, 02:08 AM
#2
New Member
Re: Similar Game
Well, it's still possible to do all those things.
Dim PBoxArray() as PictureBox = {PBox1, PBox2, ...}
to change the location, use
pboxarray(index of pbox to change).location = new point(x,y)
and the size
pboxarray(index).size = new size(x,y)
-
Nov 13th, 2008, 11:58 AM
#3
Thread Starter
Junior Member
Re: Similar Game
Well, thanks
but i'm still not able to change some properties like backcolor, i'm not able to show or hide a given picturebox using pboxarray(index).hide/show or pboxarray(index).backcolor.
I'm not understanding this. in VB6 it was easy to make things visually...
thanks
chacpt
-
Nov 16th, 2008, 01:03 PM
#4
Thread Starter
Junior Member
Re: Similar Game
I'm really without ideas... i'm not beeing able to mak mi code work...
Code:
Dim picBaixo() As PictureBox = {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 To NumImgs
picCima(i).BackColor(Color.Aqua)
Next
End Sub
if u can help
-
Nov 16th, 2008, 04:25 PM
#5
Re: Similar Game
BackColor is not a method, it is a property. Properties can not be called.
VB.NET Code:
picCima(i).BackColor = Color.Aqua
-
Nov 17th, 2008, 08:38 PM
#6
Thread Starter
Junior Member
Re: Similar Game
When using this code:
Code:
Public Class Form1
Const NumImgs As Integer = 12
Const NumTick As Integer = 5
Dim primeira, acerto As Boolean
Dim NumTicks As Integer
Dim ind As Integer
Dim picCima() As PictureBox
Dim picBaixo() As PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim picCima() As PictureBox = {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
Dim picBaixo() As PictureBox = {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
For i = 0 To NumImgs - 1
picBaixo(i).Visible = False
picCima(i).BackColor = Color.Aqua
Next
primeira = True
End Sub
i already can make all that i want, but when i try to alter some properties in the procedure i use to concentrate the pic clicks i'm not able to do so.
Code:
Private Sub picClick(ByVal index As Integer)
...
picCima(index - 1).Visible = False
picBaixo(index - 1).Visible = True
...
im desperating, when i take one step forward, all of a suden i give two backwards...
-
Nov 18th, 2008, 04:15 AM
#7
Re: Similar Game
You need to handle the pictureboxes Click event. Just because you name a subroutine "picClick", doesnt mean that it'll handle the event. Names of subroutines are meaningless.
You could add eventhandlers at runtime, or you could create it at designtime.
To do it at designtime, double-click any of the pictureboxes in the form and an EventHandler for the Click Event will be generated automatically. Now this EventHandler will only handle the Click Event of the PictureBox you just double-clicked, as you can see at the end of its signature:
To have it handle more PictureBoxes click event, simply add them to that statement:
VB.NET Code:
Handles picC1.Click, picC2.Click, picC3.Click 'Etc etc
After you're done, this method will handle all your pictureboxes Click event. The 'Sender' parameter references the object that raised the event, which in this case would be the PictureBox that was clicked.
-
Nov 18th, 2008, 09:52 AM
#8
Thread Starter
Junior Member
Re: Similar Game
What i did was to in each picture, at the click event i called picclicklike so
Code:
Private Sub picC1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picC1.Click
picClick(1)
End Sub
it gives me a better understanding of the code.
mi problem is that i cannot alter the properties directly... And i don't know why...
-
Nov 18th, 2008, 09:57 AM
#9
Re: Similar Game
Theres nothing wrong with doing it that way, though it may be a little unnecessary as sender can be used directly to reference the clicked PictureBox.
Alter what properties directly? Why could you not do that?
-
Nov 18th, 2008, 10:14 AM
#10
Thread Starter
Junior Member
Re: Similar Game
These properties...
Code:
Private Sub picClick(ByVal index As Integer)
...
picCima(index - 1).Visible = False
picBaixo(index - 1).Visible = True
...
i don't know why i can't alter these properties...
the error that it gives is
NullReferenceException was unhandled
Object reference not set to an instance of an object.
-
Nov 18th, 2008, 10:21 AM
#11
Re: Similar Game
That basically means that in any of those two arrays, there is no object on index - 1, OR, that the actual arrays havent been created yet.
If you look at your Form_Load subroutine, you'll find that you're declaring two local arrays inside the subroutine which you then fill with the pictureboxes on your form. These two arrays have nothing to do with the ones you've declared at class level. Thats where your problem lies.
-
Nov 18th, 2008, 10:38 AM
#12
Thread Starter
Junior Member
Re: Similar Game
This is the basic code i've got.
i think that the arrays are created... and filled...
Code:
Public Class Form1
Const NumImgs As Integer = 12
Const NumTick As Integer = 5
Dim primeira, acerto As Boolean
Dim NumTicks As Integer
Dim ind As Integer
Dim picCima() As PictureBox
Dim picBaixo() As PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim picCima() As PictureBox = {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
Dim picBaixo() As PictureBox = {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
For i = 0 To NumImgs - 1
picBaixo(i).Visible = False
picCima(i).BackColor = Color.Aqua
Next
primeira = True
End Sub
...
Private Sub picClick(ByVal index As Integer)
...
picCima(index - 1).Visible = False
picBaixo(index - 1).Visible = True
...
so i don't know why i can't do that...
-
Nov 18th, 2008, 10:41 AM
#13
Re: Similar Game
Code:
Public Class Form1
Const NumImgs As Integer = 12
Const NumTick As Integer = 5
Dim primeira, acerto As Boolean
Dim NumTicks As Integer
Dim ind As Integer
Dim picCima() As PictureBox
Dim picBaixo() As PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim picCima() As PictureBox = {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
Dim picBaixo() As PictureBox = {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
For i = 0 To NumImgs - 1
picBaixo(i).Visible = False
picCima(i).BackColor = Color.Aqua
Next
primeira = True
End Sub
...
Private Sub picClick(ByVal index As Integer)
...
picCima(index - 1).Visible = False
picBaixo(index - 1).Visible = True
...
You're declaring picCima and picBaixo on two places in your code. When you try to do this:
VB.NET Code:
picCima(index - 1).Visible = False
picBaixo(index - 1).Visible = True
Which one of the declarations do you think youre accessing?
-
Nov 18th, 2008, 10:44 AM
#14
Thread Starter
Junior Member
Re: Similar Game
it should be only one...
but as it is giving one error, i'm accessing the global one, when i should be accessing the private one
-
Nov 18th, 2008, 10:49 AM
#15
Re: Similar Game
There is no global one, nor a private one. The one declared at class level is public, it has also not been instantiated, thus giving you the NullReferenceException.
The one you're declaring in Form_Load, is a local variable and it looses its scope at the end of that method. Instead of declaring new arrays in Form_Load, you should work with the ones you've declared at class level.
-
Nov 18th, 2008, 11:10 AM
#16
Thread Starter
Junior Member
Re: Similar Game
if i do that, it gives the following error:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim picCima() As PictureBox = {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
'Dim picBaixo() As PictureBox = {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
For i = 0 To NumImgs - 1
picBaixo(i).Visible = False
picCima(i).BackColor = Color.Aqua
Next
primeira = True
End Sub
NullReferenceException was unhandled
Object reference not set to an instance of an object.
so i go from one error to the next... without noticing
-
Nov 18th, 2008, 11:18 AM
#17
Re: Similar Game
 Originally Posted by chacpt
if i do that...
Do what exactly?
-
Nov 18th, 2008, 11:33 AM
#18
Thread Starter
Junior Member
Re: Similar Game
if i do what you "told" me to...
as u can see in the code above, i commented the local form_load variables so, i'm using the variables declared at class level... right?
but it gives me the error i told you about...
NullReferenceException was unhandled
Object reference not set to an instance of an object.
and i'm despering... when i look backwards and see that the time i spent in VB6 doing the game (3 or 4 hours with debugging)... i miss VB6
-
Nov 18th, 2008, 11:59 AM
#19
Re: Similar Game
 Originally Posted by chacpt
if i do what you "told" me to...
Yes but what is that? It was this:
Instead of declaring new arrays in Form_Load, you should work with the ones you've declared at class level.
You might think I'm a bit rude not to give you the correct pieces of code to solve your problem, but I'm actually trying to help you understand the problem yourself, so you can fix it yourself.
Right now you've removed the local arrays you declare at Form_Load, thats good, you dont need them.
However the arrays you've declared at class level are not instantiated, they havent been created. You obviously already know how to create new arrays as you did so in Form_Load previously.
-
Nov 18th, 2008, 12:11 PM
#20
Thread Starter
Junior Member
Re: Similar Game
Code:
Public Class Form1
...
Dim picCima() As PictureBox = {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
Dim picBaixo() As PictureBox = {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim picCima() As PictureBox = {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
'Dim picBaixo() As PictureBox = {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
For i = 0 To NumImgs - 1
picBaixo(i).Visible = False
picCima(i).BackColor = Color.Aqua
Next
primeira = True
End Sub
like this...
this is what i did... i understand you... i do that also... i prefer to make someone understand their mistakes and try to make them solve their problems for themselves...
and i'm still not being able to debug the code
as for mi last statment i was only telling of mi experience with VB... as i liked VB6 as a powerfull and easy to use and learn IDE.
Last edited by chacpt; Nov 18th, 2008 at 12:16 PM.
-
Nov 19th, 2008, 06:38 AM
#21
Thread Starter
Junior Member
Re: Similar Game
Like the previous code, the application should work right? i thought it would be... but im not able to find the glitch...
-
Nov 19th, 2008, 07:44 AM
#22
Re: Similar Game
You'd have to tell us whats wrong.
-
Nov 20th, 2008, 10:11 AM
#23
Thread Starter
Junior Member
Re: Similar Game
give me some kind of a clue... i'm not getting to where i'm suposed to go...
i've even tried
Code:
Public Class Form1
...
Dim picCima() As PictureBox = {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
Dim picBaixo() As PictureBox = {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 To NumImgs - 1
picBaixo(i).Visible = False
picCima(i).BackColor = Color.Aqua
Next
primeira = True
End Sub
Last edited by chacpt; Nov 20th, 2008 at 10:16 AM.
-
Nov 20th, 2008, 10:27 AM
#24
Re: Similar Game
Let me rephrase the question:
Are you getting any errors? Is something not happening as it should? Remember I know nothing of your current problem.
-
Nov 20th, 2008, 10:29 AM
#25
Thread Starter
Junior Member
Re: Similar Game
yes, a nullreferenceexception unhandeled
Sorry
-
Nov 20th, 2008, 10:34 AM
#26
Re: Similar Game
Ah yes, you cant assign the array of controls directly in its declaration, try this:
VB.NET Code:
Dim picCima() As PictureBox
Dim picBaixo() As PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
picCima = New PictureBox() {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
picBaixo = New PictureBox() {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
For i = 0 To NumImgs - 1
picBaixo(i).Visible = False
picCima(i).BackColor = Color.Aqua
Next
primeira = True
End Sub
-
Nov 20th, 2008, 12:25 PM
#27
Thread Starter
Junior Member
Re: Similar Game
thanks, this works now... this seems to me a lot like java...
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
|