Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Game Programming > Creating Dynamic array of PictureBoxes


Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2004, 05:49 PM
Laeys Laeys is offline
Newcomer
 
Join Date: Jun 2003
Posts: 8
Default Creating Dynamic array of PictureBoxes


Hey all,

This may be something incredibly simple, but I am not so good at VB.Net's features. Anyways this is the idea, and then what I have so far.
I want a GroupBox to contain 5 Pictureboxes that should be initialized to nothingness when the program starts off. Then on a click event they will be drawn dynamically( I hope) to contain randomly generated images.

What I have follows and it just keeps spitting out System.NullReferrenceException error messages. I know I am missing something silly, but can;t seem to find it.

Code:
    Public diceRoll As PictureBox()
    Const picHeight = 64
    Const picWidth = 72

    Sub InitializeRoll()
        Dim count As Integer = 0
        Dim upper As Integer = 5
        Dim xPos As Integer = 240
        Dim yPos As Integer = 40
        ReDim diceRoll(upper)

        For count = 0 To 5
            Me.Controls.Add(diceRoll(count))
            diceRoll(count) = picDie(count)

            diceRoll(count).Height.Equals(picHeight)
            diceRoll(count).Width.Equals(picWidth)
            diceRoll(count).Location.X.Equals(xPos)
            diceRoll(count).Location.Y.Equals(yPos)
            'diceRoll(count).Visible = False
            diceRoll(count).Image = Nothing

            yPos += 70

        Next

    End Sub
There is some other surrounding code as well...and this isn't very clean but I just want to see if this idea will work before I go too much further. Thanks in advance for any help.

Laeys
Reply With Quote
  #2  
Old 05-23-2004, 07:00 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

When you redim an array of controls like that, you probably want to initialize each member of the control array to a new picturebox instance.
So, before you add the picturebox to the form, why don't you try setting it to a new instance.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 05-23-2004, 08:29 PM
Laeys Laeys is offline
Newcomer
 
Join Date: Jun 2003
Posts: 8
Default

Thanks IcePlug. I will do that, but I think I found another way to do what i want...more or less.

I do have another problem though. I am not sure why this line doesn't seem to work
Code:
   ' *************************************************
   ' this is really klodgy any ideas?
   ' what I am trying to do is to assign a value to a structure member
   ' based on the number of pips on the image that is randomly 
   ' generated.
   ' *************************************************

    Sub DieValue1(ByVal picKeeper1 As PictureBox)

        If picKeeper1.Image Is Image.FromFile(Directory.GetCurrentDirectory _
                                            & PRE & "die1" & SUF) Then
            newDice.d1 = 1
        ElseIf picKeeper1.Image Is Image.FromFile(Directory.GetCurrentDirectory _
                                            & PRE & "die2" & SUF) Then
            newDice.d1 = 2
        ElseIf picKeeper1.Image Is Image.FromFile(Directory.GetCurrentDirectory _
                                            & PRE & "die3" & SUF) Then
            newDice.d1 = 3

        ElseIf picKeeper1.Image Is Image.FromFile(Directory.GetCurrentDirectory _
                                            & PRE & "die4" & SUF) Then
            newDice.d1 = 4
        ElseIf picKeeper1.Image Is Image.FromFile(Directory.GetCurrentDirectory _
                                            & PRE & "die5" & SUF) Then
            newDice.d1 = 5
        ElseIf picKeeper1.Image Is Image.FromFile(Directory.GetCurrentDirectory _
                                            & PRE & "die6" & SUF) Then
            newDice.d1 = 6
        End If
    End Sub
thanks again
Reply With Quote
  #4  
Old 05-23-2004, 08:36 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

That actually looks like 12-15 lines.

Anyway, pictures are not comparable like that.
Even if you make thousands of Image.FromFiles, they'll all be 'different', but their pixel data will remain the same.

Instead, you could load all of your pictures into an ImageList, and then assign them to controls there... then do your comparisons with the images in the image list.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #5  
Old 05-24-2004, 03:16 PM
Laeys Laeys is offline
Newcomer
 
Join Date: Jun 2003
Posts: 8
Default

not quite getting that. Like I said I am relatively new to VB.Net. Thanks for your help though. I have got an ImageList object, but there is nothing in it I can change except for the images I put in there. Also if I do use it like:
Code:
diceRoll(i) = DiceList.Images(i)
The image quality is so much less than if I just load the image into a PictureBox. That being said I am not sure how to compare this to an image file.
Reply With Quote
  #6  
Old 05-24-2004, 04:16 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

OK, nevermind, don't use the ImageList control... that is for Toolbars and listviews and other controls.

Instead, use a bitmap array.
Dim Bmps(19) As Bitmap

When you start the program, you will load all of your unique images into the bitmap array.
Bmps(0) = Image.FromFile(Pth & "rock.gif")

Then, to assign an image to a picturebox:
PB.Image = Bmps(4)

To check if the pictures are equal:
If PB.Image Is Bmps(4) Then
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #7  
Old 05-24-2004, 05:18 PM
Laeys Laeys is offline
Newcomer
 
Join Date: Jun 2003
Posts: 8
Unhappy

I feel like a moron here...but I have been beating my head against this NullException error
Code:
An unhandled exception of type 'System.NullReferenceException' occurred in dynamicimages.exe

Additional information: Object reference not set to an instance of an object.
So here is what I have without the windows generated code


Code:
Imports System.IO



Public Class Form1
    Inherits System.Windows.Forms.Form


    Public diceRoll(5) As PictureBox


    ' For the random number generator
    Dim objRandom As Random = New Random
    Dim intRandom As Integer

    Const PRE As String = "/Images/"
    Const SUF As String = ".png"

    ' image placeholder
    Public bmpDice(5) As Bitmap




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        initRoll()
        Call DisplayDie(diceRoll)

        'This was a test to see if the array worked
        'diceRoll(1).Image = bmpDice(0)     End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' filling up the array
        bmpDice(0) = Image.FromFile(Directory.GetCurrentDirectory _
                                        & PRE & "die1" & SUF)
        bmpDice(1) = Image.FromFile(Directory.GetCurrentDirectory _
                                                & PRE & "die2" & SUF)
        bmpDice(2) = Image.FromFile(Directory.GetCurrentDirectory _
                                                & PRE & "die3" & SUF)
        bmpDice(3) = Image.FromFile(Directory.GetCurrentDirectory _
                                                & PRE & "die4" & SUF)
        bmpDice(4) = Image.FromFile(Directory.GetCurrentDirectory _
                                                & PRE & "die5" & SUF)
        bmpDice(5) = Image.FromFile(Directory.GetCurrentDirectory _
                                                & PRE & "die6" & SUF)

    End Sub


    Function Roll() As Integer
        intRandom = objRandom.Next(1, 7)
        Return (intRandom)

    End Function


    Function DisplayDie(ByVal diceRoll() As PictureBox)

        Dim i As Integer
        initRoll()

        For i = 0 To 5
            Call Roll()
            Select Case intRandom
                Case 1
                    diceRoll(i).Image = bmpDice(i)
                Case 2
                    diceRoll(i).Image = bmpDice(i)
                Case 3
                    diceRoll(i).Image = bmpDice(i)
                Case 4
                    diceRoll(i).Image = bmpDice(i)
                Case 5
                    diceRoll(i).Image = bmpDice(i)
                Case Else
                    diceRoll(i).Image = bmpDice(i)
            End Select
        Next i
    End Function



    Private Sub initRoll()

        diceRoll(1) = picDie1
        diceRoll(2) = picDie2
        diceRoll(3) = picDie3
        diceRoll(4) = picDie4
        diceRoll(5) = picDie5

    End Sub


End Class
Works fine in my original project. Was using this as a test and was going to integrate the ability to dynamically draw the 'dice'. I can;t seem to figure out the error though. Thanks again.
Reply With Quote
  #8  
Old 05-24-2004, 05:32 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

Are you getting the error for the diceRoll pictureboxes?
Because I don't see where you are instantiating them to new instances.

diceroll(0) = New PictureBox
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
auto-populate popup menu from another popup menu Geuis Web Programming 1 04-20-2004 12:13 AM
Dynamic array question OsMo General 5 03-25-2004 02:10 PM
Control array & dynamic array IGBP General 2 01-17-2004 05:08 PM
Array and String Functions rhawke General 5 07-10-2003 02:33 AM
Dynamic array mark82 General 15 02-20-2003 06:17 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->