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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Entry Box binding 1

Status
Not open for further replies.

ramsunder

Programmer
Sep 3, 2003
9
DE
Hi,

I had a particular binding in Tk3.6 as
set event "if \{\[ap_filter_uplownum %A \[%W get\] $length\]\} \{%W insert \[%W index insert\] \[string toupper %A\]\}"
eval bind $entry <Any-KeyPress> $event


And, procedure ap_filter_uplownum is defined as

proc ap_filter_uplownum { key entry length} {

if { [ string length $entry ] >= $length } {
return 0
}

if {(($key>=&quot;A&quot;) && ($key<=&quot;Z&quot;)) || (($key>=&quot;a&quot;) && ($key<=&quot;z&quot;)) || (($key>=&quot;0&quot;) && ($key<=&quot;9&quot;)) || ($key==&quot;_&quot;) || ($key==&quot;.&quot;)} {
return 1 }

return 0
}


Now, I want to migrate the same to tk8.4, this binding is not get affected.

I had tried using:
1) break statement, then I am getting a TCL error.
2) I had tried using return -code break in procedure ap_filter_uplownum but, then no TCL error,, but binding is not invoked.

Can any one please help me out.

Thnaks in advance
Ram
 
Tk 8.4 knows about entry validation.
The -validation option lets you choose the type of validation and the -vcmd option lets you define a validation proc with %args.

Your new code could be:
# set max length
set length 5
# validation proc
proc vcmd {length d s S} {
# check operation
if {$d != 1} { return 1; # not an insert operation }
# check length
if {[string length $s] == $length} { return 0; # overflow }
# check inserted chars
if {[string match {[A-z0-9.]} $S]} { return 1 }
# bad char
return 0
}
# create entry
entry .e -validate key -vcmd [list vcmd $length %d %s %S]
# show it
pack .e

For more on entry validation:
HTH

ulis
 
To see the error generated by the break command of your proc, define a bgerror proc:
proc bgerror {args} { tk_messageBox -message $args }

ulis
 
Hi ullis,

I apreciate your reply.
But if you look it carefully, we are converting lower case letters to upper case.

Can you please help me out with your validation procedure how I can do that.

Thanks in advance
Ram
 
The best way to edit the content of the entry during the validating proc is to associate a textvariable to the entry.
But doing so (editing inside the validating proc) reset the -validate option. So you need to reset this option to its value.

Here is the script updated:
# set max length
set length 5
# validation proc
proc vcmd {length [red]w[/red] d s S} {
# check operation
if {$d != 1} { return 1; # not an insert operation }
# check length
if {[string length $s] == $length} { return 0; # overflow }
# check inserted chars
if {[string match {[A-z0-9.]} $S]} { [red]
# get first & last part
set insert [$w index insert]
set first [string range $s 0 [expr {$insert - 1}]]
set last [string range $s $insert end]
# insert upper char
set ::value $first[string toupper $S]$last
# place cursor
$w icursor [incr insert]
# reset the -validate option after the return
after 0 $w config -validate key[/red]
# return
return 1
}
# bad char
return 0
}
# create entry
entry .e -validate key -vcmd [list vcmd $length [red]%W[/red] %d %s %S] [red]-textvariable ::value[/red]
# show it
pack .e
focus -force .e
(maybe better to do)

For more information, see carefully the Validation section of the entry page of the Tk manual:
HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top