E
ebxca
Unregistered / Unconfirmed
GUEST, unregistred user!
用C# 作 aps.net 程序
以下代码(在打印机上打印),出错,不知该怎样???
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Printing;
using System.IO;
namespace WebApplication4
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.ComponentModel.Container components;
protected System.Web.UI.WebControls.Button Button1;
protected Font printFont;
protected StreamReader streamToPrint;
private void Page_Init(object sedner, EventArgs e)
{
InitializeComponent();
}
private void Page_Load(object sedner, EventArgs e)
{
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Button1.Click += new stem.EventHandlerthis.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void pd_printPage(object sender,PrintPageEventArgs ev)
{
float lpp =0 ;
float ypos =0;
int counter =0;
float leftMar = ev.MarginBounds.Left;
float topMar = ev.MarginBounds.Top;
string textline = null;
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
while (counter < lpp &&
((textline=streamToPrint.ReadLine())!= null))
{
ypos = topMar+(counter * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawStringtextline,printFont,Brushes.Black,leftMar,y pos,new StringFormat());
counter++;
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
try
{
StreamReader streamToPrint=new StreamReader(@"c:/abc.txt");
try
{
Font PrintFont = new Font("Arial",12);
PrintDocument pd = new PrintDocument();
pd.PrintPage+=new PrintPageEventHandler(this.pd_printPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
Label1.Text ="Error printing file:" + ex.ToString();
}
}
}
}
出错提示(也就是Label1.Text的内容)为:
Error printing file:System.NullReferenceException: 未将对象引用设置到对象的实例。 at WebApplication4.WebForm1.pd_printPage(Object sender, PrintPageEventArgs ev) in c:/inetpub/wwwroot/webapplication4/webform1.aspx.cs:line 68 at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintController.PrintLoop(PrintDocumentdo
cument) at System.Drawing.Printing.PrintController.Print(PrintDocumentdo
cument) at System.Drawing.Printing.PrintDocument.Print() at WebApplication4.WebForm1.Button1_Click(Object sender, EventArgs e) in c:/inetpub/wwwroot/webapplication4/webform1.aspx.cs:line 87
我刚学c#,水平有限,望高手指点!
以下代码(在打印机上打印),出错,不知该怎样???
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Printing;
using System.IO;
namespace WebApplication4
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.ComponentModel.Container components;
protected System.Web.UI.WebControls.Button Button1;
protected Font printFont;
protected StreamReader streamToPrint;
private void Page_Init(object sedner, EventArgs e)
{
InitializeComponent();
}
private void Page_Load(object sedner, EventArgs e)
{
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Button1.Click += new stem.EventHandlerthis.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void pd_printPage(object sender,PrintPageEventArgs ev)
{
float lpp =0 ;
float ypos =0;
int counter =0;
float leftMar = ev.MarginBounds.Left;
float topMar = ev.MarginBounds.Top;
string textline = null;
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
while (counter < lpp &&
((textline=streamToPrint.ReadLine())!= null))
{
ypos = topMar+(counter * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawStringtextline,printFont,Brushes.Black,leftMar,y pos,new StringFormat());
counter++;
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
try
{
StreamReader streamToPrint=new StreamReader(@"c:/abc.txt");
try
{
Font PrintFont = new Font("Arial",12);
PrintDocument pd = new PrintDocument();
pd.PrintPage+=new PrintPageEventHandler(this.pd_printPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
Label1.Text ="Error printing file:" + ex.ToString();
}
}
}
}
出错提示(也就是Label1.Text的内容)为:
Error printing file:System.NullReferenceException: 未将对象引用设置到对象的实例。 at WebApplication4.WebForm1.pd_printPage(Object sender, PrintPageEventArgs ev) in c:/inetpub/wwwroot/webapplication4/webform1.aspx.cs:line 68 at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintController.PrintLoop(PrintDocumentdo
cument) at System.Drawing.Printing.PrintController.Print(PrintDocumentdo
cument) at System.Drawing.Printing.PrintDocument.Print() at WebApplication4.WebForm1.Button1_Click(Object sender, EventArgs e) in c:/inetpub/wwwroot/webapplication4/webform1.aspx.cs:line 87
我刚学c#,水平有限,望高手指点!