Graphics and Event

Jan 30 2010 2:14 PM

Hi !

I am trying to draw to draw a string on the window form when an  Paint event occures:

If I run the program following process will be done:

1) Ten Paint events will be generated and fired
2) For each fired Paint event one Number will be assigned
3) The corresponding Number has be drawed on the form

Problem: If  I run the program only the Number assigned to the last fired event will be drawed on the form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ControlPaint
{
    public partial class Form2 : Form
    {
       
        private volatile int Number;
      
        public event PaintEventHandler Paint;

        public Form2()
        {
            InitializeComponent();

            Paint+=new PaintEventHandler(ProcessPaint);
            GenPaint();



        }

        /// <summary>
        ///  Methode that fires an event
        /// </summary>
        /// <param name="pe"></param>

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            if (Paint != null) Paint(new object(), pe);
        }



        /// <summary>
        ///  Methode that generates  the events
        /// </summary>
        private void GenPaint()
        {

          
            Graphics g = Graphics.FromHwnd(this.Handle);


            for (int chiffre = 0; chiffre < 50; chiffre += 5)
            {
                OnPaint(new PaintEventArgs(g, new Rectangle(0, 0, chiffre, 2 * chiffre)));
                this.Number = chiffre;
            }
        }


        /// <summary>
        ///  Methode that will draw the string on the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pe"></param>
        private void ProcessPaint(object sender, PaintEventArgs pe)
        {
            this.Refresh();
          
            if(InvokeRequired)
            {
                this.Invoke(new PaintEventHandler(ProcessPaint), new object[] { sender, pe });
                return;
            }


            Graphics g = pe.Graphics;
          
           g.DrawString(this.Number.ToString(), new Font("Arial",14 ,FontStyle.Bold),
                                new SolidBrush(Color.Blue), this.Number, this.Number * 2);
          
          
          
           
        }




    }

}


Thanks in advance for the help !


Attachment: ControlPaintForm2.zip