[C#] What am I doing wrong?

bleh....
Loyal Member
Joined
Oct 15, 2008
Messages
2,896
Reaction score
1,129
Solved previous problem, now I have another, so instead of making a new thread I'll post it here.


PHP:
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?
 
Last edited:
Custom Title Activated
Loyal Member
Joined
Apr 8, 2008
Messages
1,125
Reaction score
330
Back to basics, conditionals and bools.
PHP:
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:
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.
 
Last edited:
Legendary Battlemage
Loyal Member
Joined
Apr 7, 2009
Messages
647
Reaction score
25
Essentially, in your text changed event, you want to do something like:
PHP:
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:
 
Banned
Banned
Joined
Oct 20, 2006
Messages
3,245
Reaction score
1,652
Why not just use a NumericUpDown with a Maximum of 199 and Minimum of 1?