简体   繁体   English

如何获取Designer.cs以访问C#中的其他类

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

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. 后面的故事是,为了组织代码,我创建了一个名为Printing.cs的新类,并将代码从main.cs中转储到了那里。 In my designer.cs I have this: this.DVPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.DVPrintDocument_PrintPage); 在我的designer.cs中,我有: 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? 如何使Designer.cs可以访问其他类?

This is the class I want the designer.cs to look at: 这是我希望designer.cs查看的类:

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@email.com", 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. 抱歉,我仍在学习C#,但这是我学校上课的一个项目。 Any help would be gladly appreciated! 任何帮助将不胜感激!

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. 由WinForms设计器生成formXXXX.designer.cs文件,以保留您的用户界面对象并使用通过Winform Designer界面进行的设置来初始化其属性。
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. 由于此类文件是由WinForms基础结构创建和维护的,因此您不应尝试添加自己的代码或在此处手动更改任何内容。
(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. 因此,您只需要像以前一样将事件处理程序DVPrintDocument_PrintPage重新添加到您的主窗体中。 But none blocks you from writing something in your event handler that calls the code in the Printing class. 但是,没有哪个函数可以阻止您在事件处理程序中编写某些东西来调用Printing类中的代码。

Something like this 像这样

In your main form.cs file readd the event handler for DVPrintDocument 在您的主要form.cs文件中,读取了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 
       .....
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM