Page 1 of 1

How to attach a main form to a project, with code?

Posted: Mon Aug 12, 2013 5:01 pm
by PBExplorer12
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?

Re: How to attach a main form to a project, with code?

Posted: Mon Aug 12, 2013 6:31 pm
by Polo

Re: How to attach a main form to a project, with code?

Posted: Mon Aug 12, 2013 6:47 pm
by PBExplorer12
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?

Posted: Mon Aug 12, 2013 6:48 pm
by TI-994A
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?
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.

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
7. Close the form file, and copy the following code into a new source file:

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
8. Save this file as firstForm.pb, and make sure that it is saved in the same folder as firstForm.pbf
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. :)

Re: How to attach a main form to a project, with code?

Posted: Mon Aug 12, 2013 7:21 pm
by Polo
Very well explained, thanks for taking the time doing that!

Re: How to attach a main form to a project, with code?

Posted: Mon Aug 12, 2013 7:46 pm
by PBExplorer12
TI-994A wrote:
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?
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.

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
7. Close the form file, and copy the following code into a new source file:

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
8. Save this file as firstForm.pb, and make sure that it is saved in the same folder as firstForm.pbf
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?

Posted: Thu Aug 15, 2013 7:43 am
by Opcode
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?
A little something like this.

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

Re: How to attach a main form to a project, with code?

Posted: Thu Aug 15, 2013 5:19 pm
by PBExplorer12
Thanks Opcode,

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?

Posted: Tue Aug 20, 2013 10:51 am
by Opcode
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.
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. :)