是按照C#经典教程(第三版)中做的例子。
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
while( linesPrinted < lines.Length )//程序在运行到这里时出错。提示:未将对象引用到实例
{
e.Graphics.DrawString(lines[linesPrinted++] , new Font("Arial", 10), Brushes.Black, x, y);
y += 15;
if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
}
linesPrinted = 0;
e.HasMorePages = false; } private void miFilePrint_Click(object sender, EventArgs e)
{
try
{
if (DlgPrint.ShowDialog() == DialogResult.OK)
{
printDocument.Print();
}
}
catch(InvalidPrinterException ex)
{
MessageBox.Show(this,ex.Message,"本页面",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
第一次在这里发布求助,不到之处请谅解!
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
while( linesPrinted < lines.Length )//程序在运行到这里时出错。提示:未将对象引用到实例
{
e.Graphics.DrawString(lines[linesPrinted++] , new Font("Arial", 10), Brushes.Black, x, y);
y += 15;
if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
}
linesPrinted = 0;
e.HasMorePages = false; } private void miFilePrint_Click(object sender, EventArgs e)
{
try
{
if (DlgPrint.ShowDialog() == DialogResult.OK)
{
printDocument.Print();
}
}
catch(InvalidPrinterException ex)
{
MessageBox.Show(this,ex.Message,"本页面",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
第一次在这里发布求助,不到之处请谅解!
解决方案 »
- 求助C#与SQL Server2008的连接字符串问题
- C#下string.Remove和Replace()无效
- c# winform datagridview 单元格固定
- c#数据库连接出现错误
- 问一下用C#发送邮件在接收邮件的时候怎么才能让内容是超链接的?
- 一个页面一次查询
- 有个问题在vb版问了,vb版高手太少,特来这问问
- 请高手指点:datagridview内有一个列类型为:DataGridViewComboBoxColumn
- 关于循环停止的问题
- C# ACCeSS Update 标准表达式中数据类型不匹配?
- 有没有那种在windows程序中显示网页的控件(网页的内容可编辑)
- 关于WINDOWS写的网站后台管理程序的问题,请高人们都进来看一下
public partial class SimpleEditorForm : Form
{
private string filename = "Untitled";
private string[] lines;
private int linesPrinted; public SimpleEditorForm(string filename)
{
InitializeComponent();
if (filename != null)
{
this.filename = filename;
OpenFile();
}
}
private void OnBeginPrint(object sender, PrintEventArgs e)
{
char[] param ={ '\n' };
string[] lines = TextBoxEdit.Text.Split(param);
int i = 0;
char[] trimParam ={ '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
private void OnBeginPrint(object sender, PrintEventArgs e)
{
char[] param ={ '\n' };
string[] lines = TextBoxEdit.Text.Split(param);
int i = 0;
char[] trimParam ={ '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
多了一个string[] ,谢谢楼上。