How to Get Designer.cs to access other classes in C#

929 views Asked by At

The back story is that to organize my code I made a new class called Printing.cs and dumped my code there from my main.cs. In my designer.cs I have this: this.DVPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.DVPrintDocument_PrintPage);I am getting an error message saying does not contain definition and no extension method found. How can I make it so Designer.cs can access other classes?

This is the class I want the designer.cs to look at:

using static TicketingSystem.TicketingSystem;

namespace TicketingSystem
{
   class Printing
   {
    TicketingSystem ticketingSystem;
        public Printing(TicketingSystem ticketingSystem) => 
this.ticketingSystem = ticketingSystem;


    public void DVPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Bitmap r3tsLogo = Properties.Resources.rt3slogo;
        Image image1 = r3tsLogo; //image 1 is r3tsLogo
        e.Graphics.DrawImage(image1, 350, 0, image1.Width, image1.Height);
        // e.Graphics.DrawString("Employee Name:" + employee.Text, new Font("Arial", 15, FontStyle.Regular), Brushes.Black, new Point(50, 200)); //Put to bottom of paper
        e.Graphics.DrawString("Address:", new Font("Impact", 12, FontStyle.Regular), Brushes.Black, new Point(300, 90));//change the new point to put text on different part of paper.
        e.Graphics.DrawString("Room 61", new Font("Arial", 10, FontStyle.Regular), Brushes.Black, new Point(370, 94)); //This line of code connects to Code line 151   
        e.Graphics.DrawString("Email:", new Font("Impact", 12, FontStyle.Regular), Brushes.Black, new Point(300, 120));//change the new point to put text on different part of paper.
        e.Graphics.DrawString("[email protected]", new Font("Arial", 10, FontStyle.Regular), Brushes.Black, new Point(350, 124)); //This line of code connects to Code line 154
        e.Graphics.DrawString("Date: " + DateTime.Now, new Font("Arial", 13, FontStyle.Regular), Brushes.Black, new Point(300, 150));
        e.Graphics.DrawString(ticketingSystem.dashes.Text, new Font("Arial", 12), Brushes.Black, new Point(0, 160));
      }

Sorry I am still learning C# but this a project for my school class. Any help would be gladly appreciated!

1

There are 1 answers

2
Steve On

The formXXXX.designer.cs file is generated by the WinForms designer to keep your user interface objects and initialize their properties with the settings you make through the Winform Designer interface.
As such file is created and maintained by the WinForms infrastructure you shouldn't try to add your own code or manually change anything here.
(Besides, every time you change your form, this file is rewritten)

So, you simply need to add back the event handler DVPrintDocument_PrintPage to your main form as it was before. But none blocks you from writing something in your event handler that calls the code in the Printing class.

Something like this

In your main form.cs file readd the event handler for DVPrintDocument

public void DVPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
     Printing print = new Printing(ticketingSystem);
     print.PrintPageHandler(sender, e);
}

in your Printing class

public class Printing
{
    TicketingSystem ticketingSystem;
    public Printing(TicketingSystem ticketingSystem) => 
                    this.ticketingSystem = ticketingSystem;


    public void PrintPageHandler(object sender, PrintPageEventArgs e)
    {
        Bitmap r3tsLogo = Properties.Resources.rt3slogo;
        Image image1 = r3tsLogo; //image 1 is r3tsLogo
        // remainder of your current code 
       .....
    }
}