How to use Integers with InputRequester

Just starting out? Need help? Post your questions and find answers here.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

How to use Integers with InputRequester

Post by Distorted Pixel »

Hi

I just need a way to enter a new image size when user clicks "New" in my image editor menu. I can't use Console or TextGadget and I was hoping not to have to use StringGadget. Or can I use StringGadget to do it? I search and found a calculator created, but I don't need that and I didn't see anything I could use from it. I don't need a big program just to do this do I?

Code: Select all

Procedure NewImageSize()
  
  width$ = InputRequester("M.i.ED", "Enter Image Width", "")
  height$ = InputRequester("M.i.ED", "Enter Image Height", "")
  
EndProcedure
Last edited by Distorted Pixel on Wed Nov 23, 2022 11:38 am, edited 1 time in total.
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
idle
Always Here
Always Here
Posts: 5049
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to use Integers with MessageRequester

Post by idle »

I would build a window really and use gadgets , though this should be ok

Code: Select all

EnableExplicit 

Procedure IsNumeric(strin.s) 
  Protected *char.Unicode = @strin 
  Protected result 
  While *char\u <> 0 
    If *char\u = 46 Or (*char\u >=48 And *char\u <=57) 
     result = 1 
    Else  
      result = 0
      Break 
   EndIf
   *char+2 
  Wend
 ProcedureReturn result  
EndProcedure

Procedure NewImageSize()
  Protected width,height,dims.s,temp.s 
  dims = InputRequester("M.i.ED", "Enter Image Width*Height", "800*600")
  If CountString(dims,"*") = 1 
    temp = StringField(dims,1,"*")
    If IsNumeric(temp) 
      width = Val(temp)
    EndIf
    temp = StringField(dims,2,"*")
    If IsNumeric(temp) 
      height = Val(temp)
    EndIf   
  EndIf   
  
  If (width And height) 
    Debug Str(width) + " " + Str(height) 
  Else 
    Debug "Please enter numeric only"  
  EndIf   
   
EndProcedure

NewImageSize()
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: How to use Integers with MessageRequester

Post by juergenkulow »

Code: Select all

; #PB_String_Numeric StringGadget Flag
Structure PicSizeType : weith.i : height.i : EndStructure

Procedure NewImageSize(*PicSize.PicSizeType)
  If OpenWindow(0, 0, 0, 322, 150, "Picture Size", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget  (0, 8,  10, 306, 20, "Enter Image Width")
    StringGadget(1, 8,  30, 306, 20, Str(*PicSize\weith), #PB_String_Numeric)
    TextGadget  (2, 8,  60, 306, 20, "Enter Image Height")
    StringGadget(3, 8,  80, 306, 20, Str(*PicSize\height), #PB_String_Numeric)
    ButtonGadget(4, 8, 110, 306, 20, "Ready") 
    SetActiveGadget(1)
    Repeat
      event = WaitWindowEvent()
    Until #PB_Event_CloseWindow=event Or (#PB_Event_Gadget=event And EventGadget()=4)
    *PicSize\weith=Val(GetGadgetText(1))
    *PicSize\height=Val(GetGadgetText(3))
  EndIf
EndProcedure

PicSize.PicSizeType\weith=1920
PicSize\height=1080 
NewImageSize(@PicSize)
Debug PicSize\weith
Debug PicSize\height
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

Re: How to use Integers with MessageRequester

Post by Axolotl »

Code: Select all

Procedure NewImageSize(*Width.INTEGER, *Height.INTEGER) 
  Protected text$, sep$ = "x" 

; maybe this is not needed 
; If *Width\i <> 0 And *Height\i <> 0 
;   text$ = Str(*Width\i) + " " + sep$ + " " + Str(*Height\i) 
; EndIf 

  text$ = InputRequester("M.i.ED", "Enter Image Width " + sep$ + " Height ", text$) 
  If text$ <> "" 
    *Width\i = Val(StringField(text$, 1, sep$)) 
    *Height\i = Val(StringField(text$, 2, sep$)) 
    ProcedureReturn #True  
  EndIf     
  ProcedureReturn #False 
EndProcedure

CompilerIf #PB_Compiler_IsMainFile 
  Global W = 400, H = 200 

  While NewImageSize(@W, @H) 
    Debug "Result => " + W + " x " + H 
  Wend 

CompilerEndIf 
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: How to use Integers with MessageRequester

Post by Distorted Pixel »

@idle, juergenkulow and Axolotl

Hi again,

Thank you all for your examples, they all work and are great.
juergenkulow's example was the closest to what I was looking for and I may use it. But at some point I do want to combine both InputRequesters into one input and I may combine juergenkulow's into one.

idle's and Axolotl's if I remember right already do that and I may choose to use one of theirs, but for now I spent some time after I got home from work to see if I could figure out myself how to use my original idea with 2 InputRequesters and I was able to get some simple code to work for now which I have posted below. While I was at work I researched about converting a numerical string into an integer and came across the Val() command. So I worked at it hard and came up with as I said already the code below for now until I want to combine things into one. I will edit the below code to do even more of what I want eventually.

Thank you all for your help

Code: Select all

Global W.i, H.i

 If OpenWindow(0, 0, 0, 1024, 768, "M.i.ED", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   
    Width$ = InputRequester("M.i.ED", "Enter New Image Width", "")
  
    Height$ = InputRequester("M.i.ED", "Enter New Image Height$", "")
    
    W = Val(Width$)
    H = Val(Height$)  
    Debug W
    Debug H
    
    CreateImage(1, W, H, 32) 
    StartDrawing(ImageOutput(1))
    
    DrawImage(ImageID(1), 0, 0, W, H)
    Box(0, 0, W, H)
    FillArea(0, 0, -1, #White)
    StopDrawing()
    ImageGadget(1, 0, 0, W, H, ImageID(1), #PB_Image_Border)
    
    
    Repeat
      
      event = WaitWindowEvent()
      
    Until #PB_Event_CloseWindow=event
  EndIf
  
  
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
idle
Always Here
Always Here
Posts: 5049
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to use Integers with MessageRequester

Post by idle »

a spin gadget might be more appropriate, it will still let a user type in letter but they will evaluate to 0

Code: Select all

Structure PicSizeType
  width.i
  height.i
EndStructure

Procedure NewImageSize(*PicSize.PicSizeType)
  Protected width,height 
  width = 32000  ;set max width and height 
  height = 32000 
    
  If OpenWindow(0, 0, 0, 240, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SpinGadget(0, 20, 20, 100, 25, 0, width,#PB_Spin_Numeric )
    SetGadgetState (0, 800) : SetGadgetText(0, "800")   ; set initial value
    SpinGadget(1, 120, 20, 100, 25, 0, height,#PB_Spin_Numeric  ) 
    SetGadgetState (1, 600) : SetGadgetText(1, "600")   ; set initial value
    Repeat
      Event = WaitWindowEvent()
      If Event = #PB_Event_Gadget
        Evt = EventType() 
        Select EventGadget()
            Case 0 
              If evt <>  #PB_EventType_Change
                SetGadgetText(0, Str(GetGadgetState(0)))
              EndIf   
            Case 1   
               If evt <>  #PB_EventType_Change
                 SetGadgetText(1, Str(GetGadgetState(1)))
               EndIf   
        EndSelect 
      EndIf
    Until Event = #PB_Event_CloseWindow
  EndIf
  
  *PicSize\width=Val(GetGadgetText(0))
  *PicSize\height=Val(GetGadgetText(1))
  
EndProcedure

Global PicSize.PicSizeType
NewImageSize(@PicSize) 

Debug Str(PicSize\width) + " " + Str(picsize\height) 

User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: How to use Integers with MessageRequester

Post by Distorted Pixel »

idle wrote: Wed Nov 23, 2022 5:33 am a spin gadget might be more appropriate, it will still let a user type in letter but they will evaluate to 0
I just tried it and I actually like it a lot, thank you idle. I might just use this. It's going to be hard to choose between this and juergenkulow's now if I decide against mine.

I'll go ahead and implement it and see. I'll post back
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: How to use Integers with InputRequester

Post by ChrisR »

If you are interested in the spin gadget, for Windows you can add the #ES_NUMBER Style to allow only digits to be entered into the edit control.
I don't know why #ES_NUMBER is not applied by default for the spin gadget with #PB_Spin_Numeric like for the string gadget with #PB_String_Numeric!

Code: Select all

Structure PicSizeType
  Width.i
  Height.i
EndStructure

Procedure NewImageSize(*PicSize.PicSizeType)
  If OpenWindow(0, 0, 0, 220, 80, "Resolution", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SpinGadget(0, 10, 10, 100, 25, 0, 32000, #PB_Spin_Numeric)
    SetGadgetState (0, *PicSize\Width)
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) | #ES_NUMBER)
    CompilerEndIf
    SpinGadget(1, 110, 10, 100, 25, 0, 32000, #PB_Spin_Numeric)
    SetGadgetState (1, *PicSize\Height)
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      SetWindowLongPtr_(GadgetID(1), #GWL_STYLE, GetWindowLongPtr_(GadgetID(1), #GWL_STYLE) | #ES_NUMBER)
    CompilerEndIf
    ButtonGadget(2, 10, 45, 200, 25, "Apply")
    Repeat
      Event = WaitWindowEvent()
      If Event = #PB_Event_Gadget And EventGadget() = 2
        *PicSize\Width  = GetGadgetState(0)
        *PicSize\Height = GetGadgetState(1)
        Break
      EndIf
    Until Event = #PB_Event_CloseWindow
  EndIf
EndProcedure

Global PicSize.PicSizeType
PicSize\Width  = 800
PicSize\Height = 600
NewImageSize(@PicSize) 

Debug Str(PicSize\Width) + " x " + Str(PicSize\Height)
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: How to use Integers with InputRequester

Post by Distorted Pixel »

ChrisR wrote: Thu Nov 24, 2022 2:10 am If you are interested in the spin gadget, for Windows you can add the #ES_NUMBER Style to allow only digits to be entered into the edit control.
I don't know why #ES_NUMBER is not applied by default for the spin gadget with #PB_Spin_Numeric like for the string gadget with #PB_String_Numeric!

Code: Select all

Structure PicSizeType
  Width.i
  Height.i
EndStructure

Procedure NewImageSize(*PicSize.PicSizeType)
  If OpenWindow(0, 0, 0, 220, 80, "Resolution", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SpinGadget(0, 10, 10, 100, 25, 0, 32000, #PB_Spin_Numeric)
    SetGadgetState (0, *PicSize\Width)
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      SetWindowLongPtr_(GadgetID(0), #GWL_STYLE, GetWindowLongPtr_(GadgetID(0), #GWL_STYLE) | #ES_NUMBER)
    CompilerEndIf
    SpinGadget(1, 110, 10, 100, 25, 0, 32000, #PB_Spin_Numeric)
    SetGadgetState (1, *PicSize\Height)
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      SetWindowLongPtr_(GadgetID(1), #GWL_STYLE, GetWindowLongPtr_(GadgetID(1), #GWL_STYLE) | #ES_NUMBER)
    CompilerEndIf
    ButtonGadget(2, 10, 45, 200, 25, "Apply")
    Repeat
      Event = WaitWindowEvent()
      If Event = #PB_Event_Gadget And EventGadget() = 2
        *PicSize\Width  = GetGadgetState(0)
        *PicSize\Height = GetGadgetState(1)
        Break
      EndIf
    Until Event = #PB_Event_CloseWindow
  EndIf
EndProcedure

Global PicSize.PicSizeType
PicSize\Width  = 800
PicSize\Height = 600
NewImageSize(@PicSize) 

Debug Str(PicSize\Width) + " x " + Str(PicSize\Height)
@ChrisR
Since it is after midnight here, lol I'll have time later today to look at this code
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
Post Reply