Results 1 to 7 of 7

Thread: [02/03] Adding a picturebox to the main form from code.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    [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:
    1. Public Sub New(ByVal Container As System.ComponentModel.IContainer, ByVal tempCash As Integer, ByVal tempPlaying As Integer, ByVal tempColor As String)
    2.         MyClass.New(tempCash, tempPlaying, tempColor)
    3.  
    4.         'Required for Windows.Forms Class Composition Designer support
    5.         Container.Add(Me)
    6.     End Sub
    7.  
    8.     Public Sub New(ByVal tempCash As Integer, ByVal tempPlaying As Integer, ByVal tempColor As String)
    9.         MyBase.New()
    10.  
    11.         'This call is required by the Component Designer.
    12.         InitializeComponent()
    13.  
    14.         'Add any initialization after the InitializeComponent() call
    15.         cash = tempCash
    16.         colorName = tempColor
    17.         playing = tempPlaying
    18.         If tempColor = "Red" Then
    19.             token.Image() = System.Drawing.Bitmap.FromFile("redpiece.png")
    20.             color = System.Drawing.Color.FromArgb(255, 128, 128)
    21.         ElseIf tempColor = "Blue" Then
    22.             token.Image() = System.Drawing.Bitmap.FromFile("bluepiece.png")
    23.             color = System.Drawing.Color.FromArgb(128, 128, 255)
    24.         ElseIf tempColor = "Green" Then
    25.             token.Image() = System.Drawing.Bitmap.FromFile("greenpiece.png")
    26.             color = System.Drawing.Color.FromArgb(128, 255, 128)
    27.         ElseIf tempColor = "Yellow" Then
    28.             token.Image() = System.Drawing.Bitmap.FromFile("yellowpiece.png")
    29.             color = System.Drawing.Color.FromArgb(255, 255, 128)
    30.         ElseIf tempColor = "Black" Then
    31.             token.Image() = System.Drawing.Bitmap.FromFile("blackpiece.png")
    32.             color = System.Drawing.Color.Gray
    33.         ElseIf tempColor = "White" Then
    34.             token.Image() = System.Drawing.Bitmap.FromFile("whitepiece.png")
    35.             color = System.Drawing.Color.White
    36.         Else
    37.             System.Console.WriteLine("Error in New() in Player.vb while trying to assign a color to the token")
    38.         End If
    39.  
    40.         If playing = 1 Or playing = 2 Then
    41.             token.Visible = True
    42.             token.Location = spacesArray(0).getLocation
    43.         End If
    44.     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?

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

    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.
    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
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [02/03] Adding a picturebox to the main form from code.

    umm.. just a note.. shouldn't u use select case?

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

    Re: [02/03] Adding a picturebox to the main form from code.

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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    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.

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

    Re: [02/03] Adding a picturebox to the main form from code.

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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    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
  •  



Click Here to Expand Forum to Full Width