3

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

2
  • Where is the problem then, you never set salesTax in frmSalesTax and you never use it in other form Commented Mar 16, 2019 at 5:27
  • I set it as percent Commented Mar 16, 2019 at 5:49

1 Answer 1

1

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 :-)

Sign up to request clarification or add additional context in comments.

7 Comments

I did exactly what you put and it still didn't work. It's still only showing the sales tax that's a default.
Oh! I see the bug...the property...hang on...I'll edit to show
no that still didn't work >.< I'm getting frustrated with this one now for sure. is that edit in frmInvoiceTotal or frmSalesTax?
Put all your current, updated code in the question...let's have a look
I have an error code CS1061 'decimal' does not contain a definition for 'Text'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.