Could some kinda coder please try out my code and test it

Windows specific forum
nrasool
User
User
Posts: 28
Joined: Tue Apr 26, 2005 9:55 am

Could some kinda coder please try out my code and test it

Post by nrasool »

Hi there, Please could someone copy and paste the following code into Purebasic 4, run it and test it please. The problem is that, if you run the code, select option Test 1, and select "Test Button" it works for the first time, as you will get a debug. But if you select Test 2 and select "Test Button", then it does work, but you also get the messagerequester box, and I'm not sure why. The messagerequester is only suppose to appear, if you haven't selected any option, and selected "Test Button"

Here is the code:

Code: Select all

;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #Radio_0
  #Radio_1
  #Radio_2
  #Frame3D_0
  #Button_0
  #Button_1
EndEnumeration

;- StatusBar Constants
;
Enumeration
  #StatusBar_0
EndEnumeration



Procedure Open_Window_0()
  If OpenWindow(#Window_0, 258, 212, 277, 162, "Test Application",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
    If CreateStatusBar(#StatusBar_0, WindowID(#Window_0))
      EndIf
      If CreateGadgetList(WindowID(#Window_0))
        OptionGadget(#Radio_0, 16, 32, 120, 30, "Test 1")
        GadgetToolTip(#Radio_0, "Check Test 1")
        OptionGadget(#Radio_1, 16, 60, 120, 30, "Test 2")
        GadgetToolTip(#Radio_1, "Check Test 2")
        OptionGadget(#Radio_2, 16, 92, 120, 30, "Test 3")
        GadgetToolTip(#Radio_2, "Check Test 3")
        ButtonGadget(#Button_0, 184, 20, 76, 28, "Test Button")             
      EndIf
    EndIf
EndProcedure

Open_Window_0()

Repeat ; Start of the event loop
  
  Event = WaitWindowEvent() ; This line waits until an event is received from Windows
  
  WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
  
  GadgetID = EventGadget() ; Is it a gadget event?
  
  EventType = EventType() ; The event type
  
  ;You can place code here, and use the result as parameters for the procedures
  
  If Event = #PB_Event_Gadget
  

   
    If GetGadgetState(#Radio_0)=1 And GadgetID=#Button_0 : Debug "Test 1 - Success" : EndIf
    

    If GetGadgetState(#Radio_1)=1 And GadgetID=#Button_0 : Debug "Test 2 - Success" : EndIf
    

    If GetGadgetState(#Radio_2)=1 And GadgetID=#Button_0 : Debug "Test 3 - Success" : EndIf
    
    If GadgetID = #Button_0 And GetGadgetState(#Radio_0)=0 Or GetGadgetState(#Radio_1)=0 Or GetGadgetState(#Radio_2)=0 : MessageRequester("Error","Nothin has been selected",#PB_MessageRequester_Ok) : EndIf
   
       
  EndIf
  
Until Event = #PB_Event_CloseWindow ; End of the event loop

End
Many thanks in advance who can help out :)
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

You have a logic problem

Try like this

Code: Select all

;- Window Constants 
Enumeration 
  #Window_0 
EndEnumeration 

;- Gadget Constants 
Enumeration 
  #Radio_0 
  #Radio_1 
  #Radio_2 
  #Frame3D_0 
  #Button_0 
  #Button_1 
EndEnumeration 

;- StatusBar Constants 
Enumeration 
  #StatusBar_0 
EndEnumeration 

Procedure Open_Window_0() 
  If OpenWindow(#Window_0, 258, 212, 277, 162, "Test Application",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered ) 
    If CreateStatusBar(#StatusBar_0, WindowID(#Window_0)) 
      EndIf 
      If CreateGadgetList(WindowID(#Window_0)) 
        OptionGadget(#Radio_0, 16, 32, 120, 30, "Test 1") 
        OptionGadget(#Radio_1, 16, 60, 120, 30, "Test 2") 
        OptionGadget(#Radio_2, 16, 92, 120, 30, "Test 3") 
        SetGadgetState(#radio_0, 1)   ; set first option as active one
        ; Now an option is always set - later check for no option selected is unnecessary
        ; Clicking a different option always unchecks the others in the option group defined
        ; until you define another type gadget.
        GadgetToolTip(#Radio_0, "Check Test 1") 
        GadgetToolTip(#Radio_1, "Check Test 2") 
        GadgetToolTip(#Radio_2, "Check Test 3") 
        ButtonGadget(#Button_0, 184, 20, 76, 28, "Test Button")              
      EndIf 
    EndIf 
EndProcedure 

Open_Window_0() 

Repeat ; Start of the event loop 
  Event = WaitWindowEvent() ; This line waits until an event is received from Windows 
  WindowID = EventWindow()  ; The Window where the event is generated, can be used in the gadget procedures 
  GadgetID = EventGadget()  ; Is it a gadget event? 
  EventType = EventType()   ; The event type 
  
  ;You can place code here, and use the result as parameters for the procedures 
  If Event = #PB_Event_Gadget 
    Select GadgetID
      Case #Button_0  ; Button was clicked, now check the option gadgets' status
        If GetGadgetState(#Radio_0) 
          MessageRequester("Debug", "Test 1 - Success",0) 
        ElseIf GetGadgetState(#Radio_1)
          MessageRequester("Debug", "Test 2 - Success",0)
        ElseIf GetGadgetState(#Radio_2)
          MessageRequester("Debug", "Test 3 - Success",0)
        Else
          ; This isn't really necessary, see setting the options comment above.
          ; One of the options will always be set.
          MessageRequester("Error","Nothing has been selected",#PB_MessageRequester_Ok)
        EndIf
    EndSelect  
  EndIf 
Until Event = #PB_Event_CloseWindow ; End of the event loop 
End 

User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

The only line that actually needs rewriting is this one

Code: Select all

If GadgetID = #Button_0 And GetGadgetState(#Radio_0)=0 Or GetGadgetState(#Radio_1)=0 Or GetGadgetState(#Radio_2)=0 : MessageRequester("Error","Nothin has been selected",#PB_MessageRequester_Ok) : EndIf
You say to show the message if Radio 0 or Radio 1 or Radio 2 is 0.

You only want to show the message if Radio 0 and Radio 1 and Radio 2 are all 0.
nrasool
User
User
Posts: 28
Joined: Tue Apr 26, 2005 9:55 am

Post by nrasool »

Hey GedB (You a fan of head over heels? The avatar you have is the speccy version of that game :) ), Thanks for that, and also thanks Terry, that way is more elegant as well. Should have done that in the beginning :)

Many thanks for both of your help
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

nrasool,

Yep. I also like the Dalek element of it.
Post Reply