Home > other >  Sum textboxes to other textbox wpf C#
Sum textboxes to other textbox wpf C#

Time:10-07

I have i wpf form with 3 textboxes there i should write how many tickets, then i want to multiply that number with a value At the end i have another textbox there i want the sum from the 3 textboxes even if only 1 has value

i have tried this:

 private void textBox1_TextChanged(object sender, EventArgs e)
 {
  if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
  textBox3.Text = (Convert.ToInt32(textBox1.Text)   Convert.ToInt32(textBox2.Text)).ToString();
 }

 private void textBox2_TextChanged(object sender, EventArgs e)
 {
  if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
  textBox3.Text = (Convert.ToInt32(textBox1.Text)   Convert.ToInt32(textBox2.Text)).ToString();
 }

enter image description here

Cant get it to work Please help

CodePudding user response:

To convert a string value to an integer value you may use int.Parse method. On the other hand this method (also Convert.ToInt32) requires you making sure that string is always in a good format to get converted. If you are not sure and/or you know that the string may be not in correct format, you may use int.TryParse method, which returns true/false value, stating whether the convert was succesful also giving out required value if it was successful. If it fails you get default value - 0.

If all textboxes follow same procedure you may create only one TextChanged event and bind it to all of them.

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int sum = 0;
        if (!string.IsNullOrEmpty(textBox1.Text) && int.TryParse(textBox1.Text, out int gold_ticket_count))
        {
          sum  = 120 * gold_ticket_count;
        }
        if (!string.IsNullOrEmpty(textBox2.Text) && int.TryParse(textBox2.Text, out int silver_ticket_count))
        {
          sum  = 60 * silver_ticket_count;
        }
        if (!string.IsNullOrEmpty(textBox3.Text) && int.TryParse(textBox3.Text, out int big_show_ticket_count)) 
        {
          sum  = 500 * big_show_ticket_count;
        }
        // do smth with the sum...
    }

Check if I named textBoxes correctly. It is a good practise to give your controls a meaningful name.

int.Parse doc: https://learn.microsoft.com/en-us/dotnet/api/system.int32.parse?view=net-6.0 int.TryParse doc: https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-6.0

CodePudding user response:

an ugly, quick and dirty but working solution:

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private int sum1;
    private int sum2;
    private int sum3;
    private int overallSum;

    private void tb1_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (int.TryParse(tb1.Text, out int tb1Value))
            sum1 = tb1Value *   120;
        else
            sum1 = 0;

        sum_1.Text = sum1.ToString();
        RecalcOverallSum();
    }

    private void tb2_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (int.TryParse(tb2.Text, out int tb1Value))
            sum2 = tb1Value * 120;
        else
            sum2 = 0;
     
        sum_2.Text = sum2.ToString();
        RecalcOverallSum();
    }

    private void tb3_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (int.TryParse(tb3.Text, out int tb1Value))
            sum3 = tb1Value * 120;
        else
            sum3 = 0;
     
        sum_3.Text = sum3.ToString();
        RecalcOverallSum();
    }

    private void RecalcOverallSum()
    {
        overallSum = sum1   sum2   sum3;
        overall_sum.Text = overallSum.ToString();   
    }
}

you can check the the entire code here: github repo

BTW: I would recommend using MVVM instead of code behind, but I know it wasn't the question

  • Related