Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i am having three textboxes one inputting hours ,one numbers and one power rate and a button
but am having a formatException whenever i try to input values in the textboxes.
How can i fore go this please?
whenever i compile it runs well but i am unable to input the values
help please
Thanks!
C#
int num1 = int.Parse(TextBox1.Text);
             
             double num2 = double.Parse(TextBox2.Text;);
            
             int num3 = int.Parse(TextBox3.Text;);
             soln1 = num1 * num2 * num3;
             MessageBox.Show(soln1.ToString());
Posted
Comments
Philippe Mori 20-Aug-15 21:29pm    
Missing important information like the text you have in the text box when the exception occurs ...

Instead of just converting the values it would be better to use TryParse to test if the value can be converted and if it can, it'll be converted. Have a look at Int.TryParse[^] and double.TryParse[^]

Something like
C#
double num2;
if (!double.Parse(TextBox2.Text, out num2)) {
   System.Diagnostics.Debug.WriteLine(string.Format("{0} is not a double.", TextBox2.Text));
   return;
}


Note that you originally had an extra semicolon in
C#
double num2 = double.TryParse(TextBox2.Text;);
 
Share this answer
 
v4
Comments
Shaddy Wiz Kay 28-Jul-15 4:04am    
am a young learner in c#
would you help me please write for me a sample code please
Thanks!!
Wendelius 28-Jul-15 4:13am    
Have a look at the modified answer. Also there are good examples in the documentation if you have a look at the links I provided.
Shaddy Wiz Kay 28-Jul-15 5:07am    
ooh i have seen
it was just a typing error
Shaddy Wiz Kay 28-Jul-15 5:24am    
i am getting errors from this part double num2;
if (!double.Parse(TextBox2.Text, out num2)) {
System.Diagnostics.Debug.WriteLine(string.Format("{0} is not a double.", TextBox2.Text));
return;
}
saying"invalid urguments"
Wendelius 29-Jul-15 2:21am    
Sorry, my bad, instead of Parse use TryParse.
You have two options: use, as suggested, the TryParse methods, or handle the thrown exceptions. In either ways you should explicitely deal with possible invalid user inputs. For instance, you may show a message box explaining what are the format requirements for the given field.
 
Share this answer
 
C#
int value;
if (Int32.TryParse("50", out value))
    Console.WriteLine(value);
else
    Console.WriteLine("Failed to parse the string value to integer.");
 
Share this answer
 
Comments
Shaddy Wiz Kay 28-Jul-15 5:11am    
thanks!!
In addition to other answer like using TryParse, you also have to ensure that proper local. For example, if system decimal separator don't match separayor used in text box, you might get an exception (or wrong number if decimal separator is interpreted as thousand separator).

By the way, you have to tell us the text you used and if any locale is configured or you used the system one in which case, you have to know the language/region set in control panel.
 
Share this answer
 
 
Share this answer
 
Based on: .NET 4.5

C# program that uses int.Parse

using System;

class Program
{
static void Main()
{
// Convert string to number.
string text = "500";
int num = int.Parse(text);
Console.WriteLine(num);
}
}
 
Share this answer
 
Comments
Richard MacCutchan 29-Jul-15 6:52am    
And when the string contains garbage?
int num = 5;
string str = num.ToString();
 
Share this answer
 
Comments
Richard MacCutchan 29-Jul-15 6:51am    
Read the question.
[no name] 29-Jul-15 7:07am    
Read the already answered question.
Philippe Mori 20-Aug-15 21:21pm    
If you want to have some points by adding extra information, explicitly tell that it is an addition to an existing answer (and which one). Otherwise, you only points for what you have written.
//It will work

try
{
int Hour=Convert.ToInt32(txtHour.Text.ToString());
int Min=Convert.ToInt32(txtMin.Text.ToString());
int Sec=Convert.ToInt32(txtSec.Text.ToString());
}
catch(Exception e)
{
Label1.Text="InValid Number";
}
 
Share this answer
 
v4
Comments
[no name] 31-Jul-15 8:49am    
What happens with your code when the user starts typing in anything but numbers in the tetboxes? No, this will not work.
JIGAR DAK 31-Jul-15 9:00am    
if user try to type anything then in this code simply put in try{...}catch(){...} block and display "only integer number is allow"
[no name] 31-Jul-15 9:04am    
Why not just do it correctly to begin with?
Richard MacCutchan 4-Aug-15 3:27am    
ROFL!
Although I imagine your beginner since you ask that kind of question, and for beginners it might be irrelevant, but I would go with what JIGAR DAK said (Solution 9) because Convert.ToInt32() is way faster than int.Parse() or int.TryParse()
 
Share this answer
 
Comments
Philippe Mori 20-Aug-15 21:24pm    
Any proof for that... And even if it would be true, it would be irrelevant for converting a number from a text box.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900