Seite 1 von 1

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

Verfasst: 10.02.2012 20:40
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

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

Verfasst: 10.02.2012 21:01
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 :)

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

Verfasst: 10.02.2012 21:15
von Danilo
ts-soft hat geschrieben:Habe Danilos Codebeispiel mal um Minus-Werte erweitert:
Danke! :allright: