错误太长,无法输入标题。所以这就是它所说的:
错误 1 无法通过嵌套类型“WindowsFormsApplication3.Form1.PCPrint”访问外部类型“WindowsFormsApplication3.Form1”的非静态成员 C:\Users\Andrew\documents\visual studio 2010\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs 407 39 Windows窗体应用程序3
我正在尝试打印我的表单,但是它无法识别文本框 txtName.Text。为什么?这是代码:
public class PCPrint : System.Drawing.Printing.PrintDocument
{
private Font _font;
private string _text;
private string TextToPrint
{
get { return _text; }
set { _text = value; }
}
public Font PrinterFont
{
get { return _font; }
set { _font = value; }
}
public PCPrint()
: base()
{
_text = string.Empty;
}
public PCPrint(string str)
: base()
{
_text = str;
}
protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint(e);
if (_font == null)
{
_font = new Font("Times New Roman", 10);
}
}
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
int printHeight;
int printWidth;
int leftMargin;
int rightMargin;
Int32 lines;
Int32 chars;
{
printHeight = base.DefaultPageSettings.PaperSize.Height -
base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
printWidth = base.DefaultPageSettings.PaperSize.Width -
base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
leftMargin = base.DefaultPageSettings.Margins.Left;
rightMargin = base.DefaultPageSettings.Margins.Right;
}
if (base.DefaultPageSettings.Landscape)
{
int tmp;
tmp = printHeight;
printHeight = printWidth;
printWidth = tmp;
}
Int32 numLines = (int)printHeight / PrinterFont.Height;
RectangleF printArea = new RectangleF(leftMargin, rightMargin, printWidth, printHeight);
StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
e.Graphics.MeasureString(_text.Substring(RemoveZeros(curChar)), PrinterFont, new SizeF(printWidth, printHeight), format, out chars, out lines);
e.Graphics.DrawString(_text.Substring(RemoveZeros(curChar)), PrinterFont, Brushes.Black, printArea, format);
curChar += chars;
if (curChar < _text.Length)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
curChar = 0;
}
}
public int RemoveZeros(int value)
{
switch (value)
{
case 0:
return 1;
default:
return value;
}
}
public void PrintDocument()
{
PCPrint printer = new PCPrint();
printer.PrinterFont = new Font("Verdana", 10);
printer.TextToPrint = txtName.Text; //ERROR IS HERE
printer.Print();
}
private void btnCreate_Click(object sender, EventArgs e)
{
PrintDocument();
}
}
}
}