循环后将输入返回到相邻的输出标签以计算值

时间:2017-03-11 03:27:45

标签: c# arrays

我得到了正确的结果,但我想具体说明为每个输入相邻的每个标签显示的计算值。

目前使用两个换行符&它似乎与运气的输入排列在一起。 enter image description here 程序根据打印的页数计算书籍成本:

1 - 500页每页2美分

500 - 100页每页1.5美分

1000页每页1美分

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

    private void btnPriceResults_Click(object sender, EventArgs e)
    {
        //VARIABLES
        double[] arrPrintPageNums = new double[3];
        double total = 0;


        //INPUTS
        arrPrintPageNums[0] = double.Parse(txtInputBook1.Text);
        arrPrintPageNums[1] = double.Parse(txtInputBook2.Text);
        arrPrintPageNums[2] = double.Parse(txtInputBook3.Text);


        //PROCESS (i = INDEX NUMBER(0 1 2 3 4))
        for (int i = 0; i < arrPrintPageNums.Length; i++)
        {
            if (arrPrintPageNums[i] > 0 && arrPrintPageNums[i] <= 500)
            {
                lblBook1Price.Text += "Print Cost: " + (arrPrintPageNums[i] * 2) + "\n\n";
                total += arrPrintPageNums[i] * 0.02;
            }
            else if (arrPrintPageNums[i] > 500 && arrPrintPageNums[i] <= 1000)
            {
                lblBook1Price.Text += "Print Cost: " + (arrPrintPageNums[i] * 1.5) + "\n\n";
                total += arrPrintPageNums[i] * 0.015;
            }
            else if (arrPrintPageNums[i] > 1000)
            {
                lblBook1Price.Text += "Print Cost: " + (arrPrintPageNums[i] * 1) + "\n\n";
                total += arrPrintPageNums[i] * 0.01;
            }
            else
            {
                lblBook1Price.Text += "ERROR! OUTSIDE PRINT RANGE" + "\n\n";
            }

        }


        //OUTPUT
        lblBookPriceTotal.Text = "Total price: " + total.ToString("C2");
    }
}

期望的结果:

Book 1 Pages Input - Book 1 Total Print Cost Label

Book 2 Pages Input - Book 2 Total Print Cost Label

Book 3 Pages Input - Book 3 Total Print Cost Label

按钮'图书打印成本' - 总计打印成本标签

2 个答案:

答案 0 :(得分:1)

使用与输入右侧的标签匹配的标签数组:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        bookPriceLabels = new Label[] { lblBook1Price, lblBook2Price, lblBook3Price };
    }

    private Label[] bookPriceLabels;

    private void btnPriceResults_Click(object sender, EventArgs e)
    {
        //VARIABLES
        double[] arrPrintPageNums = new double[3];
        double total = 0;


        //INPUTS
        arrPrintPageNums[0] = double.Parse(txtInputBook1.Text);
        arrPrintPageNums[1] = double.Parse(txtInputBook2.Text);
        arrPrintPageNums[2] = double.Parse(txtInputBook3.Text);

        Label[] labels = new Label


        //PROCESS (i = INDEX NUMBER(0 1 2 3 4))
        for (int i = 0; i < arrPrintPageNums.Length; i++)
        {           
            if (arrPrintPageNums[i] > 0 && arrPrintPageNums[i] <= 500)
            {
                bookPriceLabels[i].Text = "Print Cost: " + (arrPrintPageNums[i] * 2);
                total += arrPrintPageNums[i] * 0.02;
            }
            else if (arrPrintPageNums[i] > 500 && arrPrintPageNums[i] <= 1000)
            {
                bookPriceLabels[i].Text = "Print Cost: " + (arrPrintPageNums[i] * 1.5);
                total += arrPrintPageNums[i] * 0.015;
            }
            else if (arrPrintPageNums[i] > 1000)
            {
                bookPriceLabels[i].Text = "Print Cost: " + (arrPrintPageNums[i] * 1);
                total += arrPrintPageNums[i] * 0.01;
            }
            else
            {
                bookPriceLabels[i].Text = "ERROR! OUTSIDE PRINT RANGE" + "\n\n";
            }

        }


        //OUTPUT
        lblBookPriceTotal.Text = "Total price: " + total.ToString("C2");
    }
}

答案 1 :(得分:0)

我对您提供的图片和发布的代码感到很困惑。从第一张图片中“出现”,您在每个文本框的右侧使用三(3)个标签。但是,提供的代码仅使用一个标签,即...... lblBook1Price。对于所有书籍来说,将这个标签用于第1册是令人困惑的。你是正确的肯定是他们排队正确的运气。删除行中的“\ n”之一:lblBook1Price.Text += "Print Cost: " + (arrPrintPageNums[i] * 1) + "\n\n";,它将无法正常排列。

对于正在阅读代码的其他人来说,这看起来很奇怪而且不太直观。一种解决方案是简单地使用3个不同的标签,如第一张图所示,每个文本框一个。至少你没有使用书籍1标签输出书籍3的费用。我希望这是有道理的。

我看到的另一个问题是用户卫生,如果用户键入字母而不是数字,您的代码将崩溃。我连接了两个事件来处理这个问题。如果用户复制并粘贴无效文本,则仅允许数字输入的KeyPressed事件和TextChanged事件。希望这可以帮助。

private void button1_Click(object sender, EventArgs e) {
  updateForm();
}


private void updateForm() {
  double total = 0;
  //INPUTS
  double curCost = 0;
  curCost = GetBookCost(txtInputBook1, 0.02);
  lblBook1Price.Text = curCost.ToString("C2");
  total += curCost;

  curCost = GetBookCost(txtInputBook2, 0.015);
  lblBook2Price.Text = curCost.ToString("C2");
  total += curCost;

  curCost = GetBookCost(txtInputBook3, 0.01);
  lblBook3Price.Text = curCost.ToString("C2");
  total += curCost;

  //OUTPUT
  lblBookPriceTotal.Text = "Total price: " + total.ToString("C2");
}

private double GetBookCost(TextBox pagesText, double pricePerPage) {
  double totPages = 0;
  double.TryParse(pagesText.Text, out totPages);
  double cost = totPages * pricePerPage;
  return cost;
}



private void txtInputBook1_KeyPress(object sender, KeyPressEventArgs e) {
  KeyPressManager(sender, e);
}
private void txtInputBook2_KeyPress(object sender, KeyPressEventArgs e) {
  KeyPressManager(sender, e);
}
private void txtInputBook3_KeyPress(object sender, KeyPressEventArgs e) {
  KeyPressManager(sender, e);
}

private void KeyPressManager(object sender, KeyPressEventArgs e) {
  char keyPressed = e.KeyChar;
  if ((!char.IsDigit(keyPressed)) && (!char.IsControl(e.KeyChar))) {
    e.Handled = true;
  }
}

private void txtInputBook1_TextChanged(object sender, EventArgs e) {
  TextChangedManager(sender, e);
}
private void txtInputBook2_TextChanged(object sender, EventArgs e) {
  TextChangedManager(sender, e);
}

private void txtInputBook3_TextChanged(object sender, EventArgs e) {
  TextChangedManager(sender, e);
}

private void TextChangedManager(object sender, EventArgs e) {
  TextBox tb = (TextBox)sender;
  int userInput = 0;
  if (int.TryParse(tb.Text, out userInput)) {
    updateForm();
  } else {
    if (tb.Text != "") {
      MessageBox.Show("Invalid input for number of pages!");
      txtInputBook1.Text = "";
    }
  }
}