24 de mayo de 2016

Cuadros de textos con indicios o "hints"

Un ejemplo de como hacer nuestros cuadros de textos con indicios.

* TextBoxes con Hints o "Indicios"

WITH CREATEOBJECT("xForm")
  .SHOW(1)
ENDWITH
RETURN


DEFINE CLASS xForm AS FORM
  HEIGHT = 170
  WIDTH  = 250

  ADD OBJECT txt1 AS HintTextBox WITH LEFT = 10, TOP = 10 , HintText = "Nombre de usuario", VALUE = "Usuario"
  ADD OBJECT txt2 AS HintTextBox WITH LEFT = 10, TOP = 40 , HintText = "Contraseña anterior"
  ADD OBJECT txt3 AS HintTextBox WITH LEFT = 10, TOP = 70 , HintText = "Nueva contraseña"
  ADD OBJECT txt4 AS HintTextBox WITH LEFT = 10, TOP = 100, HintText = "Confirmar la contraseña"

  ADD OBJECT cmdExit AS COMMANDBUTTON WITH LEFT = 70, TOP = 130, HEIGHT = 30, CANCEL = .T., CAPTION = "Salir"

  PROCEDURE cmdExit.CLICK
    THISFORM.HIDE()
  ENDPROC
ENDDEFINE


DEFINE CLASS HintTextBox AS TEXTBOX
  LabelName = ""
  HintText  = ""
  KeyPressed = .F.
  WIDTH = 200
  SELECTONENTRY = .T.

  PROCEDURE INIT
    LOCAL sName
    WITH THISFORM
      sName = SYS(2015)
      .ADDOBJECT(sName, "HintLabel", THIS.HintText)
      THIS.LabelName = sName
      WITH .&sName
        .TextBoxName = THIS.NAME
        .BACKCOLOR = THIS.BACKCOLOR
        .MOVE(THIS.LEFT + THIS.MARGIN + 1, THIS.TOP + THIS.MARGIN + 1)
        .WIDTH = THIS.WIDTH - THIS.MARGIN * 2
      ENDWITH
    ENDWITH
  ENDPROC

  PROCEDURE REFRESH
    WITH THIS
      .UpdateHint()
    ENDWITH
  ENDPROC

  PROCEDURE GOTFOCUS
    WITH THIS
      .UpdateHint()
    ENDWITH
  ENDPROC

  PROCEDURE LOSTFOCUS
    WITH THIS
      .KeyPressed = .F.
      .UpdateHint()
    ENDWITH
  ENDPROC

  PROCEDURE UpdateHint
    WITH EVALUATE([ThisForm.] + THIS.LabelName)
      .VISIBLE = EMPTY(THIS.VALUE) AND ! THIS.KeyPressed
    ENDWITH
    RETURN

  PROCEDURE INTERACTIVECHANGE
    WITH THIS
      .KeyPressed = .T.
      .UpdateHint()
    ENDWITH
  ENDPROC

ENDDEFINE

DEFINE CLASS HintLabel AS LABEL
  FORECOLOR = RGB(100, 100, 100)
  BACKSTYLE = 1
  TextBoxName = ""

  PROCEDURE INIT (sCaption)
    IF PCOUNT() > 0
      THIS.CAPTION = sCaption
    ENDIF
  ENDPROC

  * Hago al label "transparente" a los clicks
  PROCEDURE CLICK
    WITH EVALUATE([ThisForm.] + THIS.TextBoxName)
      .KeyPressed = .T.
      .UpdateHint()
      .SETFOCUS()
    ENDWITH
  ENDPROC

ENDDEFINE

*---

Mario López

3 comentarios :

  1. Excelente aporte. Lo he incluido en mis proyectos y ha quedado muy resultón

    ResponderBorrar
  2. Lo he incorporado en mi proyecto y ha quedado muy aparente. Le da otro aire a los formularios

    ResponderBorrar
  3. Si lo bueno es simple, doblemente bueno.

    ResponderBorrar

Los comentarios son moderados, por lo que pueden demorar varias horas para su publicación.