checkInt: wie Intervallbegrenzung?

Anfängerfragen zum Programmieren mit PureBasic.
Benutzeravatar
ProgOldie
Beiträge: 236
Registriert: 19.05.2012 17:09
Computerausstattung: Windows11, Arduinos, Pi3, PureBasic 6.02

checkInt: wie Intervallbegrenzung?

Beitrag von ProgOldie »

Von Danilo und ts-soft stammt ein prima Programm zum Testen der Eingabe von Kommazahlen.
Ich habe es etwas abgespeckt für ganzahlige Werte.

Code: Alles auswählen

EnableExplicit
Define.i Ev

Procedure checkInt()
  ;nach Danilo und ts-Soft
  Protected gadget.i
  Protected start, count, pointcount, new$
  gadget=GetActiveGadget()
  SendMessage_(GadgetID(gadget), #EM_GETSEL, @start, 0)
  Protected txt$ = GetGadgetText(gadget)
  Protected *p.Character = @txt$
  
  While *p\c 
    If 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

If OpenWindow(0,0,0,500,400,"Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  StringGadget(1,50,50,100,50,"")
  BindGadgetEvent(1,@checkInt(),#PB_EventType_Change)
EndIf

Repeat
  Ev=WaitWindowEvent()
Until Ev=#PB_Event_CloseWindow
Wie baue ich in checkInt ein, dass nur ganzzahlige Werte zwischen z.B. -25 und 30 akzeptiert werden?

Ich finde die wahrscheinlich total einfache Lösung nicht. Vielleicht liegt's auch am Frust über den BL-Abstieg von Paderborn....
Windows10 / PB5.70 / Arduino (-Due) / Raspberry Pi3 /Linux Mint 18
Benutzeravatar
edel
Beiträge: 3667
Registriert: 28.07.2005 12:39
Computerausstattung: GameBoy
Kontaktdaten:

Re: checkInt: wie Intervallbegrenzung?

Beitrag von edel »

Dürfte ja reichen wenn du Werte ausserhalb deines Bereiches einfach ueberschreibst.

Code: Alles auswählen

Procedure checkInt()
  ;nach Danilo und ts-Soft
  Protected gadget.i
  Protected start, count, pointcount, new$
  gadget=GetActiveGadget()
  SendMessage_(GadgetID(gadget), #EM_GETSEL, @start, 0)
  Protected txt$ = GetGadgetText(gadget)
  Protected *p.Character = @txt$
  
  While *p\c 
    If 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
  
  If Val(new$) < -25
    new$ = "-25"
  EndIf
  
  If Val(new$) > 30
    new$ = "30"
  EndIf  
  
  
  SetGadgetText(gadget, new$)
  SendMessage_(GadgetID(gadget), #EM_SETSEL, start, start)
EndProcedure
Benutzeravatar
ProgOldie
Beiträge: 236
Registriert: 19.05.2012 17:09
Computerausstattung: Windows11, Arduinos, Pi3, PureBasic 6.02

Re: checkInt: wie Intervallbegrenzung?

Beitrag von ProgOldie »

Hab's jetzt doch selbst hinbekommen. Jetzt wird eine neue Ziffer nur angenommen, wenn der Wert in den Grenzen liegt.

Code: Alles auswählen

EnableExplicit
Procedure checkInt()
  ;nach Danilo und ts-Soft
  ;Prüft, ob Werte  (im Beispiel) zwischen -25 und +30 liegen
  Protected.i gadget,start,count
  Protected new$,lookup$
  gadget=GetActiveGadget()
  SendMessage_(GadgetID(gadget), #EM_GETSEL, @start, 0)
  Protected txt$ = GetGadgetText(gadget)
  Protected *p.Character = @txt$
  While *p\c 
    If count = 0 And *p\c = '-'
      new$ + Chr('-')
    ElseIf count=0 And *p\c='+'
      new$ + Chr('+')
    ElseIf *p\c >= '0' And *p\c <= '9'
      lookup$=new$+Chr(*p\c)
      If Val(lookup$) >=-25 And Val(lookup$)<=30
        new$=lookup$
      Else
        start -1
      EndIf
    Else
      start - 1
    EndIf
    *p + SizeOf(Character)
    count + 1
  Wend
  SetGadgetText(gadget, new$)
  SendMessage_(GadgetID(gadget), #EM_SETSEL, start, start)
EndProcedure

Define.i Ev
If OpenWindow(0,0,0,500,400,"Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  StringGadget(1,50,50,100,50,"")
  BindGadgetEvent(1,@checkInt(),#PB_EventType_Change)
EndIf

Windows10 / PB5.70 / Arduino (-Due) / Raspberry Pi3 /Linux Mint 18
Antworten