C # create drawing object

The following example is an example from MSDN:

private void Form1_Paint(object sender, 
   System.Windows.Forms.PaintEventArgs pe) 
{
   // Declares the Graphics object and sets it to the Graphics object
   // supplied in the PaintEventArgs.
   Graphics g = pe.Graphics;
   // Insert code to paint the form here.
}

      

I have several questions:

  • Can I change the name of the method Form1_Paint

    ? I mean, should it have the "Paint" suffix? When does the .net method call this method? How does the framework know which method to call so that it can draw images?

  • I don't understand why we just define that the method Form1_Paint

    can take 2 arguments, and then magically the struct just calls the method with a reference to object

    and an object reference PaintEventArgs

    (re).

I apologize for the stupid questions, but I come from mostly functional programming and I am confused about using frameworks because they seem to call their own methods. Can someone explain this as a 6 year old?

+3


source to share


2 answers


As per the comment Form1_Paint

is an event handler for the event Paint

.

The arguments are not magic, they are required for this event - i.e. if you want to bind to this event, the handler method implementation MUST match the required event arguments. A is PaintEventHandler

defined as:

public delegate void PaintEventHandler(object sender, PaintEventArgs e);

      

By default, when you add a handler to the constructor (for example, by double-clicking on a user interface control or on the icon Events

below the lightning tab), an event handler is automatically created with a default name:

{name of the control}_{name of event}

      

In your case, your form had a name Form1

when the handler method was created.

You can rename the handler method, but if so, you will also need to change the binding of the corresponding event to Form1.designer.cs

(i.e. change this.Form1_Paint

to below):



this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

      

( +=

indicates a subscription to an event - after signing, when the event is raised, all subscription methods will be used)

Edit

Since you're coming from an FP background, you might be wondering that there is no need for an explicit named event handler, you can subscribe to a suitable typed lambda as well:

this.Paint += (sender, pe) => 
{
   // Declares the Graphics object and sets it to the Graphics object
   // supplied in the PaintEventArgs.
   Graphics g = pe.Graphics;
   // Insert code to paint the form here.
};

      

Where sender

u pe

are of the same types as before. The designer will not do this by default, so what you can do is programmatically add the above subscription to your constructorForm1

+3


source


Winforms is an event-based technology meaning events and the job of the programmer to decide what to do (if any) when the event occurs. A little magic happens to achieve this event-based programming model used by C #.

Whenever you create a new form in a Winforms project, an associated file is created .designer.cs

as well as your .cs

. .designer.cs

is an auto-generated file. It is used by Visual Studio to create the design view that you see when you design your form.



When you add a control to your form, the control is created programmatically in a file .designer.cs

and some default values ​​(such as size, name, and ID) are set. This file is then parsed by the Visual Studio designer and the designer displays a designer view based on this file .designer.cs

. You can open the file .designer.cs

and see all the controls in your form that have been programmed by the program.

Now, the physical properties of the control are not the only ones that are programmatically created in the file .designer.cs

. Events are also generated. When you create a new event, such as an event Paint

which visual studio is doing this, it goes to the file .designer.cs

and attaches an event handler to the event of that control. Then it creates a method stub in your file .cs

and attaches that method to your event in the file .designer.cs

. You can change the name to whatever you want, as long as you don't change the method parameters. The name of your method in your file .cs

must match the name in your file .designer.cs

, so you need to change the name in both places.

+2


source







All Articles