dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Why does the Form_Paint event run continuously and only draw once ?

  1. #1
    Join Date
    Sep 2007
    Posts
    340

    Why does the Form_Paint event run continuously and only draw once ?

    I have the following code, I put a label in Form_Paint to track the number jump, I see the label jumping continuously but e.Graphics.DrawString(...) only looks once, does anyone know why ?

    Code:
    public partial class Form1 : Form
        {       
            public Form1()
            {
                InitializeComponent();
                //progressBar1.Visible = false;
                progressBar1.Minimum = 0;
                progressBar1.Maximum = 100;
    
                this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    
            }
            int iNum = 0;       
            private void btnDrawText_Click(object sender, EventArgs e) //PaintEventArgs e
            {           
                progressBar1.Value = 0;
                progressBar1.Visible = true;
                this.timer1.Interval = 100;
                this.timer1.Enabled = true;           
                           
            }
            private void ShadowedTextPaint(PaintEventArgs e, int num)
            {
                using (Font font1 = new Font("Times New Roman", 250, FontStyle.Bold, GraphicsUnit.Pixel))
                {
                    PointF pointF1 = new PointF(310, 270);
                    e.Graphics.DrawString(num.ToString(), font1, Brushes.LightGreen, pointF1);               
                    lblNum.Text = num.ToString();
                }           
               
            }
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                ShadowedTextPaint(e, iNum);
            }
            private void timer1_Tick(object sender, EventArgs e)
            {           
                if (progressBar1.Value < 100)
                {               
                    Random rd = new Random();
                    iNum = rd.Next(0, 999);               
                    progressBar1.Value++;
                }
                else
                {               
                    this.timer1.Enabled = false;
                    progressBar1.Visible = false;               
                }
               
            }
      }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: Why does the Form_Paint event run continuously and only draw once ?

    Do you know how to use a debugger?

  3. #3
    Join Date
    Sep 2007
    Posts
    340

    Re: Why does the Form_Paint event run continuously and only draw once ?

    Of course I know how to debugger, many ways, the simple way I'm doing in the above example is to use labels

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: Why does the Form_Paint event run continuously and only draw once ?

    Quote Originally Posted by dongtrien View Post
    Of course I know how to debugger, many ways, the simple way I'm doing in the above example is to use labels
    What do labels have to do with stepping through code in a debugger?

  5. #5
    Join Date
    Sep 2007
    Posts
    340

    Re: Why does the Form_Paint event run continuously and only draw once ?

    Depending on the case of troubleshooting, but using step by step debugging or running continuously to get the results, my case is needed to get the results of the Form_Paint event to run ? So do you edit my code to make it work ?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: Why does the Form_Paint event run continuously and only draw once ?

    Quote Originally Posted by dongtrien View Post
    Depending on the case of troubleshooting, but using step by step debugging or running continuously to get the results, my case is needed to get the results of the Form_Paint event to run ? So do you edit my code to make it work ?
    You need to use debugging techniques to isolate the problem. I can't do it for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)