 |
 |

05-23-2004, 05:49 PM
|
|
Newcomer
|
|
Join Date: Jun 2003
Posts: 8
|
|
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
|
|

05-23-2004, 07:00 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
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. 
|
|

05-23-2004, 08:29 PM
|
|
Newcomer
|
|
Join Date: Jun 2003
Posts: 8
|
|
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
|
|

05-23-2004, 08:36 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
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. 
|
|

05-24-2004, 03:16 PM
|
|
Newcomer
|
|
Join Date: Jun 2003
Posts: 8
|
|
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.
|
|

05-24-2004, 04:16 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
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

|
|

05-24-2004, 05:18 PM
|
|
Newcomer
|
|
Join Date: Jun 2003
Posts: 8
|
|
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.
|
|

05-24-2004, 05:32 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
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

|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|