Using button on a form

Just starting out? Need help? Post your questions and find answers here.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using button on a form

Post by Danilo »

ptaylor2014 wrote:

Code: Select all

Declare EventButtonOK(EventType)
[...]

Code: Select all

Procedure EventButtonOK(EventGadget, EventType)
  MessageRequester("Form", "OK has been clicked on.")
    Quit = 1
EndProcedure
This shouldn't compile. Declaration and Implementation do not match.
The code looks incomplete. What is variable 'Quit'? The event loop is also missing.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using button on a form

Post by Danilo »

ptaylor2014 wrote:The EventButtonOk matches the name in the procedure and in the main code. How do I fix it?
The procedure parameters do not match. Remove 1st parameter 'EventGadget'.

Code: Select all

Global Window_0
Global Quit
Global EventButtonOK

Declare EventButtonOK(EventType)

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
    Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_ScreenCentered)
    EventButtonOK = ButtonGadget(#PB_Any, 220, 110, 140, 30, "", #PB_Button_Toggle)
EndProcedure

Procedure Window_0_Events(event)
    Select event
        Case #PB_Event_CloseWindow
            ProcedureReturn #False
            
        Case #PB_Event_Menu
            Select EventMenu()
            EndSelect
            
        Case #PB_Event_Gadget
            Select EventGadget()
                Case EventButtonOK
                    EventButtonOK(EventType()) 
            EndSelect
    EndSelect
    ProcedureReturn #True
EndProcedure

OpenWindow_0(100,100)

Repeat
    event = WaitWindowEvent()
    Select EventWindow()
        Case Window_0 : Window_0_Events(event)
    EndSelect
Until Quit


;This is the Events.pb file

Procedure EventButtonOK(EventType)
    MessageRequester("Form", "OK has been clicked on.")
    Quit = 1
EndProcedure
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using button on a form

Post by Danilo »

Sorry, can't help you with the Form Designer.

Image Image
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Using button on a form

Post by TI-994A »

ptaylor2014 wrote:I created a project using the the current version of Purebasic and it includes the button EventButtonOK. I added the procedure EventButtonOK in Events.pb and for the EventProcedure I put EventButtonOK but when I click on the button is not executing the event.
Hi ptaylor2014, and welcome to the PureBasic forum.
ptaylor2014 wrote:After I removed any lines in the main pdf file and save it & execute the code it adds it back to the main code. Why and how can I fix that issue?
The code that is generated and managed by the Form Designer is usually not changeable, and any manual code changes would be expunged when the form is edited, or when the .pbf file is saved and re-opened with the Form Designer. To ensure that changes are not expunged, the .pbf file could be saved as a standard .pb file. However, once saved as a standard .pb file, the form would no longer be visually editable by the Form Designer. The good news is, the file could still be re-saved as a .pbf file, and consequently opened and edited with the Form Designer. But, as before, all manual code changes will be expunged.

As such, all additional code should be saved in external modules, and the code structures should conform to the Form Designer's auto-generated code. Based on your code example, placing a single ButtonGadget() on the form would produce the following code:

Code: Select all

Global Window_0

Global EventButtonOK

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EventButtonOK = ButtonGadget(#PB_Any, 220, 110, 140, 30, "", #PB_Button_Toggle)
EndProcedure
The above code only opens a window and places a button on it. However, since the code is wrapped within a procedure, it would not run unless called. Back to that in a minute. Also, the #PB_Window_SystemMenu property would be selected by default, to enable the close-window button in the titlebar. Without this, a separate close-window method must be coded, so it's better to select it while testing.

Now, an events loop is required to process the various events of the form. To do this, simply tick the [Generate events procedure] option in the properties window in the right panel of the Form Designer. If you don't see this option, make sure that the window object is selected by clicking on any portion of the window object in the visual editor on the left.

Once that is done, the name of the procedure that would handle the events of the button must be specified. To do this, first select the button object by clicking on it in the visual editor, to display its properties in the right panel. Then, in the [Event procedure] field, enter a procedure name of your choice. Here, it's simply named Button_Procedure, and it's also been given the caption, Click Me. After that, the resulting code should look like this:

Code: Select all

Global Window_0

Global EventButtonOK

Declare Button_Procedure(EventType)

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EventButtonOK = ButtonGadget(#PB_Any, 220, 110, 140, 30, "Click Me", #PB_Button_Toggle)
EndProcedure

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

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
        Case EventButtonOK
          Button_Procedure(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
At this point, the code still does absolutely nothing. Although running the code from the Form Designer would display the window with the button, that is just a feature of the Form Designer to enable us to see what the resulting window would look like when it is ultimately executed.

The code is only comprised of two procedures, OpenWindow_0() and Window_0_Events(), which as mentioned earlier, would not run unless called. Furthermore, it is still not complete. The procedure that was assigned to the button, Button_Procedure(), has not been coded. This has to be coded in a separate module, as it is not possible to save any additional code to this form file.

Before proceeding, save this form file. For this example. it's saved as MyForm.pbf. Once saved, open a new code page. The event procedure for the button will be coded here, along with the main executable section of the project that would be calling the various procedures into action. The first line would be to include the recently saved form file, like so:

Code: Select all

IncludeFile "MyForm.pbf"
Next, for the event code. In this example, clicking the button would simply result in a message box popping-up with a message. The code should then look like this:

Code: Select all

IncludeFile  "MyForm.pbf" 

Procedure Button_Procedure(EventType)
  MessageRequester("Button Example", "Hello, ptaylor2014!")
EndProcedure
Again, at this point, the code still does absolutely nothing. So, time to add some executable code. All that is required is a call to the OpenWindow_0() procedure to initialise the window, and then to the Window_0_Events() procedure to process the various events that occur. Here's what it should look like:

Code: Select all

IncludeFile  "MyForm.pbf" 

Procedure Button_Procedure(EventType)
  MessageRequester("Button Example", "Hello, ptaylor2014!")
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = False
Now, save this code in the same folder as MyForm.pbf - the file name is not important. And that's all!

Run this code (not the form file) and it should work.

In case you might be interested, there's a video tutorial on an older version of the Form Designer; although the overall structure has changed quite a bit, it should still provide a good overview of its functionalities:

The PureBasic 5.0 Form Designer - A Quick Tutorial

Hope it helps. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using button on a form

Post by Danilo »

ptaylor2014 wrote:
Danilo wrote:Sorry, can't help you with the Form Designer.

Image Image

It's missing some information.
Right to the point. :D

You see the font problems. Black text on black background, text-height bigger
than a property grid cell. Ignoring code changes - your Code changes get lost.

To sum it up, the Form Designer is unusable. It is quite an example of really bad software.

Beside that, it is uglier than any Win95 program I've ever seen. And know what,
the PB guys seem to be proud of that piece of code. :shock:
But, hey, the PB developers are even proud of the Win95-style drawing and printing capabilities included in PB. :lol:

The PB IDE is programmed using PB itself, and it shows that PB may be not the best choice for cross-platform development.
Bugs in the IDE just get ignored for years...
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Using button on a form

Post by TI-994A »

Danilo wrote:...PB may be not the best choice for cross-platform development.
Hi Danilo. Honest question: in its class, what do you consider better? :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using button on a form

Post by Danilo »

TI-994A wrote:
Danilo wrote:...PB may be not the best choice for cross-platform development.
Hi Danilo. Honest question: in its class, what do you consider better? :)
To be honest, PB is the best I know, within the lowest quality and lowest price class.
Better tools are always hundreds or thousands of $$$.

Personally, I consider Qt one of the best cross-platform toolkits available, if not the best.

When it comes to supported different platforms (like Raspberry Pi), others may take the race:

Monkey:
- Raspberry Pi
- Anyone for Pi?

BlitzMax:
- BMX-NG (BlitzMax OpenSource Compiler that emits to C/C++, by Brucey)
- OpenSource Monkey Compiler
- RaspberryPi 2, quad core, 6x performance of B+
- Rasperoids!
- Digesteroids On Steroids
- Raspberry Jam [Release 0.18] - (completely unofficial) Raspberry Pi edition of BlitzMax

Qt is available everywhere. Raspberry Pi, ODROID, Intel NUC, Android, iOS, ... just everywhere.
Last edited by Danilo on Sun Feb 22, 2015 9:08 am, edited 2 times in total.
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Using button on a form

Post by TI-994A »

Danilo wrote:To be honest, PB is the best I know, within the lowest quality and lowest price class.
Better tools are always hundreds or thousands of $$$.
Unique perspective. :wink:

Personally, it's strengths far outweigh any shortcomings (which usually have workarounds anyway). Purely a godsend. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using button on a form

Post by Danilo »

TI-994A wrote:Personally, it's strengths far outweigh any shortcomings (which usually have workarounds anyway). Purely a godsend. :D
When ignoring the so called "Form Designer" completely, PB is quite OK, I agree. Old-style, but OK.
The Designer is still an unusable piece of something I don't want to mention. You can't change my opinion about this with just smooth words...
Good idea, very bad implementation.
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Using button on a form

Post by TI-994A »

Danilo wrote:When ignoring the so called "Form Designer" completely, PB is quite OK, I agree. Old-style, but OK.
Favouring a more integrated coding style, the FD's form-only limitation is definitely not quite preferable. However, it's great for rapid prototyping; simply drag&drop the required layouts, and extract the generated form code without implementing the form file.

Real time-saver! :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using button on a form

Post by Danilo »

TI-994A wrote:Real time-saver! :)
Congratulations! I can do much faster by hand coding the gadget positions out of my mind. ;)

An useable Form Designer would be even faster, I agree. See VisualBasic 15 yeas ago... it was better than PB Form Designer in 2015, in my humble opinion.
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Using button on a form

Post by TI-994A »

Danilo wrote:I can do much faster by hand coding the gadget positions out of my mind.
Perhaps, but definitely not for aesthetics and revisions. In the FD, you'd be able to see a mock-up, and how it'd all look in the final layout; fonts, colours, sizes, etc. And rearranging/resizing gadgets is a cinch - just a few quick mouse clicks and moves, and you're done. That would require some serious hand-coding to realise. :wink:

For me, that's a boon. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using button on a form

Post by Danilo »

TI-994A wrote:Perhaps, but definitely not for aesthetics and revisions. In the FD, you'd be able to see a mock-up, and how it'd all look in the final layout; fonts, colours, sizes, etc. And rearranging/resizing gadgets is a cinch - just a few quick mouse clicks and moves, and you're done. That would require some serious hand-coding to realise. :wink:

For me, that's a boon. :D
Unfortunately It does not help me. I still see half-cut fonts (not DPI-aware) and black text on black background (to be honest, I can't see black text on black background).

Image Image

Nice if it works for you. I think it is quite unusable, and un-acceptable, low-quality stuff.

I gave up reporting bugs ([PB5.21 LTS] IDE Tool "ColorPicker:Wheel" does not update), as nobody cares about it. It's actually wasted life-time...

Do you want to use your very valueable life-time for such a crab? Think about it, at least two times...

- The old world
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Using button on a form

Post by TI-994A »

Danilo wrote:Unfortunately It does not help me. I still see half-cut fonts (not DPI-aware) and black text on black background (to be honest, I can't see black text on black background).
You could be right about it not being dpi-aware, although many mainstream commercial apps aren't either. From your screenshots, it still seems to be readable though. The color picker wheel in the OSX version clearly has a glitch, but ultimately it does the job. As for the the black-on-black fonts, aren't you able to change the font color in the preferences settings?

Nonetheless, it works well for me:

Image

Image


I personally believe that PureBasic will always be a work in progress, which is a good thing, really. When needed, and where possible, I'd simply sidestep the shortcomings, and not let it affect my progress. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
TI-994A
Addict
Addict
Posts: 2744
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Using button on a form

Post by TI-994A »

ptaylor2014 wrote:If I have Myform.pbf forms and I have two buttons the first one is Accounts and I want to to load the form Accounts.pbf when the user clicks on it. The second one is Quit and the form is Quit so when the user clicks on it the program will end? I need to be able to use mix events.
Hello again ptaylor2014. Please allow me to clarify a little further.

You've placed two buttons on the form, but you've mixed up the naming conventions. For the bigger button, you've named the variable and the event procedure Button2, and for the smaller button, you've named them both Button_Procedure.

The Form Designer automatically assigns a variable name for each and every gadget that is created on the form. Although naming them as you have is not strictly incorrect, it should be noted that the [Variable name] refers to the gadget, while the [Event procedure] refers to the procedure that handles the events of the gadget. Improper naming of these elements could lead to confusion, especially during event handling.

To better illustrate the Form Designer's code architecture, here's a step-by-step breakdown which recreates your project. For speed and convenience, you could also just cut & paste the three code blocks below, and save them under their respective names (MyForm.pbf, Accounts.pbf, and MyProject.pb, all in the same folder), and they'd be ready to run.

Properties of the first form, Window_0:
1. Open a new form
2. Caption = Multi-Window Form Designer
3. Make sure that the following settings are ticked:
- Generate Events Procedure
- #PB_Window_SystemMenu
- #PB_Window_ScreenCentered
4. All other settings will remain on default
5. Place two buttons on the form:
a. Button_0 properties:
- Caption = Open Accounts Window
- x/y position: 200, 90
- width/height = 200 x 50
- Event Procedure = Button_0_Procedure
b. Button_1 properties:
- Caption = Q U I T
- x/y position: 200, 190
- width/height = 200 x 50
- Event Procedure = Button_1_Procedure

The auto-generated code of the first form, saved as MyForm.pbf:

Code: Select all

Global Window_0

Global Button_0, Button_1

Declare Button_0_Procedure(EventType)
Declare Button_1_Procedure(EventType)

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Multi-Window Form Designer Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Button_0 = ButtonGadget(#PB_Any, 200, 90, 200, 50, "Open Accounts Window")
  Button_1 = ButtonGadget(#PB_Any, 200, 190, 200, 50, "Q U I T")
EndProcedure

Procedure Window_0_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_0
          Button_0_Procedure(EventType())          
        Case Button_1
          Button_1_Procedure(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
Properties for the second form, Accounts:
1. Open a new form
2. Change the variable name from the default Window_0 to Accounts
3. Caption = Accounts Window
4. Make sure that the following settings are ticked:
- Generate Events Procedure
- #PB_Window_SystemMenu
- #PB_Window_ScreenCentered
5. Reduce the window size, width/height = 300 x 200
6. All other settings will remain on default
7. Place two buttons on the form:
a. Button_0 properties:
- Change the variable name from the default Button_0 to Button_2
- Caption = Do Something
- x/y position: 80, 40
- width/height = 140 x 40
- Event Procedure = Button_2_Procedure
b. Button_1 properties:
- Change the variable name from the default Button_1 to Button_3
- Caption = Q U I T
- x/y position: 80, 100
- width/height = 140 x 40
- Event Procedure = Button_3_Procedure

The auto-generated code of the second form, saved as Accounts.pbf:

Code: Select all

Global Accounts

Global Button_2, Button_3

Declare Button_2_Procedure(EventType)
Declare Button_3_Procedure(EventType)

Procedure OpenAccounts(x = 0, y = 0, width = 300, height = 200)
  Accounts = OpenWindow(#PB_Any, x, y, width, height, "Accounts Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Button_2 = ButtonGadget(#PB_Any, 80, 40, 140, 40, "Do Something")
  Button_3 = ButtonGadget(#PB_Any, 80, 100, 140, 40, "Close this window")
EndProcedure

Procedure Accounts_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_2
          Button_2_Procedure(EventType())          
        Case Button_3
          Button_3_Procedure(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
The main code, saved as MyProject.pb:

Code: Select all

IncludeFile  "MyForm.pbf"
IncludeFile  "Accounts.pbf"

Global appQuit, accountsOpen

Procedure Button_0_Procedure(EventType)
  If Not accountsOpen
    OpenAccounts()
    accountsOpen = 1
  Else
    SetActiveWindow(Accounts)
  EndIf
EndProcedure

Procedure Button_1_Procedure(EventType)
  appQuit = 1
EndProcedure

Procedure Button_2_Procedure(EventType)
  MessageRequester("Accounts Window", "You clicked a button.")
EndProcedure

Procedure Button_3_Procedure(EventType)
  CloseWindow(Accounts)
  accountsOpen = 0  
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
  Select EventWindow()
    Case Window_0
      If Window_0_Events(event) = #False
        appQuit = 1
      EndIf
    Case Accounts
      If Accounts_Events(event) = #False
        CloseWindow(Accounts)
      EndIf
  EndSelect
Until appQuit
Make sure to save all three files, MyForm.pbf, Accounts.pbf and MyProject.pb, in the same folder, and give it a run. That's all.

Hope it works for you. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply