是按照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);
            }
        }
第一次在这里发布求助,不到之处请谅解!

解决方案 »

  1.   

    查看一下是否对lines进行过初始化。
      

  2.   

    成员变量已经定义了。
     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);
                }
            }
      

  3.   

    问题解决了
    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[] ,谢谢楼上。