TextBox accepts only int - c#

I have a problem with my textbox. I wanted that one can manually set the interval of the x- and y-axis for a chart in the GUI over two textboxes. That works but when I type a char in or when I typed an int in and delete it, the program crashes immediately and I get a System.FormatException (without clicking the button to accept the changes). How can I solve it that one can just type in different signs without immediately crashing the program? My code below:
public void textBox2_TextChanged(object sender, EventArgs e)
{
x_axis_num = Convert.ToInt32(xAxisBox.Text, usC);
}
private void yAxisBox_TextChanged(object sender, EventArgs e)
{
y_axis_num = Convert.ToInt32(yAxisBox.Text);
}
That gets passed to another event:
chart1.ChartAreas[0].AxisX.Interval = x_axis_num;
chart1.ChartAreas[0].AxisY.Interval = y_axis_num;

In the line x_axis_num = Convert.ToInt32(xAxisBox.Text, usC);, you are taking whatever is in the text box and try to convert it to an integer value.
What do you think the conversion of "Hey, I'm not a number!" will do? It will crash horribly, basically because that text is not, and never will be, a number.
Instead, you can use the Int.TryParse method which will take any text and TRY to convert it to a number.
If the conversion is successful, then no problem. If it was not successful, then you get a false value on a flag indicating the text could not be converted.
Example:
int number;
bool result = Int32.TryParse(YourTextBox.Text, out number);
If the conversion is successful, then number has the value, otherwise, result is false so do something like this then:
if(result)
{
xAxisBox.Text = number.ToString();
x_axis_num = number;
}
else
{
xAxisBox.Text = string.Empty;
// Be careful here with what you set.
// This is the value you will set when the Text box has a non numeric value!
x_axis_num = 0;
}

Related

Convert string from TextBox to Number in WPF

I have an application with four text boxes. I want user to be able to enter a value in the first three text boxes. The fourth will give the result (box1 + box2 + box3) / 3.
My textboxes look like this.
private void rBox_1_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
CheckIsNumber(e);
}
And a function to let user enter only digits.
private void CheckIsNumber (TextCompositionEventArgs e)
{
int result;
if (!int.TryParse(e.Text, out result))
{
e.Handled = true;
}
}
The problem is I don't know how to convert and store a value from each textbox in an int.
I've created a null int value to store. In public MainWindow().
int box1_value;
And for function I did this.
public static int GetNumber (string arg)
{
int number = Int32.Parse(arg);
return number;
}
When I'm trying to apply this function this way, nothing happens. Program won't start, no errors, though.
public MainWindow()
{
InitializeComponent();
int box1_value = GetNumber(rBox_1.Text);
}
Remove the
int box1_value = GetNumber(rBox_1.Text);
from your mainwindow constructor.
You're parsing a string.Empty value to int in which the compiler will automatically halt during compilation. Hence your awkward error.
Create a new function called Calculate for calculating your values
private void Calculate()
{
if(string.IsNullOrEmpty(rBox_1.Text) ||
string.IsNullOrEmpty(rBox_2.Text) ||
string.IsNullOrEmpty(rBox_3.Text))
return;
var box1 = GetNumber(rBox_1.Text);
var box2 = GetNumber(rBox_2.Text);
var box3 = GetNumber(rBox_3.Text);
rBox_4.Text = ((box1 + box2 + box3)/3).ToString();
}
You have 2 options for adding the Calculate function:
add it in each rBox_1_PreviewTextInput function as follows:
if(e.Handled)
{
Calculate();
}
or create a button (named button1)
private void button1_Click(object sender, EventArgs e)
{
Calculate();
}
Some Comments: why the string.IsNUllOrEmpty in Calculate?
-> Your GetNumber function does not catch any false string values like Empty. Or you work with tryparse in the GetNumber function or you catch the empty values before you go into the function.
Try using TryParse Method :
int number = Int32.Parse(arg); /*Instead of this line in your code use below.*/
A possible duplicate thread
I would Suggest that you should Add validation to your 1st 3 Test-boxes using PreviewTextInput in that way, you do not have to try parse int in your c# code and will reduce the chance of errors in your code

textbox to accept integers

I've found different help through this website but still can't seem to convert a string to int. I've tried many different ways. Here are two of them. On button_click I need to read the textboxes and convert them to int so I can perform standard logic on them. (a > b functions). Below the first section is what I'm using to force the use of numbers during entry into the text boxes.
private void write_button_Click(object sender, EventArgs e)
{
int mat_od1 = int.Parse(matod_box.Text); //Input string in wrong format.
int mat_id1 = int.Parse(matid_box.Text);
int fod1 = int.Parse(fod_box.Text);
int fid1 = int.Parse(fid_box.Text);
int hp1 = int.Parse(hp_box.Text);
//This next section is just to show something else I've tried.
decimal mat_od = Convert.ToDecimal(matod_box.Text); //Same error.
decimal mat_id = Convert.ToDecimal(matid_box.Text);
decimal fod = Convert.ToDecimal(fod_box.Text);
decimal fid = Convert.ToDecimal(fid_box.Text);
decimal hp = Convert.ToDecimal(hp_box.Text);
decimal pass_od = mat_od;
}
private void fod_box_TextChanged(object sender, EventArgs e)
{
try
{
int numinput = int.Parse(fod_box.Text);
if (numinput < 1 || numinput > 500)
{
MessageBox.Show("You must enter a number between 0 and 500.");
}
}
catch (FormatException)
{
MessageBox.Show("You need to enter a number.");
fod_box.Clear();
}
Any help would be appreciated.
instead of int.Parse() you should use int.TryParse(string,out int)
this way you would be able to chek the output and decide wether the string was correctly parsed or not
int i;string s="";
if(int.TryParse(s,out i))
{
//use i
}
else
{
//show error
}
The int.parse conversion should work, as in this sample:
string s = "111";
int i;
if (int.TryParse(s, out i))
{
Console.Write(i);
}
else
{
Console.Write("conversion failed");
}
Are you sure you actually provide legal input for your ints? In any case, you should use TryParse like I did in my sample. Theres no need to use try..catch where you can use boolean methods provided by the framework, which will get you the same result..
All depends on what you are allowing to be put in the text box.
If it might not be a string that can be converted to an integer, including blank, then something like
int value;
if (int.TryParse(SomeString, out value)
{
// it is an int
}
else
{
// it's not an int, so do nothing raise a message or some such.
}
In addition to using Int32.TryParse in the button Click event handler as others have pointed out, you need to be careful what you're doing in the TextBox Changed event handler. You're code here is flawed:
private void fod_box_TextChanged(object sender, EventArgs e)
{
try
{
int numinput = int.Parse(fod_box.Text);
...
}
catch (FormatException)
{
MessageBox.Show("You need to enter a number.");
fod_box.Clear();
}
Calling foo_box.Clear() will clear any text from the textbox, calling the TextChanged handler to be executed again (unless the TextBox was already empty). So if you enter a non-numeric value, your message box will be displayed twice - the first time when it attempts to parse your non-numeric value, the second time when it attempts to parse an empty string as a result of the call to Clear().
In general, I'd avoid doing validation in the Changed event handler.

Addition using a single TextBox

I am writing a c# windowsform application for addition of two entries in a single text box.
I have 1 text box, 1 add button and 1 result button.
I need to achieve following algo,
User enter the no in text box. It will be stored in int n1.
User will press add button. At this time the content of the textbox.text will be get cleared and user must be able to enter the secound value in it.
This secound value will get stored in int n2.
Now when user click on result button he should get int result=n1+n2
I have written following in add buttons's click event,
n1=convert.Int32(textbox1.text);
textbox1.text="";
n2=convert.Int32(textbox1.text);//exception occures here
And result button's click event has,
textbox1.text=result.tostring();
I leanred the working of this program and find out that it is due to NULL value assignment to int n2.
I am bit confused how can I achieve above problem using single textbox?
Is there any way to provide the textbox.txt value through user through textbox field only?
Is there any property of textbox which I need to set?
I am very new to .net! Please help!
Thanks in advance for your kind attention!
move n2 = Convert.Int32(textbox1.Text); to your result button's click event
Add button click event:
n1=convert.Int32(textbox1.text);
textbox1.text="";
result button's click event:
n2=convert.Int32(textbox1.text);
result=n1+n2;
textbox1.text=result.tostring();
try something like this:
private int result=0;
addMethod()
{
//here you can use also int.TryParse
if(string.isnullorempty(textbox1.text.trim()))
textbox1.text="0";
result+=convert.toint32(textbox1.text);
textbox1.text=string.empty;
}
resultMethod()
{
textbox1.text=result.tostring();
}
look at the folloing code
private int i = 0;
private int[] a = new int[2];
private void button1_Click(object sender, EventArgs e)
{
int b;
if(Int32.TryParse(textBox1.Text, out b))
{
a[i] = b;
i++;
textBox1.Text = "";
}
else
{
MessageBox.Show(#"Incorrect number");
}
}
private void resultbutton2_Click(object sender, EventArgs e)
{
int sum = a[0] + a[1];
MessageBox.Show("Sum: " + sum);
}
}
You code contains a couple of errors:
You are using Int32 Convert.ToInt32(String) which is bad, because will throw an exception if the user enters something that can't be cast to an Int32, you should use Boolean Int32.TryParse(String, out Int32) instead, and check if the conversion is successful.
You are calling n2 = Convert.ToInt32(textbox1.Text); right after you set textbox1.Text = ""; and this is also bad, because it won't wait for a user input, it will try to convert an empty string to an integer right away, which will yield an exception.
You can't know when the user has finished inserting his number, so you have to rely to something external to the TextBox to let you know you should perform the addition.
Try the following:
Place a TextBox.
Place a Button.
Initialize a "grandTotal" integer variable to zero.
When the user press the button, parse the content of the TextBox in a temporary "submittedValue" integer variable, then sum this temporary variable to your "grandTotal" variable, and display the resulting "grandTotal" variable value.
This way you'll be able to continuously perform the addition operation on the previous total.
Example
The global variable:
protected Int32 GrandTotal = 0;
The Button click event handler:
Int32 currentValue = 0;
if (Int32.TryParse(userValue.Text, out currentValue))
{
GrandTotal += currentValue;
MessageBox.Show(String.Format("The new total is: {0}", GrandTotal));
}
else
{
MessageBox.Show("Invalid value!");
}
I have no idea why you are doing it in this fashion.
Still you can do this as
Declare n1, n2 as int? at the form level
Then in your Add click event do it in this fashion
if (! n1.HasValue)
{
n1 = int.Parse(textBox1.Text);
}
n2 = int.Parse(textBox1.Text);
textBox1.Text = "";
if you are going to do this for lotz of number then use List<int?> myIntList = new List<int?>() and your add event would be
myIntList.Add(int.Parse(textBox1.Text));
textBox1.Text = "";
and the result is int? result = myIntList.Sum();

get and Set not working

I am writing the following get and set for validating an input from a Text Box. Basically it is supposed to check if the user has entered all of the values.
When I leave the TextBoxes empty , it does nothing and shows a '0' in output where that variable was being used. It does however show the system generated exception and stops the execution, but I wonder why doesn't it validate the input through the properties?
Here is my code:
public double RecoDoseSize
{
get
{
return recoDoseSize;
}
set
{
if (!(value>0))
{
MessageBox.Show("Please Enter the recommended dose size for this product");
textBox8.Focus();
}
recoDoseSize = value;
}
}
private void Submit2_Click(object sender, RoutedEventArgs e)
{
TotalContentProduct = double.Parse(textBox7.Text);
recoDoseSize = double.Parse(textBox8.Text);
NoOfDosespUnit = TotalContentProduct/recoDoseSize;
}
You are setting recoDoseSize, the backing field, not RecoDoseSize, the property which has your code in it. Thus, your code isn't executed. You need to change the second line of your method body to
RecoDoseSize = double.Parse(textBox8.Text);
(note the capital R).
Other have given the correct answer to the question as stated. Namely that you should call the uppercased RecoDoseSize if you want to use the getter/setter.
However it is extremely bad practice to show a message box inside the setter, because it violates the Principle of Least Surprise.
When someone looks at the line RecoDoseSize = double.Parse(textBox8.Text); it is not at all obvious that this operation could cause a message box to appear.
There are occasionally exceptions where it does make sense to have a setter trigger UI changes (for instance the Visible property on controls) however the default should always be to not do this unless you are sure it will be more confusing to not do so (for instance it would be surprising if you set Visible = false however it was still visible).
Regarding your comment on how you should implement it, the checking should be done in the click handler and the property can just be an auto-property, like so:
public double RecoDoseSize { get; set; }
private void Submit2_Click(object sender, RoutedEventArgs e)
{
TotalContentProduct = double.Parse(textBox7.Text);
double enteredSize;
if (!double.TryParse(textBox8.Text, out enteredSize) || enteredSize <= 0)
{
MessageBox.Show("Please Enter the recommended dose size for this product");
textBox8.Focus();
return;
}
RecoDoseSize = enteredSize;
NoOfDosespUnit = TotalContentProduct / recoDoseSize;
}
You'll want to use TryParse because with Parse you'll get an error if the text isn't a valid double. What TryParse does is return true or false depending on whether it succeeded, and it populates the out parameter with the result if it's successful.
So what this does is if it either failed to parse the result, or the result is <= 0 it shows the message box. In that case it also returns from the method so the rest of it isn't executed. Alternatively the rest of the method could be in an else block in which case the return isn't needed. It's a matter a style which way is preferred.
You're never actually using the getter/setter. You are using the actual field name: recoDoseSize directly. Change it to RecoDoseSize.
private void Submit2_Click(object sender, RoutedEventArgs e)
{
TotalContentProduct = double.Parse(textBox7.Text);
RecoDoseSize= double.Parse(textBox8.Text);
NoOfDosespUnit = TotalContentProduct/recoDoseSize;
}
You shouldn't be handling focus in your set statement.
Also, you need to make sure that value is not null, otherwise you can't compare it to anything (greater-than, etc.).

How do I determine if the value of a string variable changed in C#?

I have something to do under a button click (add values to listbox) only if a particular string changes from its previous value. How do I manage this? Below is a sample of my code:
private void button6_Click(object sender, EventArgs e)
{
string x = //some varying value I get from other parts of my program
listBox1.Items.Clear();
listBox1.Items.Add(x + /*other things*/);
}
I can at times have same value for string x from previous value when clicking button6. In such cases I don't want listBox1 to add the item (string x). How to add to listbox only when value of string changes? There's no way to predetermine string x. It gets value when program is running.
Note: adding values to listBox1 every single time and later deleting the duplicates wont work in my program.
Have you considered keeping a copy of the old string value around in a private field, and simply comparing the new value to the old value to see if they match?
For example:
// holds a copy of the previous value for comparison purposes
private string oldString = string.Empty;
private void button6_Click(object sender, EventArgs e)
{
// Get the new string value
string newString = //some varying value I get from other parts of my program
// Compare the old string to the new one
if (oldString != newString)
{
// The string values are different, so update the ListBox
listBox1.Items.Clear();
listBox1.Items.Add(x + /*other things*/);
}
// Save the new value back into the temporary variable
oldString = newString;
}
Edit: As the other answers suggest, there are certainly other, more complicated solutions, like encapsulating all access to the string value in a property, or wrapping the string in a custom class. Some of these alternatives have the potential to be "cleaner", more object-oriented approaches. But they're all more complicated than simply saving the previous value in a field. It's up to you to decide whether your specific use case merits the complicated solution, or a simpler one. Think about long-term maintainability, not what's easier for you to implement right now.
string last = string.Empty;
private void button6_Click(object sender, EventArgs e)
{
string x = //some varying value I get from other parts of my program
if(x!=last)
{
listBox1.Items.Clear();
listBox1.Items.Add(x + /*other things*/);
last = x;
}
}
If this string is super important and gets passed around alot, maybe you should wrap it in a class. The class can hold the string value as a property, but also keep track of when it has changed.
public class StringValue
{
private bool _changed;
public string StrValue{get; set{ _changed = true;}
public bool Changed{get;set;}
}
this is rudimentery of course
I'm not sure I understand completely, but it sounds like you should be using a property to set String x;
string _x = string.Empty;
public string X
{
set
{
if(value != this._x)
{
DoFancyListBoxWork();
this._x = value;
}
}
get
{
return this._x;
}
}
If this is web application, store your last value into session variable. If this is windows application, store it at a class level variable or in singleton class and use this last value for comparison with new value.
On the page load add the current value to viewstate and at the button click check the current value is equal to the value in the view state. If both are equal we can say that the value is not changed.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["CurrentValue"] = Your Value;
}
}
protected void btnSubmit_click(object sender, EventArgs e)
{
if (NewValue== ViewState["CurrentValue"].ToString())
{
lblmsg.Text = "value is not changed..";
return;
}
else
lblmsg.Text = "value is changed..";
}
You can check the detailed article in this link.
Check Control Value is changed or not
First, I'd like to ask you to check most of the other answers. They are more complete, in that they treat more global issues of tracking the changes of a variable.
Now, I'm assuming, from reading the snippet of code you provided, that you need to track if a string was changed by the user. So, in other words, you probably have a TextBox or other kind of control through which the user can change that value. This is where you should focus your attention: just consume the TextChanged event.
If, however, I'm mistaken and your string comes from any other kind of external source, either use the wrapper class suggested by #Ryan Bennett or, if you are using .Net 4, use a dynamic container, which raises a PropertyChanged event whenever any property is changed.

Categories