I don't know how to describe this error so I just took a picture. The problem involves the rich text box and numericUpDowns displaying what is behind them when the form loads. Pic on Left is wrong pic on right is right.

I was drawing my oscilloscope output directly onto the form, but I switched to a pictureBox so the form could be resized more easily, and this started happening.

If I move the window it corrects itself, but if I do a this.Refresh or this.Show it does not.

Recommended Answers

All 13 Replies

The .Refresh() only repains regions of the form that have been invalidated. To force a repaint call .Invalidate() . You may only want to invalidate the picture box or wherever you are doing the custom painting.

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

Hi,

try to do:

this.nameOfPictureBox.Invalidate();

if it does not work post your code

Hi,

try to do:

this.nameOfPictureBox.Invalidate();

if it does not work post your code

That is what I just said....

That is what I just said....

yes i know but i notice it after i posted it.
and even so are we in a competition here who is answering first or are we tring to help pepole?

commented: thank you +2

Point taken, you are right and I do apologize.

Point taken, you are right and I do apologize.

apologize accepted

It did not work unless I put the invalidates in OnPaint(), in which case the window displayed properly but froze.

I want to make sure we are clear: The image is displaying properly, but the controls on the left are displaying a frozen view of whatever is behind them.

I believe the constructor is the relevant portion of the code to show:

public AnalysisForm(double[,] dataPointsFromMain, char[] overVoltageFromMain, userLimits _limits,
            string[,] timingInfoFromGetTestTiming, initialDialog refFromParent, double _rightX,
            double _topY)
        {
            InitializeComponent();

            //window properties
            this.MaximizeBox = false; this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.KeyPreview = true; this.StartPosition = new FormStartPosition();

            //Get data from main form
            dataPoints = dataPointsFromMain;
            overVoltage = overVoltageFromMain;
            limits = _limits;
            timingInfo = timingInfoFromGetTestTiming;
            rootForm = refFromParent;
            
            plot = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            graphSize.Width = pictureBox1.Width - 1;
            graphSize.Height = pictureBox1.Height - 1;

            //graph scaling
            //we draw the whole x plane at first
            rightX = _rightX; topY = _topY; leftX = 0;
                                               
            g = Graphics.FromImage(i);

            //draws the scope background and makes it the pictureBox1.BackgroundImage
            drawBackground();

            //populates an image with the graph data and makes it the pictureBox1.image
            drawGraph();

            //data manipulation
            fillTable();

            //this.Invalidate();
            //this.vMaxListBox.Invalidate();
            //this.grpCursors.Invalidate();
            //this.grpWindow.Invalidate();
            //this.pictureBox1.Invalidate();
            //this.Refresh();
        }

The comments above are where I tried various combinations of invalidations, I didn't just try all at once.
A section of the drawGraph() function:

public void drawGraph()
        {
            Graphics toScreen = this.CreateGraphics();
            Graphics dc = Graphics.FromImage(plot);

            //draw it...

            pictureBox1.Image = plot;
        }

I'm not quite sure why you would be drawing in a constructor since the form is not being shown at that point. When the control loads I believe is the time to start the drawing.

Also -- The OnPaint() method is called constantly as the form is painted so it probably spun your CPU with 100% usage as well. Use a bit and only invalidate the screen when the painting changes. When the control goes to paint if the bit is set then invalidate the control area and unset the bit. This will stop constantly invalidating a control.

I still think it seems like a bad idea to be drawing in a constructor but I don't ever paint the screen myself so its' just a hunch.

I agree with sknake about drawing the object in the constructor, try to draw it in a method that will be called after the object as been construct.

If its still does not work so zip you solution and upload it here so we can see your entire code and give you the solution to your problem, maybe you are doing somthing wrong there.

I started playing around with the ideas you guys threw at me, and they fixed the initial loading but not later clipping events.
So I started trying to fix this part and ended up discovering a solution that fixed both cases (and I still don't fully understand why).
I just adjusted the OnPaint() method to check the clipping region before re-drawing the plot.

protected override void OnPaint(PaintEventArgs e)
        {
            if (!e.ClipRectangle.IsEmpty)
            {
                if (e.ClipRectangle.Left > 214) 
                         pictureBox1.Image = plot;
            }
            base.OnPaint(e);
        }

After this, there was no problem and no invalidations were needed.

Thanks for leading me down this path, I still learned about the concepts you were discussing. If you understand why my accidental solution solved the problem, I would love to hear it.

EDIT: I also optimized onPaint, I discovered my early code was inefficient: was redrawing backgroundimage every time as well.

I'm glad to see you found a solution to your problem. I don't think I can explain why this fixed your application... I don't deal with painting too much.

Please mark this thread as solved if we have answered your question.

I really dont think there is a connection between the checking the clipping region before drawing the plot to your problem,
but what i do think, is that your problem was redrawing the backgroundimage every time.

Anyway i'm happy that your problem has been solved.

I was just allowing a little time for potential explanations before I marked it solved.

Thanks again guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.