Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Simple Counting

Looking for help with simple counting. Heres the program im working on the very first part of the add button but getting error on line 2. Making the score total textbox add score textbox

System.FormatException: 'Input string was not in a correct format.'

[Image: YyLUeL7.png]

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

           TotalScore += Score;
         
       }
[Image: cIiTrNH.png]
I can try to help you debug more but I would need to see the rest of the .cs class and the .xaml class for the project. Specifically how the txtScore look and the txtScoreTotal look like. Are you making sure they're binding correctly. The second line of code compiles fine as the following.

Code
   decimal score = Convert.ToDecimal("98");
   decimal TotalScore = Convert.ToDecimal("472");

   TotalScore += score;
(Aug 20, 2019 08:37 PM)CynicalCheshire Wrote:  I can try to help you debug more but I would need to see the rest of the .cs class and the .xaml class for the project. Specifically how the txtScore look and the txtScoreTotal look like. Are you making sure they're binding correctly. The second line of code compiles fine as the following.

Code
   decimal score = Convert.ToDecimal("98");
   decimal TotalScore = Convert.ToDecimal("472");

   TotalScore += score;

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Extra_4_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true;
        }
        
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            decimal Score = Convert.ToDecimal(txtScore.Text);
            decimal TotalScore = Convert.ToDecimal(txtScoreTotal.Text);
            decimal ScoreCount = Convert.ToDecimal(txtScoreCount.Text);

            TotalScore += Score
            ScoreCount++;
        }

        private void BtnClearScores_Click(object sender, EventArgs e)
        {
            txtScore.Text = "";
            txtScoreTotal.Text = "";
            txtScoreCount.Text = "";
            txtAverage.Text = "";
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Application.Exit();
        }
    }
}

that's all I have right now. I need it to point to the textboxes so I can use the textboxes. Its just not working right though. I keep getting that error.
[Image: cIiTrNH.png]
wdyw to convert to decimal ?
(Aug 21, 2019 07:54 AM)Blanka Wrote:  wdyw to convert to decimal ?

the text inside the textboxes. Is there a better way of doing it?
[Image: cIiTrNH.png]
(Aug 21, 2019 08:22 AM)unknown321 Wrote:  the text inside the textboxes
yes but you can convert it to Int32 why decimal ? ( decimal are 12.34 or 456,789 )
(Aug 21, 2019 08:23 AM)Blanka Wrote:  yes but you can convert it to Int32 why decimal ? ( decimal are 12.34 or 456,789 )

I tried int32 right now and still getting System.FormatException: 'Input string was not in a correct format.'

im just entering random numbers but its not working
[Image: cIiTrNH.png]
(Aug 21, 2019 08:28 AM)unknown321 Wrote:  I tried int32 right now and still getting System.FormatException: 'Input string was not in a correct format.'

im just entering random numbers but its not working

int x = Int32.Parse(txtScore.Text);

this should help this will take the text value, and store it as an int which you need as x, then so on.
[Image: Inqoz4o.gif]

3D Artist and DJ Check out my portfolio
(Aug 21, 2019 09:18 AM)BeatMatch Wrote:  int x = Int32.Parse(txtScore.Text);

this should help this will take the text value, and store it as  an int which you need as x, then so on.

even so while trying this method still getting
System.FormatException: 'Input string was not in a correct format.'
on line
int TotalScore = Int32.Parse(txtScoreTotal.Text);
[Image: cIiTrNH.png]
(Aug 21, 2019 01:03 PM)unknown321 Wrote:  even so while trying this method still getting
System.FormatException: 'Input string was not in a correct format.'
on line
int TotalScore = Int32.Parse(txtScoreTotal.Text);

you are trying to capture total score as an int then parse it into an int, capture it as a string, or you could use a bool aswell would work if you know how to use that.
you would also neeed to parse all of the above in the code or its going to through out an error.

Code
void RunMainMenu(string input)
   {
       bool isValidNumber = (input == "1" || input == "2" || input == "3");
       if (isValidNumber)
       {
           level = int.Parse(input);
           AskForPassword();
        }

i decleared int level at the top of my file your's will be slightly diffrent because you are using .text but thats the best example i have on hand.
[Image: Inqoz4o.gif]

3D Artist and DJ Check out my portfolio
To be honest I didn't understand what you wanted to code. I mean the functionality.
(Aug 21, 2019 01:06 PM)BeatMatch Wrote:  you are trying to capture total score as an int then parse it into an int, capture it as a string, or you could use a bool aswell would work if you know how to use that.
you would also neeed to parse all of the above in the code or its going to through out an error.

Code
void RunMainMenu(string input)
   {
       bool isValidNumber = (input == "1" || input == "2" || input == "3");
       if (isValidNumber)
       {
           level = int.Parse(input);
           AskForPassword();
        }

i decleared int level at the top of my file your's will be slightly diffrent because you are using .text  but thats the best example i have on hand.

What is this? Are you gonna write an if statement from 0 to 9 to check if the text is a valid number?

Code
int.TryParse
[Image: vp6iypF.gif]
(Aug 22, 2019 07:57 AM)MavenGradle Wrote:  What is this? Are you gonna write an if statement from 0 to 9 to check if the text is a valid number?

Code
int.TryParse

no, why would i do that i was showing him an example of a bool, clearly he is new to coding, hence im not actually just using c# im also running it through a game engine and in only, wanted to use them 3 hence why i use input and not textbox.text because i have declared it somewhere else,

i already told him to try to int pars, but dont you worry about my code bud it runs smooth and does a job what i want it to do obviously there is more missing and thats just a snippet
[Image: Inqoz4o.gif]

3D Artist and DJ Check out my portfolio
(Aug 22, 2019 08:29 AM)BeatMatch Wrote:  no, why would i do that i was showing him an example of a bool, clearly he is new to coding, hence im not actually just using c# im also running it through a game engine and in only, wanted to use them 3 hence why i use input and not textbox.text because i have declared it somewhere else,

i already told him to try to int pars, but dont you worry about my code bud it runs smooth and does a job what i want it to do obviously there is more missing and thats just a snippet
You clearly seem to be lost, look up some basics buddy

@OP, if you can, always use int.tryParse, it handles the conversion process (or failure, if there is. No exceptions will be raised, just a simple boolean value)

“I wish I could say everything in one word. I hate all the things that can happen between the beginning of a sentence and the end.” ~ Leonard Cohen
(Aug 22, 2019 11:00 AM)vaiperboy Wrote:  You clearly seem to be lost, look up some basics buddy

@OP, if you can, always use int.tryParse, it handles the conversion process (or failure, if there is. No exceptions will be raised, just a simple boolean value)

I seem to be lost considering, my code works umm no sir you are mistaken
[Image: Inqoz4o.gif]

3D Artist and DJ Check out my portfolio


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by systxApr 20, 2017 11:20 PM 18 619 Apr 20, 2017 11:20 PM
Last Post: Goodies
[Shell] by lneidorfMar 4, 2017 05:58 PM 1 125 Mar 4, 2017 05:58 PM
Last Post: b00st3d
[Python] by billybluiceezOct 28, 2016 04:58 PM 6 208 Oct 28, 2016 04:58 PM
Last Post: billybluiceez