您好我有药房购买表格,但打印/确认按钮有错误。打印时发生错误。我刚刚在网上找到了这个代码,我想用它。
错误来自float cash = float.Parse(txBCash.Text.Substring(1, 3));
这是代码
private void btnConfirm_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
printDialog.Document = printDocument; //add the document to the dialog box...
printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateReceipt); //add an event handler that will do the printing
//on a till you will not want to ask the user where to print but this is fine for the test envoironment.
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.Print();
}
}
public void CreateReceipt(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int total = 0;
float cash = float.Parse(txBCash.Text.Substring(1, 3));
float change = 0.00f;
//this prints the reciept
Graphics graphic = e.Graphics;
Font font = new Font("Courier New", 12); //must use a mono spaced font as the spaces need to line up
float fontHeight = font.GetHeight();
int startX = 10;
int startY = 10;
int offset = 40;
graphic.DrawString(" Kwem Drugstore", new Font("Courier New", 18), new SolidBrush(Color.Black), startX, startY);
string top = "Item Name".PadRight(30) + "Price";
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;
foreach (string item in listViewOrders.Items)
{
//create the string to print on the reciept
string productDescription = item;
string productTotal = item.Substring(item.Length - 6, 6);
float productPrice = float.Parse(item.Substring(item.Length - 5, 5));
//MessageBox.Show(item.Substring(item.Length - 5, 5) + "PROD TOTAL: " + productTotal);
}
change = (cash - totalprice);
//when we have drawn all of the items add the total
offset = offset + 20; //make some room so that the total stands out.
graphic.DrawString("Total to pay ".PadRight(30) + String.Format("{0:c}", totalprice), new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + 30; //make some room so that the total stands out.
graphic.DrawString("CASH ".PadRight(30) + String.Format("{0:c}", cash), font, new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + 15;
graphic.DrawString("CHANGE ".PadRight(30) + String.Format("{0:c}", change), font, new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + 30; //make some room so that the total stands out.
graphic.DrawString(" Thank-you for your purchase,", font, new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + 15;
graphic.DrawString(" please come back soon!", font, new SolidBrush(Color.Black), startX, startY + offset);
}
}
我不知道这是否正确或我是否正确地做到了。但请帮助我解决这个问题,因为我们学校的项目。
非常感谢任何类型的回复。提前谢谢。
答案 0 :(得分:1)
C#中的数组索引从零开始,而不是1.并且Substring方法的第二个参数不是结束位置,而是要检索的字符长度。因此,您的代码会从输入的第二个字符开始,从txBCash文本框中请求3个字符。如果你想获得前三个字符,那么你应该写
float cash = float.Parse(txBCash.Text.Substring(0, 3));
但更好的方法是通过float.TryParse,当输入不是有效的float时不会触发异常。当然你不能确定你的用户在那个文本框中只输入3个字符,所以我也会删除起始和长度限制
float cash = 0.0;
if(!float.TryParse(txBCash.Text, out cash))
MessageBox.Show("Invalid value for cash");