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.'
Code private void BtnAdd_Click(object sender, EventArgs e)
{
decimal Score = Convert.ToDecimal(txtScore.Text);
decimal TotalScore = Convert.ToDecimal(txtScoreTotal.Text);
TotalScore += Score;
}
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 21, 2019 07:23 AM
(This post was last modified: Aug 21, 2019 07:24 AM by unknown321.)
(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.
wdyw to convert to decimal ?
Aug 21, 2019 08:22 AM
(This post was last modified: Aug 21, 2019 08:23 AM by unknown321.)
(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?
(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
(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.
(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);
Aug 21, 2019 01:06 PM
(This post was last modified: Aug 21, 2019 01:18 PM by BeatMatch.)
(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.
DO NOT DEAL WITH THIS MEMBER
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?
(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?
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
(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
|