【发布时间】:2013-10-22 19:54:55
【问题描述】:
我正在尝试在 for 循环中打印,在该循环中必须打印组框,直到条件满足。
//代码:
private void btnPrint_Click(object sender, EventArgs e)
{
for (int i = 1; i <= Convert.ToInt32(lblTotalBox.Text); i++)
{
lblBoxNumber.Text = i.ToString();
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
PaperSize paperSize = new PaperSize("MyCustomSize", 100, 65);
paperSize.RawKind = (int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
using (Graphics g = e.Graphics)
{
using (new Font("Arial", 16))
{
float x = new float();
float y = new float();
x = e.MarginBounds.Left;
y = e.MarginBounds.Top;
Bitmap bmp = new Bitmap(350, 400);
grpReceipt.DrawToBitmap(bmp, new Rectangle(0, 0, 350, 400));
e.Graphics.DrawImage(bmp, x, y);
}
}
}
表单图像:
当我尝试运行上面的代码时,它不会给我任何错误,但第一次打印工作正常,其他都是空的。
我哪里错了?
【问题讨论】:
-
迭代前
Convert.ToInt32(lblTotalBox.Text返回什么? -
这将是不变的。可以是 5-10 之间的任何数字。
-
什么是 printDocument1.Print()?
-
你能在一个可能的异常上将语句包装在 try catch 和断点中吗?也许追踪.Print的电话?我的想法是 .Print 抛出一个没有发生的异常并没有使其进入 main(),因此应用程序不会崩溃。 . .(这是你的疯狂猜想!)
-
您不需要
using(Graphics g = e.Graphics) { },因为您不是创建新的图形对象,而是分配对现有图形对象的引用。此外,您不要在代码中使用g。