Jump to content

General question here

- - - - -

  • Please log in to reply
3 replies to this topic

#1
williamevanl

williamevanl

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
I have a program in c# that basically draws points on the screen. A button causes these points to be drawn. Sometimes and it seems completely random, the points are drawn and then vanish for no reason. Why is this happening and how do I fix it?

#2
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
Can you post your code?
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#3
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
You must draw points on Paint event, not just when you wish it. If you do not do that, it may happen that something else triggers the Paint event which in turn paints window background using solid brush - hence your points disappear.

Correct way to draw points on button click is to invoke Invalidate method, specifying the region which should contain points. Once done, OnPaint method will be invoked from inside and there is the place to put drawing code.

#4
williamevanl

williamevanl

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
That sounds helpful ^ But I'm not sure how to do it. Here' my code

private void plotcolor()
        {

            
            Graphics g = this.CreateGraphics();
           // g.Clear(Color.LightGray);
            int points = 0;
            for (points = 0; points <= myarrays.Kmeangroup1x.Count() - 1; points++)
            {
                Size s = new Size(10, 10);
                Point p = new Point((((Convert.ToInt32(myarrays.Kmeangroup1y[points]) * chartvalues.maxwidth) / chartvalues.ymaxvalue) + 50), ((chartvalues.maxheight - (Convert.ToInt32(myarrays.Kmeangroup1x[points]) * chartvalues.maxheight) / chartvalues.xmaxvalue) + 50));

                Rectangle r2 = new Rectangle(p, s);
                Region r = new Region(r2);
                g.FillRegion(Brushes.Red, r);
                g.DrawRectangle(Pens.Black, r2);
                
            }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users