[C#] What am I doing wrong?
Solved previous problem, now I have another, so instead of making a new thread I'll post it here.
PHP Code:
private void userLevel_TextChanged(object sender, EventArgs e) {
}
private void userCurrentExp_TextChanged_1(object sender, EventArgs e) {
}
These are text fields. How do I make it so- userLevel and userCurrentExp can only have whole numbers typed in the fields
- userLevel can only have a allowed range of 1-199 typed in the field
- Both can not have a negative value
I've tried to look on google, but keep getting things that are irrelevant to what I'm trying to do. Anyone here know C# really good and can help?
Re: [C#] What am I doing wrong?
Back to basics, conditionals and bools.
PHP Code:
private boolean isPositive(int number) {
if(number >= 0) {
return true;
}
return false;
}
// how to use:
if(isPositive(-100)) // will return false.
if(isPositive(100)) // will return true.
private boolean isBetween1and199(int number) {
if(number >= 1 && <= 199) {
return true;
}
return false;
}
// see a pattern here?
From there, you just have to implement these functions in to your code. So, if I wanted to see if the number entered was between 1 and 199 in userLevel_TextChanged...
PHP Code:
private void userLevel_TextChanged(object sender, EventArgs e) {
/*
in this line below you'll see (int)textbox1.Text.
This is how you cast a variable to an integer in
java, I don't remember how it works in C#.
I think something like Convert.ToInt32(variable);.
Don't quote me on anything. lol
*/
if(isBetween1and199((int)textbox1.Text)) {
MessageBox.Show("Your number is between 1 and 199. :)");
} else {
MessageBox.Show("Your number isn't between 1 and 199. :(");
}
}
Hope I helped.
Re: [C#] What am I doing wrong?
Essentially, in your text changed event, you want to do something like:
PHP Code:
public void event
{
int temp;
if(Int32.TryParse(text, out temp))
{
//now you check if it's positive or not with mintee's solution.
}
else
//Wasn't integer
}
MSDN Docs:
Int32.TryParse:
Int32.TryParse Method (System)
Re: [C#] What am I doing wrong?
Why not just use a NumericUpDown with a Maximum of 199 and Minimum of 1?