Results 1 to 27 of 27

Thread: Similar Game

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Question 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

  2. #2
    New Member
    Join Date
    Nov 2008
    Posts
    1

    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)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    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

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Similar Game

    BackColor is not a method, it is a property. Properties can not be called.
    VB.NET Code:
    1. picCima(i).BackColor = Color.Aqua
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Unhappy 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...

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    VB.NET Code:
    1. Handles picC1.Click
    To have it handle more PictureBoxes click event, simply add them to that statement:
    VB.NET Code:
    1. 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Thumbs up 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...

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Exclamation 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.

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Exclamation 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...

  13. #13
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. picCima(index - 1).Visible = False
    2. picBaixo(index - 1).Visible = True
    Which one of the declarations do you think youre accessing?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    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

  15. #15
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    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

  17. #17
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Similar Game

    Quote Originally Posted by chacpt
    if i do that...
    Do what exactly?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Exclamation 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

  19. #19
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Similar Game

    Quote 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Thumbs up 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.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Unhappy 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...

  22. #22
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Similar Game

    You'd have to tell us whats wrong.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Question 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.

  24. #24
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Question Re: Similar Game

    yes, a nullreferenceexception unhandeled

    Sorry

  26. #26
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Similar Game

    Ah yes, you cant assign the array of controls directly in its declaration, try this:
    VB.NET Code:
    1. Dim picCima() As PictureBox
    2.     Dim picBaixo() As PictureBox
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         picCima = New PictureBox() {picC1, picC2, picC3, picC4, picC5, picC6, picC7, picC8, picC9, picC10, picC11, picC12}
    6.         picBaixo = New PictureBox() {picB1, picB2, picB3, picB4, picB5, picB6, picB7, picB8, picB9, picB10, picB11, picB12}
    7.         For i = 0 To NumImgs - 1
    8.             picBaixo(i).Visible = False
    9.             picCima(i).BackColor = Color.Aqua
    10.         Next
    11.         primeira = True
    12.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Talking 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
  •  



Click Here to Expand Forum to Full Width