Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

character limited edit control

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have an edit box in a VTcl app that needs to be limited to a certain number of characters. Is there a predefined way to handle this where when the number is met, the next keyboard stroke is a beep and prevents the character from being entered.

Thanks.
 
Check out my response in thread287-311673, "Disable frame and limit entry." It describes a couple of techniques for limiting the number of characters that the user can enter into an entry widget.

A simpler method might be to create a binding on the entry for <KeyPress> events. You could check to see if the result of the <KeyPress> would exceed the limits of the entry, and if so, to execute a break command to stop the propagation of the event to the Entry class bindings (which handle actually inserting the character into the widget). Something along the lines of:

Code:
set max 6  ;# Or whatever limit
bind .e <KeyPress> {
  if {![string equal {} %A]
      && [string is print %A]
      && ([%W index end] >= $max)} {
    break
  }
}

The downside to this approach is that it doesn't prevent a user from copying a chunk of text that exceeds the limits and then pasting it into the entry. The other approaches I describe in the link above do handle that problem. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top