C# 使用受保护的变量对类进行编码,以供其他方法访问

C# 使用受保护的变量对类进行编码,以供其他方法访问,c#,winforms,C#,Winforms,我在创建可由外部方法访问和更新的受保护变量时遇到问题 换句话说,我有一个打印多页的打印方法 我需要一个外部变量来初始化这个方法并跟踪页面 有没有人能让我开始编写这门课?正如我所知,OOP的知识是有限的 下面是打印方法 提前谢谢 private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { Graphics graphics = e.Graphics; int ypo

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

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

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

有没有人能让我开始编写这门课?正如我所知,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();
}
private void printDocument1\u PrintPage(对象发送方,PrintPageEventArgs e)
{            
图形=e.图形;
int-ypos=78;
Font f1=新字体(“Arial”,14,FontStyle.Bold,GraphicsUnit.Pixel);
笔刷=新的SolidBrush(颜色为浅灰色);
填充矩形(画笔,新矩形(10,10770,50));
钢笔黑色钢笔=新钢笔(颜色为黑色);
钢笔灰钢笔=新钢笔(颜色为浅灰色);
钢笔红笔=新钢笔(颜色为红色);
绘图矩形(黑笔,新矩形(10,10770,50));
笔刷B=新的SolidBrush(listView1.ForeColor);
图形.抽绳(黑笔,10,10,101132);
图形。拉丝(“名字”,f1,画笔。黑色,新点(20,25));
图形.抽绳(黑笔,130,10,130,1132);
图形。抽绳(“姓氏”,f1,画笔。黑色,新点(140,25));
图形.抽绳(blackpen,290,102901132);
图形。抽绳(“EXT.”,f1,画笔。黑色,新点(300,25));
图形.抽绳(黑笔,380,103801132);
图形。拉丝(“职务”,f1,画笔。黑色,新点(410,25));
图形.抽绳(黑笔,780,107801132);
int[]X={15,140,300,390,720};
int Y=60;
f1=listView1.Font;
对于(int I=0;I1132){
e、 HasMorePages=true;
返回;
}
graphics.Dispose();
}

尝试下面的函数,并调用打印事件

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

谢谢这个。。。我已将您的代码复制到我的程序中,但无法确定它将如何执行。尽管如此,我还是会尝试一下并公布结果。