0

I have a winform, and during LOAD it calls a textbox backcolor change which fires the paint event - my code never reaches the lines after that textbox colour change and I've noticed during step-through that the paint event is looping loads of times. What's happening here? Is VS realising I'm stuck in an infinite paint loop and just skipping the broken Load code for me?

public Pretty_Load()
{
    textBox1.BackColor = Color.FromArgb(230, 255, 255, 255);
    /* Other lines that aren't reached */
}

Here's the paint event which loops many times before just giving up.

private void Pretty_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Point p1 = this.ClientRectangle.Location;
    Point p2 = new Point(this.ClientRectangle.Right, this.ClientRectangle.Bottom);
    using (LinearGradientBrush brsGradient = new LinearGradientBrush(p1, p2, Color.FromArgb(64, 64, 100), Color.FromArgb(32,0,0)))
    {
        g.FillRectangle(brsGradient, e.ClipRectangle);
    }
}

I'm adding the paint event handler during the Pretty form constructor:

public Pretty()
{
    InitializeComponent();
    this.Paint += Pretty_Paint;
    /* Some other things and backcolor changes.
}

I suspect that maybe I've setup the handler wrong, or the looping is actually part of the "other things" color changes during the constructor that are all just happening during the textbox1.BackColor change

jamheadart
  • 5,047
  • 4
  • 32
  • 63
  • 1
    During Debug your paint would fire multiple times. Coz the moment breakpoint hits VS IDE is maximized & brought to focus, which causes the WinForms app to repaint. – Prateek Shrivastava Sep 24 '18 at 06:21
  • when did you call Pretty_Load() ? , and why you assign [frm_LOGIN_Paint] to this.Paint ? as you wrote you should assign this.Paint to [Pretty_Paint]. give more information about your code. – Siavash Sep 24 '18 at 06:25
  • Apologies Siavash - that was a paste error. The LOAD routine is called almost straight away since the Program runs it `Application.Run(new Pretty());` – jamheadart Sep 24 '18 at 06:33
  • 2
    Note that during Form_Load event, exceptions may be swallowed. Try adding a manual try/catch block inside Form_Load and set a breakpoint in the catch block to see if this is what happens. – Lasse V. Karlsen Sep 24 '18 at 06:33
  • @Lasse - that did it, one of my controls doesn't support transparent colors... Now I just need to figure which...EDIT: It was the TextBox itself - dunno why I tried to make my textbox transparent!!! Thanks all. Code reaches the end sucessfully now. – jamheadart Sep 24 '18 at 06:43

0 Answers0