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

You need some new stunning features ? Tell us here.
PBExplorer12
User
User
Posts: 59
Joined: Sun Jul 21, 2013 8:30 am

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

Post 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?
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

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

Post by Polo »

PBExplorer12
User
User
Posts: 59
Joined: Sun Jul 21, 2013 8:30 am

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

Post by PBExplorer12 »

Yep, Davido pointed this out, it's starting to make sense now, thanks Polo.
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

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

Post 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. :)
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
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

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

Post by Polo »

Very well explained, thanks for taking the time doing that!
PBExplorer12
User
User
Posts: 59
Joined: Sun Jul 21, 2013 8:30 am

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

Post 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.
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

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

Post 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
PBExplorer12
User
User
Posts: 59
Joined: Sun Jul 21, 2013 8:30 am

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

Post 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.
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

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

Post 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. :)
Post Reply