Search code examples
c#invalidationdrawrectangle

Invalidate and DrawRectangle don't work together


Imagine a simple .Net 2.0 Windows-Form with a MultilineTextbox that fills the whole form. I want to (re-)draw a rectangle everytime a key is pressed. In the real application there is fare more logic about the position and stuff of the rectangle - but keep it simple.

I thought: "Lets first invalidate the TextBox and then draw the rectangle." But this doesnt work. The screen flickers shortly - thats it. If I remove the line "invaliate" a rectangle is drawn - but the old ones keep their position..

Whats wrong? And how do I repaint from scratch?

Thank you in advance for your answers!

private void OnKeyDown(object sender, KeyEventArgs e)
{
        textBox1.Invalidate();

        using (Graphics g = this.textBox1.CreateGraphics())
        {
            int startX = 100;
            int startY = 300;
            int height = 200;

            Brush brush = new SolidBrush(Color.FromArgb(60, 255, 0, 0));

            Pen myPen = new Pen(Color.Black, 2);
            myPen.DashStyle = DashStyle.Dash;

            g.DrawRectangle(myPen, startX, startY, this.textBox1.Width, height);

            g.FillRectangle(brush, startX, startY, this.textBox1.Width, height);
        }
}

Solution

  • I was told to use "textBox1.Refresh()" instead of "textBox1.Invalidate();" and it works!

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        //textBox1.Invalidate(); - Dont do that
        textBox1.Refresh(); //Dominik Schelenz, my internet Hero for today!
    
        using (Graphics g = this.textBox1.CreateGraphics())
        {
            int startX = 100;
            int startY = 300;
            int height = 200;
    
            Brush brush = new SolidBrush(Color.FromArgb(60, 255, 0, 0));
    
            Pen myPen = new Pen(Color.Black, 2);
            myPen.DashStyle = DashStyle.Dash;
    
            g.DrawRectangle(myPen, startX, startY, this.textBox1.Width, height);
    
            g.FillRectangle(brush, startX, startY, this.textBox1.Width, height);
        }
    }