5

I have a form which I'm bringing up using ShowDialog which contains a couple of text boxes, labels and a button. The problem I'm having is that the text boxes are being drawn before the form itself and the other controls are drawn.

I am overriding the OnPaint method I'm not sure if this could be causing the problem:

protected override void OnPaint(PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Black, ButtonBorderStyle.Solid);
    base.OnPaint(e);
}

It's only a slight delay but it's visible and annoying. Thank you.

The form is double buffered by the way.

EDIT: I have pinpointed the issue to be the fact that the form does not have a FormBorderStyle. With the FormBorderStyle set to Sizable, this issue does not occur. However please note that having FormBorderStyle.None as my border style is necessary, so I have not found a solution yet.

Jurgen Camilleri
  • 3,559
  • 20
  • 45
  • 1
    Can you post all relevant code pertaining to the drawing of the Labels, Textboxes and Buttons? – JosephGarrone May 05 '13 at 12:38
  • Actually, the parent element calls each child control's render method before rendering itself. This is the default behavior. May be you can show the dialog once the form load is completed. – Saravanan May 05 '13 at 12:39
  • All controls have been added via the Visual Studio designer so I did not touch the drawing of these controls. – Jurgen Camilleri May 05 '13 at 12:42
  • @saravanan I'm instantiating the Form globally and calling `ShowDialog` when I need to show it. When would the `Load` event be raised in this case? If I understood correctly, you are suggesting I call `ShowDialog` in the `Load` event, is this correct? – Jurgen Camilleri May 05 '13 at 12:45
  • 1
    @JurgenCamilleri: can you try to draw the border in the `Shown` method as given here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown.aspx – Saravanan May 05 '13 at 12:52
  • How would I get a reference to the `Graphics` object from the `Shown` event? – Jurgen Camilleri May 05 '13 at 12:56
  • Does this issue occur when DoubleBufferred = false? – JosephGarrone May 05 '13 at 12:56
  • Yes, but I have found out the problem. See my edit. – Jurgen Camilleri May 05 '13 at 13:00
  • 2
    @JurgenCamilleri: You can obtain a new graphics object from inside the Shown event by using this.CreateGraphics but I doubt that would solve your current problem. – Raheel Khan May 22 '13 at 18:35
  • Since you were able to solve your problem, can you add the solution as an answer? – Ryan Gates Jun 12 '13 at 15:06
  • I have not solved my problem, I just found the source. I updated the question to make this clearer. – Jurgen Camilleri Jun 12 '13 at 16:09

1 Answers1

1

Try adding this to the dialog box form:

    protected override CreateParams CreateParams
    {
        get
        {
            // Activate double buffering at the form level.  All child controls will be double buffered as well.

            CreateParams cp = base.CreateParams;

            cp.ExStyle |= 0x02000000;   // WS_EX_COMPOSITED

            return cp;
        }
    }
Gojira
  • 2,941
  • 2
  • 21
  • 30