Can't delete first numerical number in Textbox

Started by Slasher, Fri 23/09/2022 11:44:37

Previous topic - Next topic

Slasher

Hi,

I have the following scripted for inputting a safe's code. However, if I backspace to enter a different number before confirming the number I cannot delete first numerical number.

Code: ags
if(tbInput.Visible && tbInput.Text != oldtext)
{
  if(tbInput.Text.Chars[tbInput.Text.Length-1] <= '0' || tbInput.Text.Chars[tbInput.Text.Length-1] > '9')
  {
    tbInput.Text = oldtext;
  }
  oldtext = tbInput.Text;
}

Without above code the script works fine.. It was supposed to not except letters!

PS Why the strange font for Code when adding?

Crimson Wizard

Please tell which version of AGS are you using?

Slasher

Quote from: Crimson Wizard on Fri 23/09/2022 12:09:05Please tell which version of AGS are you using?
AGS Editor .NET (Build 3.5.1.16)

Crimson Wizard

Where (which function) do you have that code you posted above?

Slasher


Crimson Wizard

#5
I'm not certain what do you mean by "can't delete first number", like, do you mean a "digit" or a number consisting of several digits, and does it reappear, or something else happening (script errors and so on). But I think that your code has a mistake, where you do not check for the text length, because if text's length is 0 you might get invalid array access error. This might be a problem if you begin deleting characters with backspace.

So that more correct condition would be:
Code: ags
if(tbInput.Text.Length > 0 &&
   (tbInput.Text.Chars[tbInput.Text.Length-1] <= '0' || tbInput.Text.Chars[tbInput.Text.Length-1] > '9'))

Slasher

Thanks Crimson...

I'm trying to implement not to add letters to the Textbox...only numbers..

Cheers

SMF spam blocked by CleanTalk