I have a textfield, with a limitText = 4
I enter numerical digits for example: 2015, 2016, 2017 etc.
But the following situation presents itself:
Once entered the four numeric digits, it does not allow me to overwrite any digit
and for some reason you need to change some digits, for example 2015 change it to 2003
To solveleo I have to position myself in the digit to be replaced and with the backspace key I delete it and insert the new digit,
but my client needs to directly overwrite the digits to be replaced, for speed and for ease of use.
Raul, would it be easier for the customer if you just selected the entire field and had the customer enter the entire 4 digits? It seems to me that that would be faster for the customer than to force them to position the cursor each time. If it were a larger field, positioning the cursor might be better, but for only 4 digits…
It is not possible do do some stuff when the field have 4 digits (as Raul set) if you select all characters.
The TextField and/or TextArea consider you want to add a fifth character and reject it. Deleting one of the characters works.
But, you can select one and change it or as Dave said use the backspace delete.
Let’s say you entered 2015, but you need to change it to 2003:
you could select all 4 numbers with mouse and enter 2003
you could select the last 2 numbers and enter 03
if the cursor is after 2015, you could press backspace 2 times and enter 03
but you can’t:
move back 2 spaces with the left arrow key and enter 03
use the mouse to put the cursor between 20 and 15, to only enter 03
I’m learning Xojo and wanted to write something about this using Keydown and SelStart.
I created a TextField with mask 9999
Keydown event, and
[code]Dim t As Text
t = me.Text.ToText
If t.Length = 4 and me.selstart < 4 Then
Select Case key
case “0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”
t = t.mid(0,me.SelStart) + key.ToText + t.mid(me.SelStart + 1, 3 - me.SelStart)
me.text = t
me.SelStart = me.SelStart + 1
end select
end if[/code]
I think it works like you want.
Then I changed the code and remove the t. Interesting that t.mid is 0 based and Mid is 1 based:
If len(me.text) = 4 and me.selstart <4 Then
Select Case key
case "0","1","2","3","4","5","6","7","8","9"
'msgbox me.SelStart.ToText
'msgbox me.SelStart.ToText
me.text = Mid(me.text, 1, me.SelStart) + key.ToText + mid(me.text, me.SelStart +2, 3 - me.SelStart)
me.SelStart = me.SelStart + 1
end select
end if
I didn’t know that Mid was 1 based and no 0 based as t.mid, that’s why I have 2 msgbox me.SelStart.ToText there, just to test the value.
Anyone know why the first MsgBox display a 2 when the cursor is between 20 and 15 (in the 2015 example), well this is the expected value, but then the next MsgBox display a 0?
I just read Emile post. Maybe there is different behavior Mac and Windows. I can select all 4 characters and replace them with no problem on my Mac. I will try the same example on Windows.
The code doesn’t work on Windows. It looks like me.SelStart = me.SelStart + 1 is always 1
Let’s say I have 2015 and want 2003, move with left arrow 2 spaces, I enter 0 but then the cursor moves to the first position (between 2 and 005 (now).
Also, if I select all 4 numbers and try to enter another I can’t. On my Mac I can enter another number without problems.
P.S. set the SelText depending on the cursor position. That way you give visual feedback to your user, and you can simply replace the selected text with the key pressed.
[quote=357484:@Emile Schwarz]You can do that on WIndows ?
(Not possible to do that on macOS)[/quote]
No I can’t do it on Windows, but I can do it on macOS. Xojo 2017r2.1, macOS 10.12.6
I just put a TextField with Mask 9999, enter 2015, select all 4 digits with mouse and write 2003. No problem on my Mac.
// set selection
If TextFieldWithOverWriteMode1.OverwriteMode then
// edge case: cursor is at the end of the
if TextFieldWithOverWriteMode1.SelStart = 4 then TextFieldWithOverWriteMode1.SelStart = 3
TextFieldWithOverWriteMode1.SelLength = 1
else
TextFieldWithOverWriteMode1.SelLength = 0
end if
End Sub
[/code]
TextFieldWithOverWriteMode:
[code]Sub KeyUp(Key As String) Handles KeyUp
// the following is necessary because pressing the left arrow on a selected text
// sets the cursor position to the beginning of the selected text,
// and NOT one character further to the left
if me.OverwriteMode AND me.Text.Len = me.LimitText then
// deal with left arrow key
if Asc(Key) = 28 then me.SelStart = me.SelStart - 1
end if
End Sub
[/code]
[code]Sub SelChange() Handles SelChange
If me.OverWriteMode and Len( me.text ) = me.LimitText then
// if cursor at end then reset to last character
if me.SelStart = me.LimitText then me.SelStart = me.LimitText - 1
me.SelLength = 1
end if
End Sub
[/code]
You have to deal with allowed keys (numbers, arrow, delete, return, tab etc) but that should give you a start.[/quote]
Emile, are you saying that with older Xojo the behavior is the same on Windows and Mac (not able to write any number if the 4 numbers are selected), and with newer Xojo now is possible to write on Mac but not on Windows?
I think, if this change is with Xojo, that the different version of Xojo should work the same on Windows and on Mac. At least that’s what a newbie wants to see, that you do something in either OS and the program will do the same on Windows and Mac.
For example (difference on Mac and Windows), why if me.SelStart = 2 when this code start:
[code]Dim t As Text
t = me.Text.ToText
If t.Length = 4 and me.selstart < 4 Then
Select Case key
case “0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”
t = t.mid(0,me.SelStart) + key.ToText + t.mid(me.SelStart + 1, 3 - me.SelStart)
me.text = t
me.SelStart = me.SelStart + 1
end select
end if[/code]
after the code on Mac me.SelStart = 3 and on Windows me.SelStart = 1 ?
Edit: It looks like me.text = t reset me.SelStart to 0 on Windows but not on Mac.