如何让Designer.cs访问C#中的其他类

时间:2017-09-02 22:20:12

标签: c# winforms printing print-preview

后面的故事是,为了组织我的代码,我创建了一个名为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#,但这是我班级的一个项目。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

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

因此,您只需将事件处理程序 DVPrintDocument_PrintPage 添加回主表单,就像之前一样。但是没有人阻止你在调用Printing类中的代码的事件处理程序中编写内容。

像这样的东西

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

sock = socket.socket()
sock.bind((hostaddr, port))
sock.listen(backlog)
print(f'Server listenning on {hostaddr}:{port}')

while True:
    client_sock, client_address = self.sock.accept()
    print(f'Incoming connection from {client_address[0]}:{client_address[1]}')

    while True:
        data = client_socket.recv(buffer_size)
        if not data:
           break

        print(f'Received "{data.decode()}" from {client_address[0]}:{client_address[1]}')    

        reply = f'Server: I got the message "{data.decode()}"'.encode()
        client_socket.sendall(reply)

    client_socket.close()

在您的印刷课程

sock = socket.socket()
sock.connect(server_address)
sock.sendall('Lorem Ipsum'.encode())

while True:
    data = sock.recv(buffer_size)
    if not data:
        break
    print(data.decode())

sock.close()