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!

TextBox Manipulation 3

Status
Not open for further replies.

astech

Programmer
Jul 25, 2000
59
ID
Hi All,

I have confuse with textbox focus.
I already set format with K, and the meaning when cursor go to the textbox, it's will be block and when i type anything, it will be replaced with the new entry. But when the textbox clicking by mouse the K format is ignored. and the meaning when i type anything, it will be inserting my type.

Now my question is how to make a textbox is like in MS-excel.
When i put any key, it always replace with the new one, but when i press F2, it will be insert.

Thanks.
 
This may get you started.

Darrell

Code:
LOCAL oForm
oForm = CREATEOBJECT("clsForm")
oForm.SHOW()

READ EVENTS

DEFINE CLASS clsForm AS FORM
  DOCREATE = .T.
  AUTOCENTER = .T.
  CAPTION = "Excel-Like text box"

  ADD OBJECT lbl1 AS LABEL WITH ;
    TOP = 2, ;
    LEFT = 10, ;
    CAPTION = "Regular textbox"

  ADD OBJECT txtNonExcelLike AS TEXTBOX WITH ;
    TOP = THIS.lbl1.TOP+THIS.lbl1.HEIGHT, ;
    LEFT = 10, ;
    WIDTH = 200

  ADD OBJECT lbl2 AS LABEL WITH ;
    TOP = THIS.txtNonExcelLike.TOP+THIS.txtNonExcelLike.HEIGHT + 5, ;
    LEFT = 10, ;
    CAPTION = "'Excel-Like' textbox"

  ADD OBJECT txtExcelLike AS TEXTBOX WITH ;
    TOP = THIS.lbl2.TOP+THIS.lbl2.HEIGHT, ;
    LEFT = 10, ;
    WIDTH = 200, ;
    SELECTONENTRY = .T., ;
    SELECTEDFORECOLOR = THIS.txtExcelLike.FORECOLOR, ;
    SELECTEDBACKCOLOR = THIS.txtExcelLike.BACKCOLOR

  * Remove the last two property settings above
  * to see the selected text when not in insert mode(F2)

  PROCEDURE DESTROY
    CLEAR EVENTS
  ENDPROC


  * Methods that implement the 'Excel-Like' functionality

  PROCEDURE txtExcelLike.KEYPRESS
    LPARAM nKeyCode, nShiftAltCtrl
    IF nKeyCode==-1
      IF THIS.SELECTONENTRY
        THIS.SELECTONENTRY = .F.
        THIS.SELSTART = LEN(ALLT(THIS.VALUE))
      ENDIF
      NODEFAULT
    ENDIF
  ENDPROC

  PROCEDURE txtExcelLike.LOSTFOCUS
    THIS.SELECTONENTRY = .T.
  ENDPROC
  
ENDDEFINE
 
Modified and tested.

Also, the code needs to fixed for non character controlsources.

Darrell

Code:
LOCAL oForm
oForm = CREATEOBJECT("clsForm")
oForm.SHOW()

READ EVENTS

DEFINE CLASS clsForm AS FORM
  DOCREATE = .T.
  AUTOCENTER = .T.
  CAPTION = "Excel-Like text box"

  ADD OBJECT lbl1 AS LABEL WITH ;
    TOP = 2, ;
    LEFT = 10, ;
    CAPTION = "Regular textbox"

  ADD OBJECT txtNonExcelLike AS TEXTBOX WITH ;
    TOP = THIS.lbl1.TOP+THIS.lbl1.HEIGHT, ;
    LEFT = 10, ;
    WIDTH = 200

  ADD OBJECT lbl2 AS LABEL WITH ;
    TOP = THIS.txtNonExcelLike.TOP+THIS.txtNonExcelLike.HEIGHT + 5, ;
    LEFT = 10, ;
    CAPTION = "'Excel-Like' textbox"

  ADD OBJECT txtExcelLike AS TEXTBOX WITH ;
    TOP = THIS.lbl2.TOP+THIS.lbl2.HEIGHT, ;
    LEFT = 10, ;
    WIDTH = 200, ;
    SELECTONENTRY = .T., ;
    bClicked = .F., ;
    SELECTEDFORECOLOR = THIS.txtExcelLike.FORECOLOR, ;
    SELECTEDBACKCOLOR = THIS.txtExcelLike.BACKCOLOR

  * Remove the last two property settings above
  * to see the selected text when not in insert mode(F2)

  PROCEDURE DESTROY
    CLEAR EVENTS
  ENDPROC


  * Methods that implement the 'Excel-Like functionality

  PROCEDURE txtExcelLike.KEYPRESS
    LPARAM nKeyCode, nShiftAltCtrl
    IF nKeyCode==-1
      IF THIS.SELECTONENTRY
        THIS.SELECTONENTRY = .F.
        THIS.SELSTART = LEN(ALLT(THIS.VALUE))
      ENDIF
      NODEFAULT
    ENDIF
  ENDPROC

  PROCEDURE txtExcelLike.LOSTFOCUS
    THIS.SELECTONENTRY = .T.
  ENDPROC

  PROCEDURE txtExcelLike.CLICK
    THIS.bClicked = THIS.SELECTONENTRY
    IF THIS.bClicked
      THIS.SELSTART = 0
      THIS.SELLENGTH = LEN(THIS.VALUE)
    ENDIF
  ENDPROC
ENDDEFINE
 
>> But when the textbox clicking by mouse the K format is ignored. and the meaning when i type anything, it will be inserting my type. <<

Create your own custom textbox class with this code in its GotFocus() and use it instead:

Code:
WITH This
  IF .SelectOnEntry
    TextBox::GotFocus()
    .SelStart = 0
    .SelLength = LEN( This.Text )
    NODEFAULT
  ENDIF
ENDWITH

The reason that you have to do it like this is that the default VFP base class GotFocus behavior is to reset .SelStart and .SelLength to 0 when you click on the control with the couse. So you need the NODEFAULT to make sure that the textbox stays selected.


Marcia G. Akins
 
Marcia's gotfocus() method code is much cleaner.

Darrell

Code:
LOCAL oForm
oForm = CREATEOBJECT("clsForm")
oForm.SHOW()

READ EVENTS

DEFINE CLASS clsForm AS FORM
  DOCREATE = .T.
  AUTOCENTER = .T.
  CAPTION = "Excel-Like text box"

  ADD OBJECT lbl1 AS LABEL WITH ;
    TOP = 2, ;
    LEFT = 10, ;
    CAPTION = "Regular textbox"

  ADD OBJECT txtNonExcelLike AS TEXTBOX WITH ;
    TOP = THIS.lbl1.TOP+THIS.lbl1.HEIGHT, ;
    LEFT = 10, ;
    WIDTH = 200

  ADD OBJECT lbl2 AS LABEL WITH ;
    TOP = THIS.txtNonExcelLike.TOP+THIS.txtNonExcelLike.HEIGHT + 5, ;
    LEFT = 10, ;
    CAPTION = "'Excel-Like' textbox"

  ADD OBJECT txtExcelLike AS TEXTBOX WITH ;
    TOP = THIS.lbl2.TOP+THIS.lbl2.HEIGHT, ;
    LEFT = 10, ;
    WIDTH = 200, ;
    SELECTONENTRY = .T.
  *!*	    , ;
  *!*	    SELECTEDFORECOLOR = THIS.txtExcelLike.FORECOLOR, ;
  *!*	    SELECTEDBACKCOLOR = THIS.txtExcelLike.BACKCOLOR

  * Remove the last two property settings above
  * to see the selected text when not in insert mode(F2)

  PROCEDURE DESTROY
    CLEAR EVENTS
  ENDPROC


  * Methods that implement the 'Excel-Like functionality

  PROCEDURE txtExcelLike.KEYPRESS
    LPARAM nKeyCode, nShiftAltCtrl
    IF nKeyCode==-1 && F2 key
      IF THIS.SELECTONENTRY
        THIS.SELECTONENTRY = .F.
        THIS.SELSTART = LEN(THIS.TEXT)
      ENDIF
      NODEFAULT
    ENDIF
  ENDPROC

  PROCEDURE txtExcelLike.LOSTFOCUS
    THIS.SELECTONENTRY = .T.
  ENDPROC

  * MarciaAkins's code

  PROCEDURE txtExcelLike.GOTFOCUS()
    WITH THIS
      IF .SELECTONENTRY
        TEXTBOX::GOTFOCUS()
        .SELSTART = 0
        .SELLENGTH = LEN( THIS.TEXT )
        NODEFAULT
      ENDIF
    ENDWITH
  ENDPROC
ENDDEFINE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top