Results 1 to 7 of 7

Thread: Help with Picture Boxes?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    4

    Help with Picture Boxes?

    I have a chess program...i want it so that when i click on a piece, it will go through an If statement and determine which type of piece it is that was clicked on, but im having some problems. Here is what i have so far inside of my function

    If Square.Picture = ("Z:\Pawn.bmp") Then
    MovePawn()
    ElseIf Square.picture = ("Z:\Rook.bmp") Then
    MoveRook()
    ElseIf ....

    I think you can get the picture of what im trying to do...but the problem is that when i run the program, i get a

    Run-Time error '13':
    Type Mismatch

    When i hit the debug button it highlights the line of the if statement that says

    If Square.Picture = ("Z:\Pawn.bmp") Then


    I have no idea what is wrong. With my logic, it seems like it should work...any ideas?

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    .Picture is a property of the picture box, which is a StdPicture object, not a string. you need to use the .LoadPicture(filename) method to load a picture from a hard drive.

    what i would do would be to load all the piece pictures that i need in an array of PictureBox controls and play with them. to make a picture show on your chess board, i would use then
    VB Code:
    1. Set ChessBoard(8 * letter + digit).Picture = Pieces(INDEX_PAWN).Picture
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    4
    i dont think you get what im saying here...i have all the pieces on the board already, first i click on the piece i want to move, then i click on the square that i want the piece to go to...But it can't be just any piece that moves. I need to make sure that if i clicked on a pawn, that a pawn will appear in the square i clicked on the second time, and then the first pawn disappears. So what it's saying is, if the first square that you clicked on had the Pawn as the .picture property, then it should go to the MovePawn function. Then the same thing happens when you click on any other piece, it just goes to the corresponding function to move that piece. I hope this is making sense, because I've been working on this for quite awhile and having no luck. I know there are probably much easier ways to make this game, but im trying a different approach.

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    One way to do it would be to use the tag property of the picturebox to hold the piece type. Set it when you move the piece to the square and clear it in the square that you moved from. When you click on the piece, use a select case on the tag property to call the needed function.

  5. #5
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    well, yes, i understand, but you cant just ask a square on your chess board 'does your picture looks like a pawn?'. i mean in theory you could, but it's not the best to compare pictures instead of comparing simple numeric values...

    i would start by declaring a piece as:
    VB Code:
    1. Type Piece
    2.   Figure As Integer
    3.   Color As Integer
    4. End Type

    for the main app i would use 2 variables. one of them would be a 2-dimensional array that would store what figure is in every square on the chess board. lets name it ChessBoard, it store the virtual layout of the pieces on the chessboard.
    VB Code:
    1. Dim ChessBoard (1 To 8, 1 To 8) As Piece
    another one would be the image of the chess board itself, built up of 64 squares represented by picture boxes. Lets name the array of 64 picture box controls ChessBoardImage (indexes 0 to 63). (unfortunately, you cannot declare a 2-dimensional array of objects, because this would have looked nicer.) separately from that, i would declare another set of preloaded picture boxes (PieceImages), each of them preloaded with the image of every piece.

    on the Form_Load event i would:
    (1) alternatively set the background colors of the ChessBoardImage picture boxes to black and white
    (2) load the images in the Pieces() array of preloaded images:
    VB Code:
    1. PieceImages(0).LoadPicture App.Path & "\PAWNWHITE.BMP"
    2. PieceImages(1).LoadPicture App.Path & "\TOWERWHITE.BMP"
    3. '...
    4. PieceImages(6).LoadPicture App.Path & "\PAWNBLACK.BMP"
    5. PieceImages(7).LoadPicture App.Path & "\TOWERBLACK.BMP"
    6. '...
    (3) would SetPiece() on the first 2 rows and last 2 rows of the chess board, to obtain the initial layout.

    now lets define the SetPiece and MovePiece functions:
    VB Code:
    1. Sub SetPiece( _
    2.   ByVal p As Piece, _
    3.   ByVal ToX As Integer, ByVal ToY As Integer _
    4. )
    5. ChessBoard(ToX, ToY).Figure = p.Figure
    6. ChessBoard(ToX, ToY).Color = p.Color
    7. Set ChessBoardImage(8 * (ToY - 1) + ToX - 1).Picture = _
    8.   PieceImages(p.Figure + 6 * Iif(p.Color = COLOR_WHITE, 0, 1)).Picture
    9. End Sub
    10.  
    11. Sub MovePiece( _
    12.   ByVal FromX As Integer, ByVal FromY As Integer, _
    13.   ByVal ToX As Integer, ByVal ToY As Integer _
    14. )
    15. 'make the destination show the piece
    16. ChessBoard(ToX, ToY).Figure = ChessBoard(FromX, FromY).Figure
    17. ChessBoard(ToX, ToY).Color = ChessBoard(FromX, FromY).Color
    18. Set ChessBoardImage(8 * (ToY - 1) + ToX - 1).Picture = _  
    19.   ChessBoardImage(8 * (FromY - 1) + FromX - 1).Picture
    20. 'delete from the origin
    21. ChessBoard(FromX, FromY).Figure = FIG_NONE
    22. ChessBoard(FromX, FromY).Color = COLOR_NONE
    23. Set ChessBoardImage(8 * (FromY - 1) + FromX - 1).Picture = Nothing
    24. End Sub
    the problem that you have, i.e. what piece do i have in the square X and Y, is simply checking the values for ChessBoard(X, Y).Figure and ChessBoard(X, Y).Color. the .Figure will take values like FIG_NONE, FIG_PAWN, FIG_TOWER and so on (constants from 0 to 6) and .Color will be COLOR_NONE, COLOR_WHITE or COLOR_BLACK (constants from 0 to 2).
    Last edited by radum; May 19th, 2003 at 09:58 PM.
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    4
    I understand what you guys are saying...im gonna try the tag thing first...ive never even heard of that before, but it sounds useful as hell...and if that doesnt work then im gonna try out what radum says...thanks!

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    4
    ahhhhh yes...the .tag property works miracles! thank you very much for all your 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