How to attach a main form to a project, with code?
-
- User
- Posts: 59
- Joined: Sun Jul 21, 2013 8:30 am
How to attach a main form to a project, with code?
Hi,
I'm just playing with buttons and edit boxes, and tried to install a simple button which displays a message box.
There's a Project, called Firstform. A form called frmFirstform.pbf. and code called frmFirstFor.pb.
How do you execute the form, and make the code work, when you compile? If click Input source file in Compiler options, it only allows you to select the code, but not the form. Are you supposed to include the form in the code? I've already added the form to the project, and added this code to the .pb module:
Procedure btnTest_Test(EventType)
MessageRequester("Info", "Return key pressed", 0)
EndProcedure
It compiles, but doesn't display the form. I can probably easily figure out how to do the whole thing in code, without the Form Designer, but would prefer not to give up the convenience of letting the Form Designer help with the coordinates.
What's missing?
I'm just playing with buttons and edit boxes, and tried to install a simple button which displays a message box.
There's a Project, called Firstform. A form called frmFirstform.pbf. and code called frmFirstFor.pb.
How do you execute the form, and make the code work, when you compile? If click Input source file in Compiler options, it only allows you to select the code, but not the form. Are you supposed to include the form in the code? I've already added the form to the project, and added this code to the .pb module:
Procedure btnTest_Test(EventType)
MessageRequester("Info", "Return key pressed", 0)
EndProcedure
It compiles, but doesn't display the form. I can probably easily figure out how to do the whole thing in code, without the Form Designer, but would prefer not to give up the convenience of letting the Form Designer help with the coordinates.
What's missing?
-
- User
- Posts: 59
- Joined: Sun Jul 21, 2013 8:30 am
Re: How to attach a main form to a project, with code?
Yep, Davido pointed this out, it's starting to make sense now, thanks Polo.
Re: How to attach a main form to a project, with code?
Hello PBExplorer12. You're right not to give up on Form Designer, especially for the convenience that it lends to layouts. The concept is really quite simple, and I'll try to walk you through the basics.PBExplorer12 wrote:...tried to install a simple button which displays a message box.
...How do you execute the form, and make the code work, when you compile?
1. Launch a new form in Form Designer.
2. In the properties window, give the window the title My First Form.
3. Add a button gadget by double-clicking on the button tool in the toolbox.
4. In the properties window, give the button the caption Show Message.
5. Under the Event procedure field, type button_clicked.
6. Save the form file as firstForm.pbf.
Your form code should look something like this (you could also simply copy & paste this code, and save it as firstForm.pbf):
Code: Select all
Global Window_0
Global Button_0
Declare button_clicked(EventType)
Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
Window_0 = OpenWindow(#PB_Any, x, y, width, height, "My First Form", #PB_Window_SystemMenu)
Button_0 = ButtonGadget(#PB_Any, 10, 10, 100, 25, "Show Message")
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_clicked(EventType())
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
Code: Select all
IncludeFile "firstForm.pbf"
OpenWindow_0()
Procedure button_clicked(eventType)
MessageRequester("Button Clicked:", "Here's your message!")
EndProcedure
While Window_0_Events(WaitWindowEvent()): Wend
9. Run firstForm.pb and your form should work.
Essentially, the Form Designer generates procedures for the form layout and events. The second code file simply calls these procedures into action:
1. IncludeFile "firstForm.pbf" instructs PureBasic to insert firstForm.pbf into firstForm.pb.
2. OpenWindow_0() is a procedure within the form code that initialises a window and sets up its gadgets.
3. The button_clicked() procedure, which we named earlier, handles the button events. Here, a messagebox is popped when the button is clicked.
4. Finally, Window_0_Events(WaitWindowEvent()) continuously calls the events loop until the window is closed.
Not exactly exhaustive, but hope it helps.

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 

Re: How to attach a main form to a project, with code?
Very well explained, thanks for taking the time doing that!
-
- User
- Posts: 59
- Joined: Sun Jul 21, 2013 8:30 am
Re: How to attach a main form to a project, with code?
TI-994A wrote:Hello PBExplorer12. You're right not to give up on Form Designer, especially for the convenience that it lends to layouts. The concept is really quite simple, and I'll try to walk you through the basics.PBExplorer12 wrote:...tried to install a simple button which displays a message box.
...How do you execute the form, and make the code work, when you compile?
1. Launch a new form in Form Designer.
2. In the properties window, give the window the title My First Form.
3. Add a button gadget by double-clicking on the button tool in the toolbox.
4. In the properties window, give the button the caption Show Message.
5. Under the Event procedure field, type button_clicked.
6. Save the form file as firstForm.pbf.
Your form code should look something like this (you could also simply copy & paste this code, and save it as firstForm.pbf):7. Close the form file, and copy the following code into a new source file:Code: Select all
Global Window_0 Global Button_0 Declare button_clicked(EventType) Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400) Window_0 = OpenWindow(#PB_Any, x, y, width, height, "My First Form", #PB_Window_SystemMenu) Button_0 = ButtonGadget(#PB_Any, 10, 10, 100, 25, "Show Message") 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_clicked(EventType()) EndSelect EndSelect ProcedureReturn #True EndProcedure
8. Save this file as firstForm.pb, and make sure that it is saved in the same folder as firstForm.pbfCode: Select all
IncludeFile "firstForm.pbf" OpenWindow_0() Procedure button_clicked(eventType) MessageRequester("Button Clicked:", "Here's your message!") EndProcedure While Window_0_Events(WaitWindowEvent()): Wend
9. Run firstForm.pb and your form should work.
Essentially, the Form Designer generates procedures for the form layout and events. The second code file simply calls these procedures into action:
1. IncludeFile "firstForm.pbf" instructs PureBasic to insert firstForm.pbf into firstForm.pb.
2. OpenWindow_0() is a procedure within the form code that initialises a window and sets up its gadgets.
3. The button_clicked() procedure, which we named earlier, handles the button events. Here, a messagebox is popped when the button is clicked.
4. Finally, Window_0_Events(WaitWindowEvent()) continuously calls the events loop until the window is closed.
Not exactly exhaustive, but hope it helps.
Doesn't have to be exhaustive, just needed to get started. It does help, thanks.
Re: How to attach a main form to a project, with code?
A little something like this.PBExplorer12 wrote:Hi,
I'm just playing with buttons and edit boxes, and tried to install a simple button which displays a message box.
There's a Project, called Firstform. A form called frmFirstform.pbf. and code called frmFirstFor.pb.
How do you execute the form, and make the code work, when you compile? If click Input source file in Compiler options, it only allows you to select the code, but not the form. Are you supposed to include the form in the code? I've already added the form to the project, and added this code to the .pb module:
Procedure btnTest_Test(EventType)
MessageRequester("Info", "Return key pressed", 0)
EndProcedure
It compiles, but doesn't display the form. I can probably easily figure out how to do the whole thing in code, without the Form Designer, but would prefer not to give up the convenience of letting the Form Designer help with the coordinates.
What's missing?
Code: Select all
XIncludeFile "frmFirstform.pbf"
OpenWindow_0()
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case Button_0
MessageRequester("Message Box", "Button 0 Clicked!")
EndSelect
Case #PB_Event_Menu
Select EventMenu()
;Menu Items
EndSelect
Case #PB_Event_CloseWindow
End
EndSelect
ForEver
-
- User
- Posts: 59
- Joined: Sun Jul 21, 2013 8:30 am
Re: How to attach a main form to a project, with code?
Thanks Opcode,
It's all covered, been working great for several days. I'm happy I stayed with it, it's a cool language.
It's all covered, been working great for several days. I'm happy I stayed with it, it's a cool language.
Re: How to attach a main form to a project, with code?
It starts to make more sense as a whole the longer you stick to it. And the more powerful it becomes as well once you know how to utilize it properly.PBExplorer12 wrote:Thanks Opcode,
It's all covered, been working great for several days. I'm happy I stayed with it, it's a cool language.
