打印预览错误 [英] print preview error

查看:100
本文介绍了打印预览错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在打印预览中有一个错误,这是我的PrintDocument代码:

Hi,
i have a bug in print preview this is my PrintDocument code:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            Font printFont = this.rtbWord.Font;
            SolidBrush myBrush = new SolidBrush(Color.Black);
            linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);

            while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
            {
                yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
                // draw the next line in the rich edit control
                e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
            }
            // If there are more lines, print another page.
            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
            myBrush.Dispose();
        }


这是我的PrintPreview代码:


and this is my PrintPreview code:

private void printPreView_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog PrintPreviewDialog = new PrintPreviewDialog();
            PrintPreviewDialog.Document = printDocument1;
            if (PrintPreviewDialog.ShowDialog() == DialogResult.OK)
            {
                printDocument1.Print();
            }
        }


while (count < linesPerPage && ((line = myReader.ReadLine()) != null))


行中 我有此错误:


i have this error:

对象引用未设置为对象的实例.

Object reference not set to an instance of an object.


请帮助我.


plz help me.

推荐答案

由于您没有向我们展示更多代码,我的猜测是,您没有像这样实例化myReader的实例.样本:
Since you don''t show us more of your code, my guess is, that you did not instantiate the instance of myReader as in this sample:
private StringReader myReader;
public Form1()
{
   InitializeComponent();
   string line = myReader.ReadLine(); //throws: Object reference not set to an instance of an object.
}


要解决该问题,您需要像本示例中一样创建StringReader对象,但是需要用内容填充它.


To solve that issue you need to create the StringReader object as in this sample, but you need to fill it with your content.

private StringReader myReader;
public Form1()
{
   InitializeComponent();
   myReader = new StringReader("HelloWorld");
   string line = myReader.ReadLine();
}


这篇关于打印预览错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆