"索引和长度必须指代字符串中的位置。"

时间:2015-09-09 17:37:22

标签: c# winforms

我在这条线上遇到错误。

float cash = float.Parse(txtCash.Text.Substring(1, 5));

我知道这个问题已经被问到了,但我仍然在这条线路上遇到错误。 如果您完全按照此链接中所示执行操作,则仍会出现错误。 http://pastebin.com/zNSxW4VN

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnAddItem_Click(object sender, EventArgs e)
    {
        listBox1.Items.Add(txtName.Text.PadRight(30) + txtPrice.Text);
        txtName.Text = "";
        txtPrice.Text = "";
    }

    private void btnRemoveItem_Click(object sender, EventArgs e)
    {
        try
        {
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        }
        catch (Exception)
        {

            return;
        }
    }


     public void CreateReceipt(object sender,     System.Drawing.Printing.PrintPageEventArgs e)
    {

        int total = 0;
        float cash = float.Parse(txtCash.Text.Substring(1, 5));
        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(" Wangoland Coffee Shop", 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 listBox1.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);


            totalprice += productPrice;

            if (productDescription.Contains("  -"))
            {
                string productLine = productDescription.Substring(0, 24);

                graphic.DrawString(productLine, new Font("Courier New", 12, FontStyle.Italic), new SolidBrush(Color.Red), startX, startY + offset);

                offset = offset + (int)fontHeight + 5; //make the spacing consistent
            }
            else
            {
                string productLine = productDescription;

                graphic.DrawString(productLine, font, new SolidBrush(Color.Black), startX, startY + offset);

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

        }

        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 custom,", 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);

    }

    private void btnPrintReciept_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();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果有效,请尝试此操作。     float cash = float.Parse(txtCash.Text.ToString());