简体   繁体   English

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

[英]Return input to adjacent output label after loop to calculate values

I am getting the correct results but I would like to be specific with the calculated values displayed for each label that is adjacent to each input. 我得到了正确的结果,但是我想具体说明与每个输入相邻的每个标签显示的计算值。

Currently using two line breaks & it seems to line up with inputs by luck. 当前使用两个换行符,并且似乎与运气输入对齐。 在此处输入图片说明 Program calculates book cost based on the number of pages printed: 程序根据打印的页数计算书籍费用:

1 - 500 pages 2 cents per page 1-5500页每页2美分

500 - 100 pages 1.5 cents per page 500-100页每页1.5美分

1000+ pages 1 cent per page 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");
    }
}

Desired result: 所需结果:

Book 1 Pages Input - Book 1 Total Print Cost Label 书本1页面输入-书本1总打印成本标签

Book 2 Pages Input - Book 2 Total Print Cost Label 书本2页面输入-书本2总打印成本标签

Book 3 Pages Input - Book 3 Total Print Cost Label 书本3页输入-书本3总打印成本标签

Button 'Book Print Cost' - Grand Total Print Cost Label 按钮“书籍印刷成本”-总计印刷成本标签

Use an array of labels that match your labels to the right of the inputs: 使用与输入右边的标签匹配的标签数组:

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");
    }
}

I was quite confused by the picture you supplied and the code that is posted. 您提供的图片和所发布的代码使我感到非常困惑。 From the first picture it “Appears” you are using three (3) labels to the right of each text box. 从第一张图片“出现”开始,您正在每个文本框的右侧使用三(3)个标签。 However the supplied code only uses one label ie.. lblBook1Price . 但是,提供的代码仅使用一个标签,即lblBook1Price This is confusing to use this label for book 1 for all the books. 将该标签用于所有书籍的书籍1会造成混淆。 You are correct is certainly blind luck that they line up properly. 您正确的肯定是他们排好队的盲目运气。 Remove one of “\\n”'s in the line: lblBook1Price.Text += "Print Cost: " + (arrPrintPageNums[i] * 1) + "\\n\\n"; 删除以下行中的“ \\ n”之一: lblBook1Price.Text += "Print Cost: " + (arrPrintPageNums[i] * 1) + "\\n\\n"; and it won't line up properly. 而且无法正确排列。

Again this seems odd and not very intuitive to others who are reading your code. 同样,对于正在阅读您的代码的其他人来说,这似乎很奇怪并且不是很直观。 One solution would be to simply use 3 different labels as the first picture shows, one for each text box. 一种解决方案是在第一张图片中简单地使用3个不同的标签,每个文本框使用一个标签。 At least you are not using the book 1 label to output the book 3 costs. 至少您没有使用书1标签输出书3成本。 I hope this makes sense. 我希望这是有道理的。

The other problem I see is user sanitation, your code wil crash if the user types in letters and not numbers. 我看到的另一个问题是用户环境卫生,如果用户键入字母而不是数字,则代码将崩溃。 I hooked up two events to handle this. 我联系了两个事件来处理这个问题。 KeyPressed event to allow only numeric input and the TextChanged event if the user copy and pastes invalid text. 如果用户复制并粘贴了无效文本,则KeyPressed事件仅允许数字输入,而TextChanged事件仅允许用户输入。 Hope this helps. 希望这可以帮助。

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 = "";
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM