Page 1 of 1

SpinGadget not allow 1 - 65535 [SOLVED]

Posted: Thu Feb 08, 2007 8:08 am
by SkyManager
I try to use a SpinGadget to input a port number which is within the range of 1 - 65535.
However, the SpinGadget does not work properly.
I guess the allowed number is only between 1 - 25000
Can anybody help?
[code]
If OpenWindow(0, 0, 0, 140, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
SpinGadget (0, 20, 20, 100, 25, 1, 65535)
SetGadgetState (0, 65530) : SetGadgetText(0, "65530") ; set initial value
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
If EventGadget() = 0
SetGadgetText(0,Str(GetGadgetState(0)))
EndIf
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
[/code] :oops:

Posted: Thu Feb 08, 2007 9:36 am
by Froggerprogger
It seems that the SpinGadget under Windows does only have a 16 bit-signed word for storing it's current state. Therefore its state has to lay in range -32768 up to 32767 to achieve the maximum of 65536 different values (including zero).

Setting the range for the SpinGadget from 1 to 65535 is handled as from 1 to -32768, so as an invalid range.

So you have to move the range (which is large enough for your problem) so that 32767 => 65535 and then -32767 => 1 and -32768 => 0. This can simply done by addition of 32768 to the current gadgetvalue. Further you don't need the 0, so the range -32767 to 32767 is enough.

See the following example:

Code: Select all

If OpenWindow(0, 0, 0, 140, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  SpinGadget (0, 20, 20, 100, 25, -32767, 32767) ; not -32768 because we don't need the 0 later
  SetGadgetState (0, -32767) : SetGadgetText(0,"1") ; -32767 => 1, -32766 => 2, ..., 0 => 32768, ..., 32767 => 65535
  Repeat 
    Event = WaitWindowEvent() 
    If Event = #PB_Event_Gadget 
      If EventGadget() = 0 
        ; get wport as word, here in [-32767, 32767]
        wport.w = GetGadgetState(0)
        ; now slide to real port-id in [1, 65535]
        port = wport + 32768 
        SetGadgetText(0,Str(port)) 
      EndIf 
    EndIf 
  Until Event = #PB_Event_CloseWindow 
EndIf

Posted: Thu Feb 08, 2007 2:47 pm
by Sparkie
Or if Win API is an option for you, try using UDM_SETRANGE32.

Code: Select all

#UDM_SETRANGE32 = #WM_USER + 111

If OpenWindow(0, 0, 0, 140, 70, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  SpinGadget (0, 20, 20, 100, 25, 1, 65535, #PB_Spin_Numeric)
  hSpin = FindWindowEx_(WindowID(0), GadgetID(0), 0, 0)
  SendMessage_(hSpin, #UDM_SETRANGE32, 1, 65535)
  SetGadgetState (0, 65535)
  SetGadgetText(0, "65535")
  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
EndIf 

Posted: Fri Feb 09, 2007 1:03 am
by SkyManager
Thanks both of you, Froggerprogger and Sparkie.
You are very nice