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:
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.
