[C#] What am I doing wrong?

Joined
Oct 15, 2008
Messages
2,898
Reaction score
1,131
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:
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:
Back