Paint two forms with one paint event handler : Form Event « GUI Windows Forms « C# / CSharp Tutorial






using System;
using System.Drawing;
using System.Windows.Forms;
   
class PaintTwoForms
{
     static Form form1, form2;
   
     public static void Main()
     {
          form1 = new Form();
          form2 = new Form();
   
          form1.Text      = "First Form";
          form1.BackColor = Color.White;
          form1.Paint    += new PaintEventHandler(MyPaintHandler);
   
          form2.Text      = "Second Form";
          form2.BackColor = Color.White;
          form2.Paint    += new PaintEventHandler(MyPaintHandler);
          form2.Show();
   
          Application.Run(form1);
     }
     static void MyPaintHandler(object objSender, PaintEventArgs pea)
     {
          Form     form = (Form)objSender;
          Graphics graphics = pea.Graphics;
          string   str;
   
          if(form == form1)
               str = "Hello from the first form";
          else
               str = "Hello from the second form";
   
          graphics.DrawString(str, form.Font, Brushes.Black, 0, 0);
     }
}








23.3.Form Event
23.3.1.Form size changed event handlerForm size changed event handler
23.3.2.All form eventsAll form events
23.3.3.React to form resize eventReact to form resize event
23.3.4.Paint eventPaint event
23.3.5.Paint two forms with one paint event handler
23.3.6.Paint one forms with two paint event handler
23.3.7.Form Mouse Up event
23.3.8.Form mouse move event
23.3.9.Form Key event
23.3.10.Form life time event: Closing, Load, Closed, Activated, Deactivate
23.3.11.Cancel Form Closing event
23.3.12.Form Paint eventForm Paint event