[Gelöst] Suche Lösung für Float Input Gadget

Für allgemeine Fragen zur Programmierung mit PureBasic.
Benutzeravatar
Danilo
-= Anfänger =-
Beiträge: 2284
Registriert: 29.08.2004 03:07

Re: [Gelöst] Suche Lösung für Float Input Gadget

Beitrag von Danilo »

Mein letzter Code schneidet eine Eingabe einfach ab, wenn man eine falsche Eingabe macht.
Zum Beispiel mit dem Cursor auf die Mitte von "1234567" gehen und dann die Taste 'a' drücken.

Ist mir erst eingefallen als ich Computer ausgeschalten hatte. :)

Immer den ganzen Text scannen sollte da besser sein:

Code: Alles auswählen

Procedure checkFloatInput(gadget)
    SendMessage_(GadgetID(gadget),#EM_GETSEL,@start,0)
    txt$ = GetGadgetText(gadget)
    *p.Character = @txt$
    While *p\c ; <> 0
        If *p\c = '.'
            pointcount+1
            If pointcount < 2
                new$ + Chr(*p\c)
            Else
                If start>count : start-1 : EndIf
            EndIf
        ElseIf *p\c >= '0' And *p\c <= '9'
            new$ + Chr(*p\c)
        Else
            start - 1
        EndIf
        *p + SizeOf(Character)
        count + 1
    Wend
    SetGadgetText(gadget,new$)
    SendMessage_(GadgetID(gadget),#EM_SETSEL,start,start)
EndProcedure

If OpenWindow(0, 0, 0, 322, 205, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0, 8,  10, 306, 20, "")
    StringGadget(1, 8,  35, 306, 20, "1234567")
    Repeat
        event = WaitWindowEvent()
        If     event = #PB_Event_CloseWindow : End
        ElseIf event = #PB_Event_Gadget
            If EventType()=#PB_EventType_Change
                checkFloatInput(EventGadget())
            EndIf
        EndIf
    ForEver
EndIf
cya,
...Danilo
"Ein Genie besteht zu 10% aus Inspiration und zu 90% aus Transpiration" - Max Planck
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Re: [Gelöst] Suche Lösung für Float Input Gadget

Beitrag von ts-soft »

Habe Danilos Codebeispiel mal um Minus-Werte erweitert:

Code: Alles auswählen

EnableExplicit

Procedure checkFloatInput(gadget)
    Protected start, count, pointcount, new$
    SendMessage_(GadgetID(gadget), #EM_GETSEL, @start, 0)
    Protected txt$ = GetGadgetText(gadget)
    Protected *p.Character = @txt$
    
    While *p\c ; <> 0
        If *p\c = '.'
            pointcount+1
            If pointcount < 2
                new$ + Chr(*p\c)
            Else
                If start>count : start-1 : EndIf
            EndIf
        ElseIf count = 0 And *p\c = '-'
          new$ + Chr('-')
        ElseIf *p\c >= '0' And *p\c <= '9'
            new$ + Chr(*p\c)
        Else
            start - 1
        EndIf
        *p + SizeOf(Character)
        count + 1
    Wend
    SetGadgetText(gadget, new$)
    SendMessage_(GadgetID(gadget), #EM_SETSEL, start, start)
EndProcedure

Define event
If OpenWindow(0, 0, 0, 322, 205, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0, 8,  10, 306, 20, "")
    StringGadget(1, 8,  35, 306, 20, "1234567")
    Repeat
        event = WaitWindowEvent()
        If     event = #PB_Event_CloseWindow : End
        ElseIf event = #PB_Event_Gadget
            If EventType() = #PB_EventType_Change
                checkFloatInput(EventGadget())
            EndIf
        EndIf
    ForEver
EndIf 
Hoffe es funktioniert jetzt noch :)
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
Benutzeravatar
Danilo
-= Anfänger =-
Beiträge: 2284
Registriert: 29.08.2004 03:07

Re: [Gelöst] Suche Lösung für Float Input Gadget

Beitrag von Danilo »

ts-soft hat geschrieben:Habe Danilos Codebeispiel mal um Minus-Werte erweitert:
Danke! :allright:
cya,
...Danilo
"Ein Genie besteht zu 10% aus Inspiration und zu 90% aus Transpiration" - Max Planck
Antworten