C# 自动生成的方法。如何添加附加参数

标签 c#

为本地企业开发收据打印机应用程序。从技术上讲,这是一次重写,我将其视为一次完整的重构。

原始代码如下所示:

private void btn_print_Click(object sender, EventArgs e)
        {

        PrintDialog pd = new PrintDialog();
        PrintDocument doc = new PrintDocument();
        pd.Document = doc;
        doc.PrintPage += new PrintPageEventHandler(pd_printpage);
        DialogResult res = pd.ShowDialog();
        if (res == DialogResult.OK)
        {
            doc.DefaultPageSettings.Landscape = false;
            doc.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Env10", 4, 8);
            doc.Print();
        }

    }
    private void pd_printpage(object sender, PrintPageEventArgs e)
    {
        Graphics gfx = e.Graphics;
        SolidBrush blk = new SolidBrush(Color.Black);
        Font courier = new Font("Courier New", 12);
        Font lucidia = new Font("Lucida Sans", 12);
        Font lucidaplus = new Font("Lucida Sans", 18);
        StringFormat fmt = new StringFormat(StringFormatFlags.DirectionVertical);

        /*****  Create Object for Reciept  *****/
        Recpt rcpt = new Recpt();
        rcpt.Contrib = new ContInfo();
        rcpt.Pri = new Address();
        rcpt.Alt = new Address();

//--- Remainder of function omitted.

这是我目前正在研究的更好的分解版本。

static class Printality
    {
        private static SolidBrush Blk = new SolidBrush(Color.Black);
        private static Font CourierBody = new Font("Courier New", 12);
        private static Font LucidaBody = new Font("Lucida Sans", 12);
        private static Font LucidaHeader = new Font("Lucida Sans", 18);
        private static StringFormat fmt;

        public static void PrintReciept(Receipt _rec)
        {
            PrintDialog pd = new PrintDialog();
            PrintDocument doc = new PrintDocument();
            doc.PrintPage += new PrintPageEventHandler(pd_printPage);
        }

        private static void pd_printPage(object sender, PrintPageEventArgs e)
        {
            Graphics gfx = e.Graphics;
            fmt = new StringFormat(StringFormatFlags.DirectionVertical);

        }
    }

最大的问题是:我将 Receipt 对象传递给函数 printReceipt() 我应该如何将它传递给 pd_printPage()

最佳答案

如果我清楚地理解你的问题,你想将参数传递给事件。

doc.PrintPage += (sender, e) => pd_printPage(sender, e, _rec);

private static void pd_printPage(object sender, PrintPageEventArgs e, Receipt rec)
{
    Graphics gfx = e.Graphics;
    fmt = new StringFormat(StringFormatFlags.DirectionVertical);

}

关于C# 自动生成的方法。如何添加附加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48347330/

相关文章:

c# - 如何为抽象类中的只读字段赋值?

c# - SignalR:使用 moq 框架模拟 IHub 并测试他的方法

c# - WPF 将 ObservableCollection 绑定(bind)到 UserControl 的意外行为

c# - 从共享某些部分的 2 个接口(interface)实现一个类

c# - 使按钮打开超链接

c# - 从 MSMQ 日志读取消息队列发送时间

c# 从 Outlook 收件箱转发电子邮件

c# - 数据表中的复数

c# - WPF 样式/模板仅应用于最后添加的元素

c# - 如何调整图表的大小超过屏幕分辨率?