6 Replies - 338 Views - Last Post: 21 October 2019 - 09:52 PM Rate Topic: *---- 2 Votes

#1 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Can I convert EventArgs type to PaintEventArgs type ?

Posted 17 October 2019 - 09:09 PM

I want to convert EventArgs type to PaintEventArgs type, cast button_click to form_paint, how do I write code ? You see my error code

namespace DrawText_Random
{
    public partial class Form2 : Form
    {        
        public Form2()
        {
            InitializeComponent();
            //progressBar1.Visible = false;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;

            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form9_Paint);

        }

        int valueforShadowedTextPaint = 0;
        private void btnDrawText_Click(object sender, EventArgs e) //PaintEventArgs e
        {
            progressBar1.Value = 0;
            this.timer1.Interval = 100;
            this.timer1.Enabled = true;            
                       
        }

        private void ShadowedTextPaint(PaintEventArgs e, int num) 
        {            
            // Set up the font
            Font fnt = new Font("Calibri", 296, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Point);

            // Get the Form's Graphics object
            Graphics g = e.Graphics;

            // Draw background text
            g.DrawString(num.ToString(), fnt, Brushes.DarkGray, 244, 104);

            // Draw main text slightly offset from shadow text
            g.DrawString(num.ToString(), fnt, Brushes.Aqua, 240, 100);

        }
                

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value < 100)
            {
                PaintEventArgs g = (PaintEventArgs)e;//Error here: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.PaintEventArgs'.
                Random rd = new Random();
                Random rd = new Random();
                int Number = rd.Next(0, 999);               
                //This place must call ShadowedTextPaint (...) function to jump continuously 
                ShadowedTextPaint(e, Number);                
                progressBar1.Value++;
            }
            else
            {
                this.Invalidate(); 
                this.timer1.Enabled = false;
            }
        }
               
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Can I convert EventArgs type to PaintEventArgs type ?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,588
  • Joined: 12-June 08

Re: Can I convert EventArgs type to PaintEventArgs type ?

Posted 17 October 2019 - 10:40 PM

No, those are two different types.

Why would you want to do that?
Was This Post Helpful? 0
  • +
  • -

#3 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Re: Can I convert EventArgs type to PaintEventArgs type ?

Posted 18 October 2019 - 12:05 AM

I want to output random numbers to the screen at a specified time, haven't you seen my code ?

This post has been edited by Skydiver: 18 October 2019 - 03:51 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Can I convert EventArgs type to PaintEventArgs type ?

Posted 18 October 2019 - 03:55 AM

Okay, but why would you cast a click event handler into a paint event handler to do that?

The key problem here is that you are embedding your all your code into event handlers, and then later you discover that you need two or more different handlers to do the same thing, you are stuck with duplicating the code. The correct thing to do is refactor out that common code into a method, and then have both event handlers call that common code. It looks like you've already started down this path, but didn't quite go all the way.

One of the things you'll discover as you go through this process of refactoring is that the paint event has a graphics object given to you, while a click event does not. WinForms has a way for you to create a graphics object for you form/control.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Can I convert EventArgs type to PaintEventArgs type ?

Posted 18 October 2019 - 06:01 PM

Cross-posted to C# Corner.

And looks like our OP is getting answers on his crosspost over at MSDN.
Was This Post Helpful? 0
  • +
  • -

#6 dongtrien   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 17-June 15

Re: Can I convert EventArgs type to PaintEventArgs type ?

Posted 21 October 2019 - 07:31 PM

View PostSkydiver, on 18 October 2019 - 06:01 PM, said:

Cross-posted to C# Corner.

And looks like our OP is getting answers on his crosspost over at MSDN.


I searched on google without a solution, so I searched more on other computer forums but there was no forum showing how to solve this problem. You wonder if my "Cross-posted to C# Corner" is natural
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,588
  • Joined: 12-June 08

Re: Can I convert EventArgs type to PaintEventArgs type ?

Posted 21 October 2019 - 09:52 PM

Event args are event args, and paint args are paint args. You don't convert them.

As it was mentioned above, you need to think about pulling your function code into methods and feeding correct things to them. Straighten out your chain of events.

The timer ticks, so what? Do you think you should be calling the repaint? Would the 'paint' event benefit from having the IF progress value in there, and perhaps some random number generating? You know.. so you have the paint event arg for graphics object to use on hand?

I think it would.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1