Amanda
Amanda

Reputation: 55

Move data from one form textbox to another in c#

So I have an invoice total form that should calculate the percentage discount among other things. I'm adding a second form to the code that allows you to change the sales tax I got it to populate the form and actually work with no errors but I can't get it to move the data from the textbox on frmSalesTax to the txtSalesTax.Text in frmInvoiceTotal.

frmInvoiceTotal Code:

 public frmInvoiceTotal()
    {
        InitializeComponent();
    }
    frmSalesTax percent = new frmSalesTax();
    private void btnCalculate_Click(object sender, EventArgs e)
    {
        decimal productTotal = Convert.ToDecimal(txtProductTotal.Text);
        decimal salesTax = (7.75m/100m) *  productTotal;
        decimal discountPercent = .0m;

        if (productTotal < 100)
            discountPercent = .0m;
        else if (productTotal >= 100 && productTotal < 250)
            discountPercent = .1m;
        else if (productTotal >= 250)
            discountPercent = .25m;

        decimal discountAmount = (productTotal + salesTax) * discountPercent;
        decimal subtotal = productTotal - discountAmount;
        decimal invoiceTotal = (subtotal + salesTax) - discountAmount;


        txtSubtotal.Text = subtotal.ToString("c");
        txtSalesTax.Text = salesTax.ToString("c");
        txtDiscountPercent.Text = discountPercent.ToString("p1");
        txtDiscountAmount.Text = discountAmount.ToString("c");
        txtTotal.Text = invoiceTotal.ToString("c");

        txtProductTotal.Focus();
    }

private void btnChange_Click(object sender, EventArgs e)
    {
        percent.salesTax = txtSalesTax.Text;
        switch (percent.ShowDialog())
        {
            case DialogResult.OK:
                txtSalesTax.Text = percent.salesTax;
                break;
        }

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();

    }

invoiceTotal GUI:enter image description here

frmSalesTax Code:

 public partial class frmSalesTax : Form
{
    public string salesTax
    {
        get;
        set;
    }
    public frmSalesTax()
    {
        InitializeComponent();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();

    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        this.salesTax = txtPercent.Text;
        txtPercent.Text = "";

        Hide();


    } 

I know I'm missing something but I can't figure out what it is.

salesTax GUI: enter image description here

Upvotes: 3

Views: 618

Answers (1)

Clay
Clay

Reputation: 5084

You've got the right idea making a property on frmSalesTax to communicate...but you're not really using it.

In your frmInvoiceTotal, you need to send the current value to frmSalesTax.salesTax and then handle the results of the percent dialog returning DialogResult.OK:

private void btnChange_Click(object sender, EventArgs e)
{
    percent.salesTax = txtSalesTax.Text; //--> send current value to frmSalesTax
    switch ( percent.ShowDialog() ) //--> ShowDialog will return the DialogResult of the pressed button
    {
       case DialogResult.OK:
         txtSalesTax.Text = percent.salesTax;  //--> update with new value from frmSalesTax
         break;
    }                    
}

...and, in your frmSalesTax, you need to put the txtPercent.Text into the salesTax property when the user clicks the OK button:

private void btnOk_Click(object sender, EventArgs e)
{
    this.salesTax = txtPercent.Text;  //--> frmInvoiceTotal will read this after the OK button is clicked
    txtPercent.Text = "";

    Hide();
}

Important: you have to make sure the frmSalesTax buttons have their DialogResult set, so that frmInvoiceTotal.btnOk_Click knows that it's okay to get the value:

DialogResult property

Edit

The property (in frmSalesTax) needs to not be based on the form's text values...because you're setting that to "" when the form hides. This is what you want for the property:

public string salesTax
{ 
  get;
  set;
}

This would go with the other changes I mentioned earlier.

Edit 2

It's easy to get frustrated. There are a lot of moving pieces, and I can understand how the eyes can cross. Here's the crux of the issue - your calculation is trashing things on you;-)

These lines in the btnCalculate_Click:

decimal salesTax = (7.75m/100m) *  productTotal;
decimal discountPercent = .0m;
//...
txtSalesTax.Text = salesTax.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");

...should be the initial values and come in the form's initialization code:

public frmInvoiceTotal()
{
   InitializeComponent();
   decimal salesTax = (7.75m/100m) *  productTotal;
   decimal discountPercent = .0m;
   txtSalesTax.Text = salesTax.ToString("c");  //--> the initial value
   txtDiscountPercent.Text = discountPercent.ToString("p1");
}

...and then, the calculation should not re-populate txtSalesTax.Text or txtDiscountPercent.Text. The txtSalesTax.Text might be update from showing frmSalesTax, and I'm guessing you're gonna make another form to override discount percent at some point.

private void btnCalculate_Click(object sender, EventArgs e)
{
    decimal productTotal = Convert.ToDecimal(txtProductTotal.Text);
    decimal salesTax = Convert.ToDecimal(salesTax.Text) * productTotal;  //--> if it got changed in frmSalesTax
    decimal discountPercent = .0m;

    if (productTotal < 100)
        discountPercent = .0m;
    else if (productTotal >= 100 && productTotal < 250)
        discountPercent = .1m;
    else if (productTotal >= 250)
        discountPercent = .25m;

    decimal discountAmount = (productTotal + salesTax) * discountPercent;
    decimal subtotal = productTotal - discountAmount;
    decimal invoiceTotal = (subtotal + salesTax) - discountAmount;


    txtSubtotal.Text = subtotal.ToString("c");
    //txtSalesTax.Text = salesTax.ToString("c"); //--> don't do this...it steps on what came from frmSalesTax
    //txtDiscountPercent.Text = discountPercent.ToString("p1");  //--> when you add another form to override this
    txtDiscountAmount.Text = discountAmount.ToString("c");
    txtTotal.Text = invoiceTotal.ToString("c");

    txtProductTotal.Focus();
}

I bet this get you a lot closer :-)

Upvotes: 1

Related Questions