0

I'm trying to create a program that will simulate a dog race. and 3 players are able to bet on any of the dogs, but they can only bet up to $15 each. I have all of this set up except for only allowing up to $15 to be bet. the code is extremely long so I'll try to explain what I have so far

So I have radio buttons to select which player want to place their bet, once a player is selected, 4 radio buttons are enabled which allows the player betting to choose which dog they want to bet on. after a dog is chosen I have a textbox named txtAmount which they then enter an amount they'd like to bet. they then press a button named btnBet which confirms their bet and allows another player to bet.

my question is this: is it possible to allow the numbers entered into the textbox to only go up to 15? (I already have it so that only numbers can be enter, but I want to restrict what number it can go up to), or would it be better to just put in a ComboBox??

UPDATE

here is the code I used to restrict the textbox to numbers only:

    private void txtAmount_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar != '\b')
        {
            e.Handled = !char.IsNumber(e.KeyChar);
        }
    }

my question is not a duplicate to the other question mentioned in the comment. here's why: I'm not asking how to restrict to just numbers, I'm asking how to restrict to certain numbers (1-15 inclusive)

John Kenny
  • 27
  • 4
  • 2
    consider a `NumericUpDown` – Ňɏssa Pøngjǣrdenlarp Jun 11 '15 at 22:58
  • @Plutonix That is one of the options in the answer on the possible duplicate question I have just linked too. ;-) – Dijkgraaf Jun 11 '15 at 23:03
  • @Dijkgraaf, my question is similar, but I've already restricted to just numbers, i was asking if it was possible to restrict which numbers can be entered for example: i want the betting player to be able to bet any amount between 1 and 15 (inclusive). but to not be able able to enter any number larger than 15 – John Kenny Jun 11 '15 at 23:05
  • Specifically, [this answer](http://stackoverflow.com/a/17893102/76337) is probably what the OP wants, since it's not just about rejecting non-digits. – John Saunders Jun 11 '15 at 23:06
  • @JohnKenny That duplicate question also has a lot of other methods that are mentioned. P.S. Your code might not work for pasted values. You may want to test for that. As per John Saunders comments you may want to look at using 'Validating' – Dijkgraaf Jun 11 '15 at 23:15
  • @Dijkgraaf I did have a look at that other question, but i didn't see any answers that helped me, as i'm looking to not only restrict to just numbers, but which numbers can be entered. and i just did a test on it, pasting does not work, numbers can be pasted in, but nothing else. – John Kenny Jun 11 '15 at 23:20
  • A NUD will constrain the value to be between the Min and Max. yes they paste an out of range value but they cannot submit it - as soon as they leave the control, it constrains the value. Your control over what the user does on their own computer remains intact – Ňɏssa Pøngjǣrdenlarp Jun 11 '15 at 23:35

1 Answers1

-2

Well, I don't know how you enable only numbers to be entered but when I did it I did it by creating a new 'num-only' Textbox which inherited everything from the normal Textbox and was then overriding the keyPressed function like this ( https://stackoverflow.com/a/463335 ). If the input was not a number, the input would not be accepted. You could just add another if statement checking if the amount entered is higher than fifteen with the new number added and if it would be not add the new number to the Textbox.

Community
  • 1
  • 1
  • The `KeyPress` event is good for checking individual keys as they come in. But [`Validating`](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx) is better for checking the entire entered contents. – John Saunders Jun 11 '15 at 23:08
  • Can't you just add if(int.tryParse(txtAmount.text + e.KeyChar) > 15){e.handled = true;} – seb-o-matic Jun 11 '15 at 23:13
  • @seb-o-matic I'm a little confused.. where would i put that piece of code?? – John Kenny Jun 11 '15 at 23:28
  • In the keyPress function after your check `if(e.KeyChar !='\b'){...}` – seb-o-matic Jun 11 '15 at 23:30
  • Oh sorry, text with a uppercase T, so txtAmount.Text ( https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.text.aspx ) – seb-o-matic Jun 11 '15 at 23:46
  • You could do that, but wouldn't it be better to wait for the full number? That would allow backspace, and other editing. – John Saunders Jun 12 '15 at 00:01
  • I see what you mean, but I thought that's what the OP asked for. Another method would be to just accept any number and then when 'submit' is clicked just reduce it to 15 if it is bigger. – seb-o-matic Jun 12 '15 at 00:05
  • i decided to go with that, and just bring up a Message Box informing the players that they can only enter a number up to and including 15 – John Kenny Jun 12 '15 at 03:13