C# Drawing on a panel inside a panel using Graphics. The first panel is bigger than the second
I have two panels: one that is always visible and another that gets declared later and belongs to the first panel: "panel1.Controls.Add(this.Panel2);"
The whole program always has 2 TextBoxes, 7 PictureBoxes that are used as buttons, 3 labels and 1 panel (they are declared as soon as the program starts running).
After the second panel is declared, a drawing function activates. The function draws on the second panel.
If Panel2 is higher or wider (or both) than Panel1 (Panel2 becomes partly visible), the first time when the drawing function gets called, the second panel becomes blank after the drawing function finishes. I have to re-size the window, or something in order to have my drawing back (the drawing function gets 开发者_JS百科called in many cases). If Panel2 is smaller than Panel1 in every way, the problem doesn't happen.
Panel2 is being manipulated is in this sequence: Declaring Panel2; Adjusting Panel2's properties; Adding Panel2 to Panel1; Drawing on Panel2; Other Panel2 related stuff.
At first I used buttons and a PictureBox inside Panel1. The problem used to happen only if the buttons weren't using their default appearances (no matter the size of the PictureBox). Later I changed the PictureBox with a Panel and the buttons with PictureBoxes (I wanted to make custom buttons). After that change I faced the problem again.
The code is big, and I'm not sure if it is necessary. I surely have a problem with the algorithm of the code.
You're drawing wrong.
You need to draw inside a Paint
handler so that your content gets redrawn whenever the control is repainted.
You should never draw to control.CreateGraphics()
.
精彩评论