【问题标题】:How to Get Designer.cs to access other classes in C#如何让 Designer.cs 访问 C# 中的其他类
【发布时间】:2017-09-02 22:20:11
【问题描述】:

背后的故事是,为了组织我的代码,我创建了一个名为 Printing.cs 的新类,并将我的代码从我的 main.cs 中转储到那里。 在我的designer.cs 我有这个:this.DVPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.DVPrintDocument_PrintPage);我收到一条错误消息,说不包含定义并且没有找到扩展方法。我怎样才能让 Designer.cs 可以访问其他类?

这是我想让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));
      }

抱歉,我仍在学习 C#,但这是我学校班级的项目。任何帮助将不胜感激!

【问题讨论】:

  • 你在printing.cs中添加了designer.cs的命名空间吗?
  • 不,据我所知,我认为我做不到。如果我错了,请纠正我? @VijayanathViswanathan
  • 如果你的两个类在同一个程序集中,请添加designer.cs的命名空间。如果不在同一个程序集中,请添加库引用并添加命名空间。
  • 你能给我看一些代码吗?因为可视化帮助我更好地@VijayanathViswanathan

标签: c# winforms printing print-preview


【解决方案1】:

formXXXX.designer.cs 文件由 WinForms 设计器生成,用于保留您的用户界面对象并使用您通过 Winform Designer 界面所做的设置初始化它们的属性。
由于此类文件由 WinForms 基础架构创建和维护,因此您不应尝试在此处添加自己的代码或手动更改任何内容。
(此外,每次更改表单时,都会重写此文件)

因此,您只需像以前一样将事件处理程序 DVPrintDocument_PrintPage 添加回您的主窗体。但是没有人阻止您在调用打印类中的代码的事件处理程序中编写一些东西。

类似的东西

在您的主 form.cs 文件中读取 DVPrintDocument 的事件处理程序

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

在你的印刷课上

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 
       .....
    }
}

【讨论】:

  • 感谢您的回答。我正在做和你说的一样的事情。所有步骤都有效,除了打印打印 = new Printing(ticketingSystem);票务系统给我一个错误,说在当前上下文中不存在。我知道错误的含义,并尝试使用 TicketingSystem ticketingSystem = new TicketingSystem(); 解决该错误。但它没有用。
  • 您的打印类需要 TicketingSystem 的实例。当您调用 Printing 类的构造函数时,您应该有一个此类的实例。从提供的代码中,我可以假设这个 TicketingSystem 是调用打印类的表单的名称。在这种情况下,只需将 this 作为参数传递给 Printing 类构造函数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
相关资源
最近更新 更多