Home > Mobile >  OnPaint event is not updating the form
OnPaint event is not updating the form

Time:01-26

I know this code will work in Visual Studio 2010, but for some reason it is not updating the form in Visual Studio 2022. I alread tried DoubleBuffer and User styles... Any clue what is wrong with this code?

`

namespace Kaledoscópio
{
    public partial class frmMain : Form
    {
        private readonly Random randomColor = new Random();

        public frmMain()
        {
            InitializeComponent();
            tmrStep.Tick  = TmrStep_Tick;
            tmrStep.Start();
        }

        private void TmrStep_Tick(object? sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void frmMain_OnPaint(object sender, PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            Color defaultColor = Color.FromArgb(randomColor.Next(255), randomColor.Next(255), randomColor.Next(255));
            using (Pen defaultPen = new Pen(defaultColor, 2))
            {
                int intHeight = this.ClientSize.Height;
                int intWidth = this.ClientSize.Width;

                int intMiddleHeight = intHeight / 2;
                int intMiddleWidth = intWidth / 2;

                Point posTopLeft = new Point(0, 0);
                Point posTopRight = new Point(intWidth, 0);
                Point posBottomLeft = new Point(0, intHeight);
                Point posBottomRight = new Point(intWidth, intHeight);
                Point posMiddle = new Point(intMiddleWidth, intMiddleHeight);
                Point posMiddleTop = new Point(intMiddleWidth, 0);
                Point posMiddleLeft = new Point(0, intMiddleHeight);
                Point posMiddleRight = new Point(intWidth, intMiddleHeight);
                Point posMiddleBottom = new Point(intMiddleWidth, intHeight);

                int defaultStep = randomColor.Next(3, 10);

                int b1;
                for (b1 = 0; b1 <= intMiddleWidth; b1  = defaultStep)
                {
                    g.DrawLine(defaultPen, posTopLeft.X   b1, posTopLeft.Y, posMiddle.X - b1, posMiddle.Y);
                    g.DrawLine(defaultPen, posTopRight.X - b1, posTopRight.Y, posMiddle.X   b1, posMiddle.Y);
                    g.DrawLine(defaultPen, posMiddle.X - b1, posMiddle.Y, posBottomLeft.X   b1, posBottomLeft.Y);
                    g.DrawLine(defaultPen, posMiddle.X   b1, posMiddle.Y, posBottomRight.X - b1, posBottomRight.Y);
                }

                int b2;
                for (b2 = 0; b2 <= intMiddleHeight; b2  = defaultStep)
                {
                    g.DrawLine(defaultPen, posMiddleTop.X, posMiddleTop.Y   b2, posMiddleLeft.X, posMiddleLeft.Y - b2);
                    g.DrawLine(defaultPen, posMiddleTop.X, posMiddleTop.Y   b2, posMiddleRight.X, posMiddleRight.Y - b2);
                    g.DrawLine(defaultPen, posMiddleRight.X, posMiddleRight.Y   b2, posMiddleBottom.X, posMiddleBottom.Y - b2);
                    g.DrawLine(defaultPen, posMiddleLeft.X, posMiddleLeft.Y   b2, posMiddleBottom.X, posMiddleBottom.Y - b2);
                }
            }
        }
    }
}

Actually, it will works if I force a call to Paint() event inside OnPaint() event, but frankly, I don´t think this is the right way to deal with this... Paint() event should be called internally.

CodePudding user response:

Base on your advices I found one solution. No sure about any colletaral effect, but it works.

I just commented base.OnPaint(e) and it works.

Here is the final code:

namespace Kaledoscópio
{
    public partial class frmMain : Form
    {
        private readonly Random randomColor = new Random();

        public frmMain()
        {
            InitializeComponent();
            tmrStep.Tick  = TmrStep_Tick;
            tmrStep.Start();
        }

        private void TmrStep_Tick(object? sender, EventArgs e)
        {
            this.Refresh();
        }

        private void frmMain_Paint(object sender, PaintEventArgs e)
        {
            //base.OnPaint(e);

            Graphics g = e.Graphics;

            Color defaultColor = Color.FromArgb(randomColor.Next(255), randomColor.Next(255), randomColor.Next(255));
            using (Pen defaultPen = new Pen(defaultColor, 2))
            {
                int intHeight = this.ClientSize.Height;
                int intWidth = this.ClientSize.Width;

                int intMiddleHeight = intHeight / 2;
                int intMiddleWidth = intWidth / 2;

                Point posTopLeft = new Point(0, 0);
                Point posTopRight = new Point(intWidth, 0);
                Point posBottomLeft = new Point(0, intHeight);
                Point posBottomRight = new Point(intWidth, intHeight);
                Point posMiddle = new Point(intMiddleWidth, intMiddleHeight);
                Point posMiddleTop = new Point(intMiddleWidth, 0);
                Point posMiddleLeft = new Point(0, intMiddleHeight);
                Point posMiddleRight = new Point(intWidth, intMiddleHeight);
                Point posMiddleBottom = new Point(intMiddleWidth, intHeight);

                int defaultStep = randomColor.Next(3, 10);

                int b1;
                for (b1 = 0; b1 <= intMiddleWidth; b1  = defaultStep)
                {
                    g.DrawLine(defaultPen, posTopLeft.X   b1, posTopLeft.Y, posMiddle.X - b1, posMiddle.Y);
                    g.DrawLine(defaultPen, posTopRight.X - b1, posTopRight.Y, posMiddle.X   b1, posMiddle.Y);
                    g.DrawLine(defaultPen, posMiddle.X - b1, posMiddle.Y, posBottomLeft.X   b1, posBottomLeft.Y);
                    g.DrawLine(defaultPen, posMiddle.X   b1, posMiddle.Y, posBottomRight.X - b1, posBottomRight.Y);
                }

                int b2;
                for (b2 = 0; b2 <= intMiddleHeight; b2  = defaultStep)
                {
                    g.DrawLine(defaultPen, posMiddleTop.X, posMiddleTop.Y   b2, posMiddleLeft.X, posMiddleLeft.Y - b2);
                    g.DrawLine(defaultPen, posMiddleTop.X, posMiddleTop.Y   b2, posMiddleRight.X, posMiddleRight.Y - b2);
                    g.DrawLine(defaultPen, posMiddleRight.X, posMiddleRight.Y   b2, posMiddleBottom.X, posMiddleBottom.Y - b2);
                    g.DrawLine(defaultPen, posMiddleLeft.X, posMiddleLeft.Y   b2, posMiddleBottom.X, posMiddleBottom.Y - b2);
                }
            }
        }
    }
}

CodePudding user response:

Try

private void TmrStep_Tick(object? sender, EventArgs e)
{
    this.Refresh();
}

This will force the repaint of the form. I assume you have the form .Paint event assigned to frmMain_OnPaint() already.

  • Related