Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 User input-text field destroys graphics
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
syntax0r




PostPosted: Mon May 23, 2005 3:50 pm   Post subject: User input-text field destroys graphics

I'm stuck in a situation where I want the user to type in something, then hit enter, which will execute the corresponding, wanted command (the basic case construct menu).

However, I do not want to allow the user to input so many spaces/characters, that it starts covering my window with white spaces or whatever character it might be.

Is there a way to limit the space (preferable only 1 character) for the user to his/her input?

Yes, yes... some of you may say "just use the getch statement." Well, that statement would not be pratical, as I want the user to have the chance to confirm his/her command, by erasing the text with the backspace key.

Perhaps, there is a GUI text-field way to do all this. I'll look into that, but i really dont want to go all complicated with GUI usage.

So, I wish there'a a way to limit the characters being displayed when using the "get" statement.

Any suggestions?
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Mon May 23, 2005 4:24 pm   Post subject: (No subject)

syntax0r wrote:
Yes, yes... some of you may say "just use the getch statement." Well, that statement would not be pratical, as I want the user to have the chance to confirm his/her command, by erasing the text with the backspace key.

You've got it. Use the getch() command. Wink
To get backspaces to work, you simply need to check first if the key that was pressed is the backspace key. If it is, check if there is in fact some text already in the variable. If there is then we delete a character. Going back one if statement, if the key pressed is not backspace (else) add it to the variable. Keep in mind that you should be checking for the enter key even before you check for the backspace key.
If you want to limit the number of characters the user inputs, you can do that with getch. When you go into the final else (you're about to add the character to the word variable) check if the length of the string is less than the maximum allowed length.
The other way to prevent too much space from being used up is by using a text field idea: you scroll the text along as they type it, and only draw text that is between two points. Making a basic textbox is actually quite easy. You just have to keep track of the location of the first character to display within the string and the location of the last character. For example, if we have the word "elephant" within a textbox that is only wide enough to fit, in general, 5 characters; we can't fit the whole thing in there. Say the first character to show is the 'l'. Thus, the start position is the second character in the string. Then, since we know this and the width of the textbox, we go through a decreasing for loop from the length of the string to the start position. We check if the Font.Width of the string from the start position to the for loop incrimenting variable is less than the width of the textbox. If it is, then we set that as out end point and exit the for loop (if we didn't exit, we would get the string going from the start position to the start position, which probably isn't what we want). You can use a similar process if you know the end position. You can't say you'll always know the start position but always have to figure out the end position, however, because of the situations involving the "END" and "HOME" keys. If you press the END key, you automatically know the end position (end pos would equal length of the string) and have to determine the start position. Thus, you'll need to use both methods, depending on the input.

So anyways, good luck with however you're choosing to tackle this. Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: