Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


c# methods

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
zaczac
Master Cheater
Reputation: 0

Joined: 01 May 2007
Posts: 266
Location: Ontario, Canada

PostPosted: Tue Nov 15, 2011 6:34 pm    Post subject: c# methods Reply with quote

I am just starting to learn methods and need to create one that will check to see if a user entered a valid entry in a text box. The valid entry's are
"+" "-" "/" "*" without quotes.

Basically coding a calculator and one of the text boxes requires the user to enter one of those operators, I need to check to make sure one is entered.

If anyone could give me some ideas or help out I would appreciate it.

Thanks in advanced!
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Wed Nov 16, 2011 3:54 am    Post subject: Reply with quote

Limit the number of characters allowed inside the textbox to only 1 character,
And check whether the entered character is one of those operators.

_________________
Stylo
Back to top
View user's profile Send private message
zaczac
Master Cheater
Reputation: 0

Joined: 01 May 2007
Posts: 266
Location: Ontario, Canada

PostPosted: Wed Nov 16, 2011 9:22 am    Post subject: Reply with quote

I know that much, just not sure what to use to check if they entered one..
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Wed Nov 16, 2011 11:02 am    Post subject: Reply with quote

I guess it'd look something like:
Code:

private void button1_Click( object sender, EventArgs e ) {
   string op = txtOperator.Text;
   switch( op ) {
      case "+":
         // code
         break;
      case "-":
         // code
         break;
      case "*":
         // code
         break;
      case "/":
         // code
         break;
   }
}

_________________
Stylo
Back to top
View user's profile Send private message
zaczac
Master Cheater
Reputation: 0

Joined: 01 May 2007
Posts: 266
Location: Ontario, Canada

PostPosted: Wed Nov 16, 2011 1:05 pm    Post subject: Reply with quote

Thanks stylo, I actually found another way to do this as well, would this be a "correct" way of doing it?

Code:
public bool IsOperator(TextBox textBox, string name)
        {
            bool valid = true;
            if (textBox.Text != "+" && textBox.Text != "-" && textBox.Text != "/" && textBox.Text != "*")
            {
                MessageBox.Show(name + "Please enter a valid operator.");
                textBox.Focus();
                valid = false;
            }

            return valid;
        }
Back to top
View user's profile Send private message
Unbr0ken
Advanced Cheater
Reputation: 2

Joined: 10 Aug 2011
Posts: 67

PostPosted: Thu Nov 17, 2011 10:34 pm    Post subject: Reply with quote

Why don't use the KeyDown Event?
Back to top
View user's profile Send private message Send e-mail MSN Messenger
FLiNG
Newbie cheater
Reputation: 0

Joined: 09 Apr 2011
Posts: 19

PostPosted: Fri Nov 18, 2011 12:52 am    Post subject: Reply with quote

There are many ways of doing this, I think it is easier to use Key Events. The following code uses Keypress Event to avoid any characters other than +-*/ go into the textbox.

Code:
        private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = "+-*/".IndexOf(char.ToUpper(e.KeyChar)) < 0;
        }
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites