Results 1 to 4 of 4

Thread: [RESOLVED] Control Backround Images

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    15

    Resolved [RESOLVED] Control Backround Images

    HI, as a noob & as an assignment I have written a Noughts and crosses
    program. It all works fine. I was trying to jazz it up by letting random noughts, crosses and empty squares fill the grid before the game starts.
    This code works fine but only for a few timer clicks...and for some reason I always end up with a row of crosses across the middle !
    the Cstr(tempPos) lines are just for debugging and they show the code and timer are still working...but the images dont change ??? HELP !

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    tempPos = Int(Rnd() * 9) + 3
    If tempPos < 6 Then
    Controls(tempPos).BackgroundImage = NoughtsAndCrosses.My.Resources.Resources.nought
    lblInfo.BackColor = Color.Red
    lblInfo.Text = " <<<<< PLAY NOW >>>>> "
    lblInfo.Text = CStr(tempPos)
    End If
    If tempPos > 5 And tempPos < 9 Then
    Controls(tempPos).BackgroundImage = NoughtsAndCrosses.My.Resources.Resources.cross
    lblInfo.BackColor = Color.Blue
    lblInfo.Text = " <<<<< PLAY NOW >>>>> "
    lblInfo.Text = CStr(tempPos)
    Else
    Controls(tempPos).BackgroundImage = NoughtsAndCrosses.My.Resources.Resources.blank
    lblInfo.BackColor = Color.Green
    lblInfo.Text = " <<<<< PLAY NOW >>>>> "
    lblInfo.Text = CStr(tempPos)
    End If
    End Sub

    Many Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Control Backround Images

    Do not use the Randomize and Rnd functions to generate random numbers. Use a Random object. You should create a 2D array for your controls and then you can generate a random X and Y value, e.g.
    vb.net Code:
    1. Private noughtImage As Image = My.Resources.Nought
    2. Private crossImage As Image = My.Resources.Cross
    3. Private blankImage As Image = Nothing
    4.  
    5. Private images As Image() = {Me.noughtImage, _
    6.                              Me.crossImage, _
    7.                              Me.blankImage}
    8.  
    9. Private squares As PictureBox(,)
    10.  
    11. Private numberGenerator As New Random
    12.  
    13. Private Sub Form1_Load(ByVal sender As Object, _
    14.                        ByVal e As EventArgs) Handles MyBase.Load
    15.     Me.squares = New PictureBox(,) {{Me.PictureBox1, Me.PictureBox2, Me.PictureBox3}, _
    16.                                     {Me.PictureBox4, Me.PictureBox5, Me.PictureBox6}, _
    17.                                     {Me.PictureBox7, Me.PictureBox8, Me.PictureBox8}}
    18. End Sub
    19.  
    20. Private Sub Timer1_Tick(ByVal sender As Object, _
    21.                         ByVal e As EventArgs) Handles Timer1.Tick
    22.     Dim imageIndex As Integer = Me.numberGenerator.Next(0, Me.images.Length)
    23.     Dim rowIndex As Integer = Me.numberGenerator.Next(0, Me.squares.GetUpperBound(0))
    24.     Dim columnIndex As Integer = Me.numberGenerator.Next(0, Me.squares.GetUpperBound(1))
    25.  
    26.     Me.squares(rowIndex, columnIndex).Image = Me.images(imageIndex)
    27. End Sub
    There are a few things to note here. Firstly, I create one Random object and call its Next method each time I want a random number.

    Second, I access the Images from My.Resources once and once only. Otherwise you're creating new Image objects over and over again, which is very inefficient.

    Thirdly, I don't actually use an Image object for blank squares. By using a null reference, i.e. Nothing, I am displaying no Image, which means a blank square. Maybe this isn't appropriate in your case but it very likely is.

    Fourthly, I create an array for the Images, so I can reference them by index, and I also create a 2D array of PictureBoxes, so I can refer to them by row and column index.

    Finally, each time the Timer Ticks I select a random Image and place it in the square in a random row and a random column. Clean and tidy.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    15

    Re: Control Backround Images

    Thanks...this is far superior.... but, out of interest, do you know why mine only worked for a short while ??

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Control Backround Images

    No idea. If you want to find out then you should debug it. You can't always work out what's happening just by reading code. Just like a car, you have to run it and see it in action to be able to diagnose a lot of problems.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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