打印文档代码错误 [英] print document code error

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

问题描述

这是我在foreach循环中的打印文档代码问题他没有运行帮助我

即时通讯使用listview来保存数据..



private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)

{



//抛出新的NotImplementedException();

Graphics graphic = e.Graphics;

Font font = new Font(Arial,12);

float fontheight = font.GetHeight ();

int startx = 10;

int starty = 10;

int offset = 40;



graphic.DrawString(Wangoland Coffee Shop,新字体(Courier New,18),新SolidBrush(Color.Black),startx,starty);

string top =Item Name.PadRight(30)+Price.PadRight(30)+Quantity.PadRight(30)+Amount;

graphic.DrawString(top,font ,新的SolidBrush(Color.Black),sta rtx,starty + offset);

offset = offset +(int)FontHeight; //使间距保持一致

graphic.DrawString(------------------------------- -------------------------------------,font,new SolidBrush(Color.Black),startx ,starty + offset);

offset = offset +(int)FontHeight + 5; //使间距保持一致



float totalprice = 0.00f;



string pn = txtProductName.Text ;

string ci = txtConfigID.Text;

string p = txtPrice.Text;



string sq = txtSaleQty.Text;

string tm = txtTotalAmount.Text;



// string productline = pn + ci + p + sq + tm;



//graphic.DrawString(。+ txtProductName.Text,new Font(Arial,12,FontStyle.Regular),Brushes.Black,startx, starty + offset);

//graphic.DrawString(。+ txtPrice.Text,new Font(Arial,12,FontStyle.Regular),Brushes.Black,startx,starty + offset );

//graphic.DrawString(。+ txtSaleQty.Text,new Font(Arial,12,FontStyle.Regular),Brushes.Black,startx,starty + offset);

//graphic.DrawString(。+ txtSubTotal.Text,new Fo nt(Arial,12,FontStyle.Regular),Brushes.Black,startx,starty + offset);





//问题从这里开始这个循环没有运行

foreach(ListView1.Items中的ListViewItem项目)

{

string productDescription = item;

string p1 = item.SubItems [1](item.Length - 6,6);





string p2 = item.SubItems [2](item.Length - 5,5);

string p3 = item.SubItems [3](item .Length - 4,4);

string p4 = item.SubItems [4](item.Length - 3,3);

string p5 = item.SubItems [ 5](item.Length - 2,2);



string productLine = productDescription;



graphic .DrawString(productLine,font,new SolidBrush(Color.Black),startx,starty + offset);



offset = offset +(int)FontHeight + 5; //使间距保持一致









//图形。 DrawString(productline,font,new SolidBrush(Color.Black),startx,starty + offset);



graphic.DrawString(Rs。+ txtSubTotal.Text, new Font(Arial,12,FontStyle.Regular),Brushes.Black,startx,starty + offset);



offset = offset +(int)FontHeight + 5;







}

解决方案

< blockquote>首先......很容易看到这类问题一步一步地调试你的代码。我们看不到剩下的代码,所以我们不可能知道什么是错的。



那就是说,我会猜测。如果未执行foreach,则有2个选项:或者ListView1没有项目,或者可能为null。我想是第一个,所以检查你如何将数据绑定到listview1。







好​​吧..现在我看一下,我认为你的代码甚至没有编译!试试这个

  foreach (ListViewItem item  in  ListView1.Items)
{
string productDescription = item.Text;
string p1 = item.SubItems [ 1 ]。文字;


string p2 = item.SubItems [ 2 ]。文本;
string p3 = item.SubItems [ 3 ]。文字;
string p4 = item.SubItems [ 4 ]。文字;
string p5 = item.SubItems [ 5 ]。文字;

string productLine = productDescription;
....



您无法将项目分配给字符串变量,您必须分配项目的文本属性。


this is my print document code problem in foreach loop he is not run help me
im using listview for holding data..

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

//throw new NotImplementedException();
Graphics graphic = e.Graphics;
Font font = new Font("Arial ", 12);
float fontheight = font.GetHeight();
int startx = 10;
int starty = 10;
int offset = 40;

graphic.DrawString(" Wangoland Coffee Shop", new Font("Courier New", 18), new SolidBrush(Color.Black), startx, starty);
string top = "Item Name".PadRight(30) + "Price".PadRight(30) + "Quantity".PadRight(30) + "Amount";
graphic.DrawString(top, font, new SolidBrush(Color.Black), startx, starty + offset);
offset = offset + (int)FontHeight; //make the spacing consistent
graphic.DrawString("--------------------------------------------------------------------", font, new SolidBrush(Color.Black), startx, starty + offset);
offset = offset + (int)FontHeight + 5; //make the spacing consistent

float totalprice = 0.00f;

string pn = txtProductName.Text;
string ci = txtConfigID.Text;
string p = txtPrice.Text;

string sq = txtSaleQty.Text;
string tm = txtTotalAmount.Text;

// string productline = pn + ci + p + sq + tm;

//graphic.DrawString("." + txtProductName.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);
//graphic.DrawString("." + txtPrice.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);
//graphic.DrawString("." + txtSaleQty.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);
//graphic.DrawString("." + txtSubTotal.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);


//problem start here this loop is not run
foreach (ListViewItem item in ListView1.Items)
{
string productDescription = item;
string p1 = item.SubItems[1](item.Length - 6, 6);


string p2 = item.SubItems[2](item.Length - 5, 5);
string p3 = item.SubItems[3](item.Length - 4, 4);
string p4 = item.SubItems[4](item.Length - 3, 3);
string p5 = item.SubItems[5](item.Length - 2, 2);

string productLine = productDescription;

graphic.DrawString(productLine, font, new SolidBrush(Color.Black), startx, starty + offset);

offset = offset + (int)FontHeight + 5; //make the spacing consistent




// graphic.DrawString(productline, font, new SolidBrush(Color.Black), startx, starty + offset);

graphic.DrawString("Rs." + txtSubTotal.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startx, starty + offset);

offset = offset + (int)FontHeight + 5;



}

解决方案

First of all.. this kind of problems are easily seen debugging your code step by step. We can't see the rest of the code,so it's impossible for us knowing what's wrong.

That said,i'll try to guess. If the foreach is not executed,there are 2 options: Or ListView1 has no items,or maybe is null. I guess is the first one, so check how you bind data to your listview1.

[EDIT]

Ok..now i look at it,i don't think your code even compiles! Try this

foreach (ListViewItem item in ListView1.Items)
{
string productDescription = item.Text;
string p1 = item.SubItems[1].Text;
 

string p2 = item.SubItems[2].Text;
string p3 = item.SubItems[3].Text;
string p4 = item.SubItems[4].Text;
string p5 = item.SubItems[5].Text;
 
string productLine = productDescription;
....


You can´t assing an item to a string variable, you must assign the text property of the item.


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

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