编码具有受保护变量的类,以便通过另一种方法

时间:2018-02-02 10:41:53

标签: c# winforms

我在创建可以通过外部方法访问和更新的受保护变量时遇到问题。

换句话说,我有一个打印多个页面的打印方法。

我需要一个外部变量来初始化此方法并跟踪页面。

是否有人可以让我开始编写这门课程?因为我对OOP的了解有限

以下是打印方法

提前致谢

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{            
    Graphics graphics = e.Graphics;
    int ypos = 78;
    Font f1 = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel);
    Brush brush = new SolidBrush(Color.LightSlateGray);
    graphics.FillRectangle(brush, new Rectangle(10, 10, 770, 50));
    Pen blackpen = new Pen(Color.Black);
    Pen graypen = new Pen(Color.LightGray);
    Pen redpen = new Pen(Color.Red);
    graphics.DrawRectangle(blackpen, new Rectangle(10, 10, 770, 50));
    Brush B = new SolidBrush(listView1.ForeColor);

    graphics.DrawLine(blackpen, 10, 10, 10, 1132);
    graphics.DrawString("FORENAME", f1, Brushes.Black, new Point(20, 25));
    graphics.DrawLine(blackpen, 130, 10, 130, 1132);
    graphics.DrawString("SURNAME", f1, Brushes.Black, new Point(140, 25));
    graphics.DrawLine(blackpen, 290, 10, 290, 1132);
    graphics.DrawString("EXT.", f1, Brushes.Black, new Point(300, 25));
    graphics.DrawLine(blackpen, 380, 10, 380, 1132);
    graphics.DrawString("JOB TITLE", f1, Brushes.Black, new Point(410, 25));
    graphics.DrawLine(blackpen, 780, 10, 780, 1132);

    int[] X = { 15, 140, 300, 390, 720 }; 
    int Y = 60; 
    f1 = listView1.Font;

    for (int I = 0; I < listView1.Items.Count; I++){
        for (int J = 0; J < listView1.Items[I].SubItems.Count - 1; J++){
            graphics.DrawString(listView1.Items[I].SubItems[J].Text, f1, B, X[J], Y);
        }
    }

    Y += f1.Height;
    graphics.DrawLine(graypen, 10, ypos, 780, ypos);
    ypos = ypos + 17;

    if (ypos > 1132){
        e.HasMorePages = true;
        return;
    }

    graphics.Dispose();
}

1 个答案:

答案 0 :(得分:0)

尝试以下功能,并调用打印事件

public void PrintDocument()
{
    try
    {              
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler
               (this.printDocument1_PrintPage);
            pd.Print();            
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}