Another InputBox demo

Source code related resources

Moderator: Rathinagiri

Post Reply
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Another InputBox demo

Post by Pablo César »

Sometimes we think we are loosing time, but when you see the results ans the perspective to re-use codes: you save time :idea:

I prepare this demo, because would be useful to treat string conversion to another types.

Code: Select all

/*
   String Conversion Demo
   By Pablo César Arrascaeta
   On January 14th, 2015
   Useful to use with InputBox and INI files
*/

#include <hmg.ch>

Function Main()
Private aTypes:={'Numeric','Character Alpha','Array','Date','Memo', 'Logical'}

DEFINE WINDOW Win_1 WIDTH 640 HEIGHT 350 TITLE "  Testing string conversion" MAIN

    _DefineHotKey ( "Win_1" , 0 , VK_ESCAPE , {|| ThisWindow.Release() } )
    _DefineHotKey ( "Win_1" , 0 , VK_RETURN , {|| Test() } )
	
	DEFINE RADIOGROUP RadioGroup_1
        ROW    40
        COL    30
        WIDTH  160
        HEIGHT 125
        OPTIONS aTypes
        VALUE 1
        FONTNAME "Arial"
        FONTSIZE 12
        ONCHANGE Test()
		SPACING 40
        FONTBOLD .T.
    END RADIOGROUP

END WINDOW
CENTER WINDOW Win_1
Win_1.Col:=(Win_1.Col)-50
ACTIVATE WINDOW Win_1
Return Nil

Function Test()
Local nOpt:=Win_1.RadioGroup_1.Value
Local cVar:="", cType:=hb_USubStr(aTypes[nOpt],1,1)

DISABLE CONTROL EVENT RadioGroup_1 OF Win_1

If cType="M" ; cVar:="Line # 1"+CRLF+"Line # 2" ; Endif
cVar:=InputBox("Type cVar",If(cType="M","Multi-Line test","cVar ("+aTypes[nOpt]+")"),cVar,,,If(cType="M",.T.,.F.))
If _HMG_SYSDATA [ 257 ]
   MsgStop("Cancelled by user !")
Else
   If IsAllType(cVar,cType)
      MsgInfo("Yes, It's all "+aTypes[nOpt]+" !","  String approved")
   Else
      MsgStop("No, It's not all "+aTypes[nOpt]+" !","  Attention")
   Endif
Endif
ENABLE CONTROL EVENT RadioGroup_1 OF Win_1
DoMethod("Win_1","RadioGroup_1","SetFocus")
Return Nil

Function IsAllType(cVar,cType,lRet)
Local I, nLen:=HMG_Len(cVar)
Local nChar1:=0, nChar2:=0

If !ValType(cVar)="C" ; Return .F. ; Endif
If ValType(cType)="C" ; cType:=HMG_Upper(cType) ; Else ; Return .F. ; Endif
DEFAULT lRet := If(cType="N" .or. cType="C",.T.,.F.)

For I=1 To nLen
    cChar:=hb_USubStr(cVar,I,1)
	Do Case
	   Case cType="N"
	        If cChar="."
			   nChar1++
			   Loop
			Endif
			If cChar="-"
			   nChar2++
			   Loop
			Endif
			If !( HMG_IsDigit(cChar) ) ; lRet:=.F. ; Exit ; Endif
	   Case cType="C"
	        If !( HMG_IsAlpha(cChar) ) ; lRet:=.F. ; Exit ; Endif
	   Case cType="A"
	        If cChar="{" ; nChar1++ ; Endif
	        If cChar="}" ; nChar2++ ; Endif
	   Case cType="D"
	        If cChar="(" ; nChar1++ ; Endif
	        If cChar=")" ; nChar2++ ; Endif
	   Case cType="M"
	        If cChar=Chr(13) .or. cChar=Chr(10) ; lRet:=.T. ; Exit ; Endif
	   Case cType="L"
	        If AScan({".T.","T","TRUE","Y","YES",".F.","F","FALSE","N","NO"},HMG_Upper(cVar)) > 0 ; lRet:=.T. ; Exit ; Endif
	EndCase
Next
If cType="A" .or. cType="D"
   If nChar1 == nChar2 .and. nChar1 > 0
      If cType == "D" 
	     If ("CTOD" $ HMG_Upper(cVar) .or. "STOD" $ HMG_Upper(cVar) .or. "DATE" $ HMG_Upper(cVar))
		    lRet:=RunMacro(cVar)
         Endif
	  Else
	     lRet:=RunMacro(@cVar)
	  Endif
   Endif
Endif
If cType == "N"
   If lRet .and. ( nChar1 > 1 .or. nChar2 > 1 )
      lRet:=.F.
   Endif
Endif
Return lRet

Function RunMacro(xVar)
Local lRet, cTestIt

BEGIN SEQUENCE WITH {|oErr| BREAK( oErr ) }
	cTestIt:=&(xVar)
    lRet:=hb_IsArray(cTestIt)
RECOVER USING oErr
    lRet:=.F.
ENDSEQUENCE
Return lRet
This code above, was re-edited for:
  • Adding Disable/Enable Event
  • Replacing HMG functions for strings when UNICODE
  • Added for Logical types
Screen1.png
Screen1.png (179.62 KiB) Viewed 3025 times
I hope you enjoy it ! :D
Attachments
Demo1 (Executable file).rar
(1.05 MiB) Downloaded 254 times
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
Javier Tovar
Posts: 1275
Joined: Tue Sep 03, 2013 4:22 am
Location: Tecámac, México

Re: Another InputBox demo

Post by Javier Tovar »

Hola Pablo César,

Gracias por compartir!

Una pregunta: Como ingresar una fecha?

Saludos
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Another InputBox demo

Post by Pablo César »

Hola Javier !

Gracias por tu interés.
Javier Tovar wrote:Como ingresar una fecha?
En este ejemplo, que solo se utiliza el InputBox, donde acepta solo variables del tipo caracter y que los trata através de un TextBox o EditBox para el caso de multiline. Debes entonces ingresar con una de estas opciones:

CToD("01/01/2015")
SToD("20150101")
Date()

Pero en mi caso con el GenInput, voy a limitarlo a que acepte SToD() nomás. Obedeciendo la misma regla que el nuestro h_ini.prg y que es baseado en lo de Grigory Filatov para grabar/leer archivos INI que tenemos en nuestra biblioteca.

Ummm ahora que veo, me faltó para el caso del tipo lógico.

Un abrazo,
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
bpd2000
Posts: 1207
Joined: Sat Sep 10, 2011 4:07 am
Location: India

Re: Another InputBox demo

Post by bpd2000 »

Nice
+1
BPD
Convert Dream into Reality through HMG
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Another InputBox demo

Post by Pablo César »

Thank you Dave for your interest.

I've included the logical type too was missing. The aLogicals array is in English, but it could be adapted for local language too.

My first message of this topic has been updated for changing.

In this topic are questioning for InputBox to work without MAIN WINDOW. (Simple and functional, was solved)
Thanks Claudio for your indications. :)

I hope you enjoy ! :D
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
Post Reply