Simple gui - button won't work

Just starting out? Need help? Post your questions and find answers here.
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Simple gui - button won't work

Post by Janni »

Hi,

Button don't do anything
What am I missing here ?

OS: Windows.

gui by form designer

Code: Select all

;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Enumeration FormWindow
  #gui
EndEnumeration

Enumeration FormGadget
  #button
EndEnumeration

Declare button(EventType)

Procedure Opengui(x = 0, y = 0, width = 600, height = 400)
  OpenWindow(#gui, x, y, width, height, "Just a test", #PB_Window_SystemMenu)
  ButtonGadget(#button, 160, 90, 100, 25, "Click me")
EndProcedure

Procedure gui_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
        Case #button
          button(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

code

Code: Select all

XIncludeFile "testgui.pbf"

opengui()

Procedure button(EventType)
  MessageRequester("Information", "Click to close", 0)
EndProcedure  
  
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_CloseWindow 
      Quit = 1
    EndIf
  Until Quit = 1
End   
Thanks in advance :)
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Simple gui - button won't work

Post by BarryG »

WaitWindowEvent() isn't sending any events (such as button clicks) to the gui_Events() procedure.

I'd do it like this:

Code: Select all

Global Quit

Enumeration FormWindow
  #gui
EndEnumeration

Enumeration FormGadget
  #button
EndEnumeration

Declare button(EventType)

Procedure Opengui(x = 0, y = 0, width = 600, height = 400)
  OpenWindow(#gui, x, y, width, height, "Just a test", #PB_Window_SystemMenu)
  ButtonGadget(#button, 160, 90, 100, 25, "Click me")
EndProcedure

Procedure gui_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      Quit = 1
      ProcedureReturn #False
      
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #button
          button(EventType())
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

opengui()

Procedure button(EventType)
  MessageRequester("Information", "Click to close", 0)
EndProcedure  

Repeat
  gui_Events(WaitWindowEvent())
Until Quit = 1
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: Simple gui - button won't work

Post by Janni »

Thanks BarryG :)
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: Simple gui - button won't work

Post by Janni »

What I don't quite understand is that on Linux I can put this in my main file and everything just works.

Code: Select all

Repeat
  event = WaitWindowEvent()
Until gui_Events(event) = #False
End 
While in Windows (Microsoft to be clear), doing the same and no buttons nor closing application works.

I use the form designer so I cannot add anything to the .pbf file, ideally I would like it to work just like on Linux :)
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: Simple gui - button won't work

Post by Janni »

What I can get to work in both Linux and Windows (with the identical code) is to have Form Designer not creating the Event procedure, but rather have the events in my main file with the suggestion from BarryG.

Code: Select all

Procedure gui_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      Quit = #True ; <----This i can't do in the form designer
      ProcedureReturn #False .....

Code: Select all

Repeat
  gui_Events(WaitWindowEvent())
Until Quit = #True
End ;
Guess I can live with that :)
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Simple gui - button won't work

Post by Paul »

Janni wrote: Tue Aug 09, 2022 5:47 pm What I don't quite understand is that on Linux I can put this in my main file and everything just works.

Code: Select all

Repeat
  event = WaitWindowEvent()
Until gui_Events(event) = #False
End 
While in Windows (Microsoft to be clear), doing the same and no buttons nor closing application works.

I use the form designer so I cannot add anything to the .pbf file, ideally I would like it to work just like on Linux :)
Not sure what you're testing on but testing your code here on Windows 10 with PB6, it works as expected.
Pressing the Button brings up the MessageRequester and pressing the close Window button closes the Window.
Image Image
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Simple gui - button won't work

Post by mk-soft »

This is how it should work ...

Code: Select all

Repeat
  event = WaitWindowEvent()
  If gui_Events(event) = #False
    Break
  EndIf
  
  If gui_dialog_Events(event) = #False
    CloseWindow(#gui_dialog)
  EndIf
  
ForEver

End
I also use the FormDesigner from Purebasic. However, I don't like the event creation. So I switch it off and write them myself, or let the EventDesigner (see signature) create all the code with all the events automatically.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Simple gui - button won't work

Post by mk-soft »

P.S.
For small program I don't use the FormDesigner and load a template ...

Example:

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainEdit
  #MainButtonOk
  #MainButtonCancel
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainEdit, 5, 5, dx -10, dy - 45)
  ResizeGadget(#MainButtonok, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
      MenuBar()
    CompilerEndIf
    
    MenuItem(#MainMenuExit, "E&xit")
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    EditorGadget(#MainEdit, 5, 5, dx -10, dy - 45)
    ButtonGadget(#MainButtonok, 10, dy - 35, 120, 30, "Ok")
    ButtonGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30, "Abbruch")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainEdit
              Select EventType()
                Case #PB_EventType_Change
                  ;
                  
              EndSelect
              
            Case #MainButtonOk
              ;
            Case #MainButtonCancel
              ;
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: Simple gui - button won't work

Post by Janni »

Not sure I remember everything I have tried today, I only know that code that works fine on Linux does not in Windows.
But I now have control over both OS'es (I hope :lol: )

Yeah, templates are great :D

Thanks for your inputs Paul, mk-soft.
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
Post Reply