|
-
Nov 6th, 2006, 04:33 PM
#1
Thread Starter
Junior Member
[02/03] Adding a picturebox to the main form from code.
Hey all,
I'm recreating an old board game on the computer using VB. This game has player tokens that have to be displayed on the screen. I used the "Component" class to create the definition of a player, from which I instantiate a player for each player that is playing. Within the class, I define a picturebox with an image inside to represent the player token.
Here's the problem - I can't get the token pictureboxes to appear on the screen. I am almost positive that it is because I am not putting them in a container. I tried to pass the form as a container to put the tokens on, but it errors out when I try to do that, saying it is an invalid cast. Here is the code for a new Player:
VB Code:
Public Sub New(ByVal Container As System.ComponentModel.IContainer, ByVal tempCash As Integer, ByVal tempPlaying As Integer, ByVal tempColor As String)
MyClass.New(tempCash, tempPlaying, tempColor)
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New(ByVal tempCash As Integer, ByVal tempPlaying As Integer, ByVal tempColor As String)
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
cash = tempCash
colorName = tempColor
playing = tempPlaying
If tempColor = "Red" Then
token.Image() = System.Drawing.Bitmap.FromFile("redpiece.png")
color = System.Drawing.Color.FromArgb(255, 128, 128)
ElseIf tempColor = "Blue" Then
token.Image() = System.Drawing.Bitmap.FromFile("bluepiece.png")
color = System.Drawing.Color.FromArgb(128, 128, 255)
ElseIf tempColor = "Green" Then
token.Image() = System.Drawing.Bitmap.FromFile("greenpiece.png")
color = System.Drawing.Color.FromArgb(128, 255, 128)
ElseIf tempColor = "Yellow" Then
token.Image() = System.Drawing.Bitmap.FromFile("yellowpiece.png")
color = System.Drawing.Color.FromArgb(255, 255, 128)
ElseIf tempColor = "Black" Then
token.Image() = System.Drawing.Bitmap.FromFile("blackpiece.png")
color = System.Drawing.Color.Gray
ElseIf tempColor = "White" Then
token.Image() = System.Drawing.Bitmap.FromFile("whitepiece.png")
color = System.Drawing.Color.White
Else
System.Console.WriteLine("Error in New() in Player.vb while trying to assign a color to the token")
End If
If playing = 1 Or playing = 2 Then
token.Visible = True
token.Location = spacesArray(0).getLocation
End If
End Sub
My error message ("Error in New()...") doesn't come up in the console, so it is assigning a picture to the token, and playing always = 1 at this point (eventually 0 will be not playing, 1 will be a human playing, and 2 will be a CPU playing). Any suggestions? Should I create a container on the form (like a panel) that is transparent and use that for the token positions?
-
Nov 6th, 2006, 05:18 PM
#2
Re: [02/03] Adding a picturebox to the main form from code.
I don't really know everything about your app but I don't really think that it's appropriate to have the Player class inherit Component or maintain a PictureBox. I would consider it more appropriate that the Player class merely inherit Object and maintain an Image object to represent its playing piece, letting the form worry about the PictureBox to display it.
-
Nov 6th, 2006, 06:32 PM
#3
Frenzied Member
Re: [02/03] Adding a picturebox to the main form from code.
umm.. just a note.. shouldn't u use select case?
-
Nov 6th, 2006, 06:43 PM
#4
Re: [02/03] Adding a picturebox to the main form from code.
 Originally Posted by Fromethius
umm.. just a note.. shouldn't u use select case?
Further to that, I'd be inclined to use a Color variable, which is limited to valid colours, where a String variable could theoretically be anything.
-
Nov 7th, 2006, 10:46 PM
#5
Thread Starter
Junior Member
Re: [02/03] Adding a picturebox to the main form from code.
jmcilhinney - How would I let the form worry about the picturebox, but also be able to reference it from the Player object? I need to reset the picturebox's location each time the piece moves, and I'd like to do that straight from a function in the Player object. If there's a good way you can think of to change the location of the picturebox straight from the Player object, then let me know.
Fromethius - Yes I should use the select case statement, but I couldn't remember the exact syntax of it and I was too lazy to look it up. Elseifs are just easier to me, and I don't think it's any more system intensive (which doesn't matter in this case anyhow).
jmcilhinney again - I would be inclined to use a color variable also, and I would have, except that I am using the string elsewhere to tell what color's turn it is (i.e. "It's Red's turn now."). I'd also have to parse the original input that selects the color (a combobox) to a color, and I didn't want to deal with those complications.
-
Nov 7th, 2006, 11:01 PM
#6
Re: [02/03] Adding a picturebox to the main form from code.
 Originally Posted by Sgt Spike
jmcilhinney - How would I let the form worry about the picturebox, but also be able to reference it from the Player object? I need to reset the picturebox's location each time the piece moves, and I'd like to do that straight from a function in the Player object. If there's a good way you can think of to change the location of the picturebox straight from the Player object, then let me know.
You should think about whether that is appropriate in a properly designed OO application. I don't think it necessarily is but, as I said, I don't know everything about your application. I'm thinking that the Player might remember its colour and its position on the playing board, then the form would turn that information an actual Location.
 Originally Posted by Sgt Spike
I would be inclined to use a color variable also, and I would have, except that I am using the string elsewhere to tell what color's turn it is (i.e. "It's Red's turn now."). I'd also have to parse the original input that selects the color (a combobox) to a color, and I didn't want to deal with those complications.
No you wouldn't. You can quite easily get a Color object directly from a ComboBox. If you create a class or structure that has a property for a Color and a property for a String that represents that colour then you can bind an array of those objects directly to a ComboBox. You assign the String property to the DisplayMember, so that was what the user saw, and the Color property to the ValueMember, so that would be what you got from the SelectedValue property.
-
Nov 7th, 2006, 11:30 PM
#7
Thread Starter
Junior Member
Re: [02/03] Adding a picturebox to the main form from code.
Makes sense... I guess I just don't like putting anything but calls to functions in the form code, but maybe I should change that habit a bit.
I'll give that a try too. Thanks for the help!
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
|