7 Replies - 365 Views - Last Post: 20 August 2019 - 02:58 PM Rate Topic: -----

#1 J54   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-December 18

Counting

Posted 20 August 2019 - 02:18 PM

Posted Image

Im working on the Add button the first part. When I click add I want it to take the current number in score and add it to the score total textbox

The code im playing with now doesn't seem like the right one can someone point me in the proper direction.

private void BtnAdd_Click(object sender, EventArgs e)
        {
            decimal Score = Convert.ToDecimal(txtScore.Text);
            
     
            txtScoreTotal.Text += Score;
           
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Counting

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,152
  • Joined: 12-June 08

Re: Counting

Posted 20 August 2019 - 02:21 PM

.Text is a string. You would need to convert "txtScoreTotal.Text" to a decimial, do your addition then assign the value to the textbox.
Was This Post Helpful? 0
  • +
  • -

#3 J54   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-December 18

Re: Counting

Posted 20 August 2019 - 02:34 PM

View Postmodi123_1, on 20 August 2019 - 02:21 PM, said:

.Text is a string. You would need to convert "txtScoreTotal.Text" to a decimial, do your addition then assign the value to the textbox.

I tried this

 {
            decimal Score = Convert.ToDecimal(txtScore.Text);
            decimal TotalScore = Convert.ToDecimal(txtScoreTotal.Text);


            TotalScore += Score;
           
        }



but getting input string was not in correct format. My conversion is correct isn't it?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,152
  • Joined: 12-June 08

Re: Counting

Posted 20 August 2019 - 02:36 PM

What do you mean "not in correct format"?
Was This Post Helpful? 0
  • +
  • -

#5 J54   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-December 18

Re: Counting

Posted 20 August 2019 - 02:39 PM

View Postmodi123_1, on 20 August 2019 - 02:36 PM, said:

What do you mean "not in correct format"?

when I click add youd think it would work but its saying System.FormatException: 'Input string was not in a correct format.'

that's what im wondering. Simple code but cant get it working.

I put 10 into the score clicked add and that pops up
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,152
  • Joined: 12-June 08

Re: Counting

Posted 20 August 2019 - 02:42 PM

*shrug* It's hard to guess at the little information given.

If you tried something new then post that entire chunk of code.

If there are some expected values in variables or textboxes post them here.

If the error is happening on a specific line then indicate that.
Was This Post Helpful? 0
  • +
  • -

#7 J54   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-December 18

Re: Counting

Posted 20 August 2019 - 02:52 PM

the error appears to be line 3

private void Btn Add_Click(object sender, EventArgs e)
        {
            decimal Score = Convert.ToDecimal(txtScore.Text);
            decimal TotalScore = Convert.ToDecimal(txtScoreTotal.Text);


            TotalScore += Score;
           
        }


line 4 my mistake
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,152
  • Joined: 12-June 08

Re: Counting

Posted 20 August 2019 - 02:58 PM

I would suggest putting a break point on that line and seeing what the content of 'txtScoreTotal.Text' is.

Think on how a system would know to convert a single space, or an empty string.

If it were me I would think about checking for this edge cases that cannot be converted so well and utilize some IF statements.

https://docs.microso...etframework-4.8
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1