PictureBox Position Changed when Complied

D' Eradicated

Member
Joined
Aug 25, 2014
Messages
5
Programming Experience
1-3
EDIT: Okay so, my code changed the position of the PictureBox. Is it because of changing it's parent?

VB.NET:
Sub setGridTransparency()        Dim I As Integer, J As Integer

        For I = 0 To 7
            For J = 0 To 7
                picGrid(I, J).Parent = picBoard
                picGrid(I, J).BackColor = Color.Transparent
            Next
        Next
    End Sub

I'm using Visual Studio 2013. I'm creating a board game using PictureBoxes where I set the Image property to the tiles I want. The problem is that the position is different from the Design interface to the compiled program. Any reason why this is happening?

Design
1.png

Compiled
2.png
 
Last edited:
Yes, location is relative to parent. Changing parent will relocate the control according to that.

Normally when you drag a control in designer it is parented to first underlying container, which should have happened if you placed a picturebox over a panel in designer. I think you must have added the picturebox to the form and manually edited the Location to place over the panel.

Document Outline window in designer will show you the parent/child relationship of controls, but is usually only necessary for advanced layout.

Using a TableLayoutPanel may be better for your layout.
 
Thanks, JohnH.

After searching for a workaround, I added these lines of code (not sure how the PointToSomething works):

VB.NET:
Sub setGridTransparency()
        Dim I As Integer, J As Integer, tempPosition As Point


        For I = 0 To 7
            For J = 0 To 7
                tempPosition = Me.PointToScreen(picGrid(I, J).Location)
                tempPosition = picBoard.PointToClient(tempPosition)
                picGrid(I, J).Parent = picBoard
                picGrid(I, J).Location = tempPosition
                picGrid(I, J).BackColor = Color.Transparent
            Next
        Next


    End Sub

Might try using a TableLayoutPanel. Can I also achieve image transparency using that over the board itself? Thanks again.
 
That would be a valid workaround, PointToScreen translates client point to a screen point, and PointToClient translates screen point to client point. Both methods works from the perspective of given control that calls the method.

Still unnessesary, since you can fix your layout in designer once and for all and don't need to rearrange programmatically each time form is loaded. A little more work now, but you'll remember this when designing forms in the future.
 
Back
Top