SOLVED: Max characters in a textbox

Started by steptoe, Thu 15/12/2011 08:50:10

Previous topic - Next topic

steptoe

Hi

I'm trying to put a max characters allowed in a textbox by user. (Max length will be 15)

TextBox  name is: tbInput1

Code: ags

if(tbInput1.Text.Chars[tbInput1.Text.Length 15])


I get:

GlobalScript.asc(863): Error (line 863): Parse error in expr near 'tbInput1'


My code at the moment is:

Code: ags

function Button11_OnClick(GUIControl *control, MouseButton button)
{
   if (tbInput1.Text != "")
 
 {
   player.Name = tbInput1.Text;
   gName.Visible=false;
   player.Name = tbInput1.Text;
   Leddie.Text = String.Format("%s", player.Name);
   ggameonfo.Visible=true;
}
  else
  Display("You can't be the Nameless One!");
}


Or could I use a variable of some sort?

Looking for the right solution, can you help?

cheers




Gilbert

Just do something like:
Code: ags
if(tbInput1.Text.Length > 15) { //blah }


For example, if you want to actively limit the length of the text while the player is inputing something just put the code below in repeatedly execute:
Code: ags
if(tbInput1.Text.Length > 15) tbInput1.Text = tbInput1.Text.Substring(0, 15);

steptoe

#2
Hi Iceboty

works a treat many thanks.

Code: ags

function repeatedly_execute() 
{
  //IF USER TRIES TO TYPES IN TOO MANY CHARACTER
  
  if(tbInput1.Text.Length > 15) { 
  
  tbInput1.Text="Maximum 15 characters";
  Wait(100);
  tbInput1.Text="Please enter Name";
  Wait(100);
  tbInput1.Text="";
  
 }


Notice I added a bit extra because can't run Say/Display etc because of Rep Exe

cheers

;)



monkey0506

You can run blocking commands in repeatedly_execute.

steptoe

Hi Monkey

if I try to do a display, say or saybackground it hangs.

I know i have Wait.

cheers

monkey0506

Sure, if you run Say or Display unconditionally 40 times per second your game is going to hang. But if it's properly encapsulated in a condition that will limit how often the command is run then it works fine.

steptoe

#6
Hi Monkey

A Say for example will hang until I click the mouse then it goes otherwise it hangs and spacebar does nothing to remove it. I think its not too bad with what I have with display in textbox with no say/display. Its durable.

Can you shed further light on:

Quote
Sure, if you run Say or Display unconditionally 40 times per second your game is going to hang. But if it's properly encapsulated in a condition that will limit how often the command is run then it works fine.

cheers


Khris

#7
If you put the Display command inside an if block that ISN'T run 40 times per second, using Say is fine.

Code: ags
function repeatedly_execute() 
{
  //IF USER TRIES TO TYPE IN TOO MANY CHARACTERS
  if (tbInput1.Text.Length > 15) {
    tbInput1.Text = tbInput1.Text.Substring(0, 15);
    Display("Maximum 15 characters");
  }
}


This should work fine because as soon as the Display command is run, steps are taken to make the outcome of the condition false again. Thus the next Display command isn't run until the player again types in a 16th character.
If you press space to remove the Display box, you might accidentally type a space character into the textbox immediately afterwards which of course again triggers the Display command, and so on. If that's indeed what happens, we have to make sure that the player has let go of any key before we continue.

Code: ags
void WaitForNoKeyPressed() {
  int i = eKeySpace;
  bool wait = true;
  while (wait) {
    wait = false;
    while (i < eKeyDelete) {
      if (IsKeyPressed(i)) wait = true;
      i++;
    }
  }
}


Put this function above repeatedly_execute, then put this line after the Display command:
    WaitForNoKeyPressed();

steptoe

#8
Hi Khris

cheers, those codes work a treat as well.

I suppose it just beats last coding as it keeps what you have in the textbox.

Display shows until you do a backspace.

Code: ags

    if (tbInput1.Text.Length > 15) {
    Display("Maximum of 15 characters only. [Use backspace to delete letters.");
    WaitForNoKeyPressed();
    tbInput1.Text = tbInput1.Text.Substring(0, 15);
}


I notice that Caps take up more space in a label which could be a problem I need to look at.

thanks for all help on this.

:=

steptoe




Khris

I've tested this.
My custom Wait function doesn't really work and I'm too lazy to find out why.
The thing is, I don't need it.

Regardless of which key I press to remove the Display message, unless I deliberately hold it down, the message disappears and then the game just sits there, waiting for my input. It works just fine, far from hanging or displaying the message over and over again.

steptoe

Your are absolutely right.

It works very well.

cheers Khris



SMF spam blocked by CleanTalk