# "... [Y]ou need a binding on the key event for that entry. # # Using the procedures below simply make this call: # # entryLimitWidth .entry 20 # # And it should then do as you wish." ################################################################################ # # Name: keyIsDisplayable # # Synopsis: This procedure takes a character value and determines whether # or not it is an ASCII displayable character or not. # ################################################################################ proc keyIsDisplayable { keyChar } { # # Empty string is not displayable. # if { $keyChar == "" } { return 0 } # # Looks like displayable ASCII. # if { $keyChar >= " " && $keyChar <= "~" } { return 1 } # # Don't know what it is. # return 0 } ################################################################################ # # Name: entryLimitWidth # # Synopsis: This procedure is used to limit the number of characters # allowed in an entry widget. This is done by creating a binding # for the entry widget on the event so that each time the # user types a key in the entry widget, the binding is executed # to check the current size of the input string. The entry wigdet # and its maximum size will be passed as parameters, so to use # them in the bind command a string representing that bind command # must be built and then evaluated using "eval". # ################################################################################ proc entryLimitWidth { entryWidget maxWidth } { # # Do not allow more than maxWidth characters in the entry field. A "bind # command" string is built so that the passed variables may be used in the # binding. If the length of the entry string exceeds the desinated maximum # width, else sound the bell and do not allow the user to enter any more # characters. # set bindCommand [ format "bind %s { \n \ if { \[ keyIsDisplayable %%A \] } { \n if { \[ string length \[ %%W get \] \] >= %d } { \n \ bell \n \ break } } }" $entryWidget $maxWidth ] # # Use "eval" to execute the "bind command" string. # eval $bindCommand }