7 Replies - 2411 Views - Last Post: 05 October 2010 - 02:12 PM Rate Topic: -----

#1 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

How do I save a textbox using a button?

Posted 04 October 2010 - 08:48 PM

I am trying to make a Windows Forms Application with three textboxes that add to a fourth textbox using a button. How do I take the numbers from the first three textboxes and then keep a running tally in three respective boxes when using another button. I will attach the coding that I have already done. I have problems with the running variables below. I can't figure out how to save the numbers and have them add together.

///// This part is where the first three textboxes add to make the forth ///////
const int ticketPriceA = 150;
            const int ticketPriceB = 125;
            const int ticketPriceC = 95;

            int TicketA = 0;
            int TicketB = 0;
            int TicketC = 0;
            decimal totalCost;

            TicketA = int.Parse(txtTicketA.Text);
            TicketB = int.Parse(txtTicketB.Text);
            TicketC = int.Parse(txtTicketC.Text);

            decimal totalCostA = ticketPriceA * TicketA;
            decimal totalCostB = ticketPriceB * TicketB;
            decimal totalCostC = ticketPriceC * TicketC;

            totalCost = (totalCostA + totalCostB + totalCostC);

            lblTOTALCOST.Text = string.Format("{0:c}", totalCost);

///////// This is where I am trying to save the variables from above into two sets of three textboxes below ///////

 const int ticketPriceA = 150;
            const int ticketPriceB = 125;
            const int ticketPriceC = 95;

            decimal VALUEA;
            decimal VALUEB;
            decimal VALUEC;

            int TicketA = 0;
            int TicketB = 0;
            int TicketC = 0;

            decimal SOLDA;          //////// I don't know if I am defining these right /////////
            decimal SOLDB;
            decimal SOLDC;

            decimal SOLDA = (TicketA);      ////// My biggest problem is what the math formulas should be   //////
            decimal SOLDB = (TicketB);      ////// or how to send the numbers from above to the bottom      //////
            decimal SOLDC = (TicketC);      ////// I know the errors here are because it is the same formula //////


            decimal VALUEA = (ticketPriceA * TicketA);
            decimal VALUEB = (ticketPriceB * TicketB);
            decimal VALUEC = (ticketPriceC * TicketB);

            lblSOLDA.Text = string.Format("{0:c}", TicketA);
            lblSOLDB.Text = string.Format("{0:c}", TicketB);
            lblSOLDC.Text = string.Format("{0:c}", TicketC);
            lblVALUEA.Text = string.Format("{0:c}", VALUEA);
            lblVALUEB.Text = string.Format("{0:c}", VALUEB);
            lblVALUEC.Text = string.Format("{0:c}", VALUEC);


MOD EDIT: USE CODE TAGS!!!!

:code:

This post has been edited by JackOfAllTrades: 05 October 2010 - 04:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I save a textbox using a button?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5526
  • View blog
  • Posts: 11,835
  • Joined: 02-June 10

Re: How do I save a textbox using a button?

Posted 04 October 2010 - 08:58 PM

Please use code tags as described in your welcome to DIC email
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5526
  • View blog
  • Posts: 11,835
  • Joined: 02-June 10

Re: How do I save a textbox using a button?

Posted 04 October 2010 - 09:04 PM

1) Change your textboxes to NumericUpDown or MaskedTextBox. That way your users can't enter "bob" where they should have a number.
2) This is a fraking mess. Please copy from your visual studio project and paste into the post. This does not look like code you are actually trying to run from VS. It looks like various samples spliced from Google projects. There are multiple lines duplicated later on... there is no method for when you click a button. Its a bunch of disjointed keyboard scribblings.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How do I save a textbox using a button?

Posted 05 October 2010 - 04:49 AM

why are you doing it all over again? Remove all of the code after you get the TotalCost and display it. From there, simply create 3 more variables to store the running totals...

// these need to be declared outside of the Click event.

decimal runningTotalA = 0;
decimal runningTotalB = 0;
decimal runningTotalC = 0;

private void btnSave_Click(object sender, EventArgs e)
{
     const int ticketPriceA = 150;
     const int ticketPriceB = 125;
     const int ticketPriceC = 95;

     int TicketA = 0;
     int TicketB = 0;
     int TicketC = 0;
     decimal totalCost;

     TicketA = int.Parse(txtTicketA.Text);
     TicketB = int.Parse(txtTicketB.Text);
     TicketC = int.Parse(txtTicketC.Text);

     decimal totalCostA = ticketPriceA * TicketA;
     decimal totalCostB = ticketPriceB * TicketB;
     decimal totalCostC = ticketPriceC * TicketC;

     totalCost = (totalCostA + totalCostB + totalCostC);

     lblTOTALCOST.Text = string.Format("{0:c}", totalCost);

     // the above code is all from your previous question
     // the below code is the new code.
     runningTotalA += totalCostA;
     runningTotalB += totalCostB;
     runningTotalC += totalCostC;

     lblSOLDA.Text = string.Format("{0:D}", TicketA);
     lblSOLDB.Text = string.Format("{0:D}", TicketB);
     lblSOLDC.Text = string.Format("{0:D}", TicketC);
     lblVALUEA.Text = string.Format("{0:D}", runningTotalA);
     lblVALUEB.Text = string.Format("{0:D}", runningTotalB);
     lblVALUEC.Text = string.Format("{0:D}", runningTotalC);
}



Once again, look at the names of my variables. They are not "value1". They are descriptive in their name so when looking at it, you know what data it contains.

Also, please don't triple post. Your other two topics have been deleted.
Was This Post Helpful? 1
  • +
  • -

#5 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

Re: How do I save a textbox using a button?

Posted 05 October 2010 - 09:55 AM

[quote name='eclipsed4utoo' date='05 October 2010 - 03:49 AM' timestamp='1286279384' post='1132704']


I am sorry for the sloppy posts, I am really new at this and don't really know what I am doing if you can't tell. I do appreciate all of your help. I have just one more question. If I wanted to have the number of tickets be a running tally as well, do I create three other variables above, declare another decimal variable, or can I do TicketA += TicketA;.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How do I save a textbox using a button?

Posted 05 October 2010 - 11:07 AM

you would need three more variables, then use the += with those three new variables...

// outside of the click event
decimal runningTotalTicketsA = 0;



// inside the click event
runningTotalTicketsA += TicketA;


Was This Post Helpful? 1
  • +
  • -

#7 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

Re: How do I save a textbox using a button?

Posted 05 October 2010 - 01:57 PM

I tried what you said but now there is a format error when I try to debug. I did it with three variables both above and below.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How do I save a textbox using a button?

Posted 05 October 2010 - 02:12 PM

View Posteric4215, on 05 October 2010 - 04:57 PM, said:

I tried what you said but now there is a format error when I try to debug. I did it with three variables both above and below.


can you post your code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1