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

Setting SelStart in Edit Box

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I'm trying to set the SelStart value in the Keypress of an EditBox. The client wants to use Shift+Tab to insert Tabs into the EditBox not the Tab key. I have the following code in the keypress of the EditBox. It works except I can't get the SelStart value to work. It always goes to 0. I've used code similar to this in a button to insert the date/time. What am I missing? VFP 9
Code:
LPARAMETERS nKeyCode, nShiftAltCtrl

IF ThisForm.RecordEdit
	IF nShiftAltCtrl = 1 AND nKeyCode = 15	&& Shift+Tab
		NODEFAULT
		WITH This
			mySelStart = .SelStart
			* Insert a Tab
			.Value = SubStr(.Value, 1, mySelStart) ;
				+ CHR(9) ;
				+ SubStr(.Value, mySelStart + 1)
			*.Refresh()
			.SelStart = mySelStart + 1
		Endwith
		Return
	ENDIF
ENDIF


Auguy
Northwest Ohio
 
Auguy,

You might need to set the AllowTabs property to .T.

I realise that you don't want to let the user use the Tab key (on its own), but I think you still need to set that property. Worth a try, at least.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Thanks Mike, It didn't seem to make any difference with AllowTabs on or off. I even tried
Code:
.AllowTabs = .T.
KEYBOARD '{TAB}' PLAIN Clear
.AllowTabs = .F.
with no luck. It just set focus to the next field.

Auguy
Northwest Ohio
 
If you don't set AllowTabs to .T., like Mike suggests, both TAB and SHIFT+TAB are hotkeys for tabbing between controls, forward with TAB, backward with SHIFT+TAB. That's most probably why SHIFT+TAB does not work as expected.

Could you negotiate them using ALT+TAB instead. then simply do this:

Code:
LPARAMETERS nKeyCode, nShiftAltCtrl

If This.AllowTabs
   Dodefault(nKeyCode, nShiftAltCtrl)
   This.AllowTabs = .F.
   Nodefault
Endif

IF ThisForm.RecordEdit AND Bittest(nShiftAltCtrl,0) AND nKeyCode = 15 && Shift+Tab        
   NODEFAULT
   This.AllowTabs = .T.
   Keyboard '{TAB}'
ENDIF

This will allow tabs only for one TAB when you press SHIFT+TAB, swallow the SHIFT+TAB and instead put a TAB on the keyboard buffer. Next Keypress will then do this tab, disallow firther TABs and there you go...

Bye, Olaf.
 
Besides this code working (I tested it), SHIFT and TAB is for backward tabbing to other controls normally. ALT+CTRL is for tabbing between apps and CTRL+TAB is for tabbing between forms. So I don't know a better way to handle both allowing tabs in the editbox and for tabbing forward, nevertheless I just want to pint out that you take away the possibility to tab backwards.

Bye, Olaf.
 
Thanks Olaf, It didn't work in my editbox class, I will review that when I have time. It did work in a generic editbox. Looks like I have investigation to do.


Auguy
Northwest Ohio
 
Oops, made a mistake. This editbox is a generic editbox in a grid, not my base editbox class. The tabs are inserted using this code, but the cursor returns to the beginning of the field and the tabs disappear as soon as I exit the field??? Not sure why. More investigation ahead.


Auguy
Northwest Ohio
 
I've ssen the bahaviour you describe too, when you add something else but TABS, like an additional space or letter, everything's fine. I don't see what's causing this effect.

Simple solution might be this.setfocus() from interactivechange(). This will go through validate and save the value in the controlsource.

Bye, Olaf.

 
Thanks Olaf, I will try to experiment with the set focus, etc. Sure is strange. One of my editbox classes contains a button to insert the date/time at the current cursor position and it works just fine in repositioning the cursor after the insert. But it does use the set focus to return the user to the editbox.

Auguy
Northwest Ohio
 
I found a work around that isn't perfect and would probably not be the way to go for memo fields that might cotain a lot of text. I stll have to account for the CR & LF to make it complete. I used the Keyboard command with the RightArrow.
Code:
IF nShiftAltCtrl = 1 AND nKeyCode = 15	&& Shift+Tab
	NODEFAULT
	WITH This
		Thisform.LockScreen = .T.
		mySelStart = .SelStart
		* Insert a Tab
		.Value = SubStr(.Value, 1, mySelStart) ;
			+ CHR(9) ;
			+ SubStr(.Value, mySelStart + 1)
		FOR iii = 1 TO mySelStart + 1
			Keyboard '{RIGHTARROW}'
		Next
		Thisform.LockScreen = .F.
	Endwith
ENDIF

Auguy
Northwest Ohio
 
Ok here's the final version (I hope)
Code:
IF ThisForm.RecordEdit
	IF nShiftAltCtrl = 1 AND nKeyCode = 15	&& Shift+Tab
		NODEFAULT
		WITH This
			Thisform.LockScreen = .T.
			mySelStart = .SelStart
			* Insert a Tab
			.Value = SubStr(.Value, 1, mySelStart) ;
				+ CHR(9) ;
				+ SubStr(.Value, mySelStart + 1)
			* Cursor always goes to beginning of field
			* Put cursor back to where it should be
			LOCAL iii
			FOR iii = 1 TO mySelStart
				IF ASC(SUBSTR(.Value,iii,1)) <> 10	&& (Treat CR (13) & LF (10) as one character)
				    Keyboard '{RIGHTARROW}'
				Endif
			Next
			Thisform.LockScreen = .F.
		Endwith
	ENDIF
ENDIF
Note: In my testing the RightArrow code is only needed when the editbox is in a grid. I will do some more testing to see if this holds true.

Any comments or improvements are welcome.

Auguy
Northwest Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top