Simple Message box method?
Simple Message box method?
I have tried to create a simple Hello world with Message box.
I posted this on another topic so sorry about that first of all, it was kind of unrelated to that topic and could not delete it.
Anyway via the form designer, I have tried to add the button_0 / buttongadget onto the form, then in a seperate .pb file where I have put an include bit of code at the top to include aforementioned .pbf file / form I created via the form designer, I am struggling to figure out the code to achieve the overall goal of when the use clicks the button on the form it displays a message such as "Hello World!" or something like that. I tried a few things and the only thing I managed to achieve was making the message box display as soon as the window activated / compiled.
Overall could someone please point me in the right direction or post a quick simple code on how to do this considering I want to use the form designer in my code as well.
Thanks!
I posted this on another topic so sorry about that first of all, it was kind of unrelated to that topic and could not delete it.
Anyway via the form designer, I have tried to add the button_0 / buttongadget onto the form, then in a seperate .pb file where I have put an include bit of code at the top to include aforementioned .pbf file / form I created via the form designer, I am struggling to figure out the code to achieve the overall goal of when the use clicks the button on the form it displays a message such as "Hello World!" or something like that. I tried a few things and the only thing I managed to achieve was making the message box display as soon as the window activated / compiled.
Overall could someone please point me in the right direction or post a quick simple code on how to do this considering I want to use the form designer in my code as well.
Thanks!
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Simple Message box method?
I know it seems like the form designer would be the quickest way to get productive if you're new to Purebasic but I don't believe that's the case. Your project needs four elements:
1) open a window
2) create a button for the window
3) create a handler for the button
4) create a message loop to process window messages until the window is closed
Here is all the code you need, it should be easier to understand than if a form designer wrote it for you:
The first two lines are fairly self-explanatory.
The BindGadgetEvent line tells the message loop to watch for a message meant for gadget# 0, the button, and call the specified procedure when it gets one. It keeps processing messages until it finds a "close window" message and then the program ends.
1) open a window
2) create a button for the window
3) create a handler for the button
4) create a message loop to process window messages until the window is closed
Here is all the code you need, it should be easier to understand than if a form designer wrote it for you:
Code: Select all
Procedure btnProc()
MessageRequester("", "Hello World!")
EndProcedure
OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ButtonGadget(0,270,230,100,20,"Say Hello")
BindGadgetEvent(0, @btnProc())
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
The BindGadgetEvent line tells the message loop to watch for a message meant for gadget# 0, the button, and call the specified procedure when it gets one. It keeps processing messages until it finds a "close window" message and then the program ends.
BERESHEIT
- VB6_to_PBx
- Enthusiast
- Posts: 627
- Joined: Mon May 09, 2011 9:36 am
Re: Simple Message box method?
modified netmaestro Code with InputRequester and SetWindowTitle
you can enter Text in InputRequester and see it displayed in Window's TitleBar
2nd example :
you can enter Text in InputRequester and see it displayed in Window's TitleBar
Code: Select all
; MessageRequester_and_InputRequester_with_BindGadgetEvent_example__v1.pb
; http://www.purebasic.fr/english/viewtopic.php?f=13&t=59310
Procedure btnProc()
MessageRequester("", "Hello World!")
Input$ = InputRequester("Title", "Please make your input:", "I'm the default input.")
SetWindowTitle(0, Input$)
EndProcedure
OpenWindow(0,0,0,640,480,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ButtonGadget(0,270,230,160,25,"Say Hello and Input something")
BindGadgetEvent(0, @btnProc())
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
2nd example :
Code: Select all
; MessageRequester_and_InputRequester_example__v1.pb
; http://www.purebasic.fr/english/viewtopic.php?f=13&t=59304&start=15
Enumeration FormWindow
#Window_0
EndEnumeration
Enumeration FormGadget
#Button_0
#Button_1
EndEnumeration
Enumeration FormFont
#Font_Window_0_0
EndEnumeration
LoadFont(#Font_Window_0_0,"Verdana", 10)
Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
OpenWindow(#Window_0, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget |
#PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
ButtonGadget(#Button_0, 160, 137, 281, 56, "Click to Popup MessageRequester")
SetGadgetFont(#Button_0, FontID(#Font_Window_0_0))
ButtonGadget(#Button_1, 160, 200, 281, 56, "Click to Popup InputRequester")
SetGadgetFont(#Button_1, FontID(#Font_Window_0_0))
EndProcedure
Procedure Window_0_Events(event)
Select event
Case #PB_Event_CloseWindow
ProcedureReturn #False
Case #PB_Event_Gadget
Select EventGadget()
Case #Button_0 : MessageRequester("Information", "Hello World", #PB_MessageRequester_Ok)
Case #Button_1 : Input$ = InputRequester("Title", "Please make your input:", "I'm the default input.")
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
OpenWindow_0()
Repeat : event = WaitWindowEvent() : Until Window_0_Events(event) = #False
End
PureBasic .... making tiny electrons do what you want !
"With every mistake we must surely be learning" - George Harrison
Re: Simple Message box method?
Hi SomeT. These steps should get you there (blue text = menus, red text = variables, green text = field names):SomeT wrote:...via the form designer, I have tried to add the button_0 / buttongadget onto the form, then in a seperate .pb file where I have put an include bit of code at the top to include aforementioned .pbf file / form I created via the form designer, I am struggling to figure out the code to achieve the overall goal of when the use clicks the button on the form it displays a message such as "Hello World!" or something like that...
...how to do this considering I want to use the form designer in my code as well.
1. Open a new form by selecting New Form in the Form menu of the PureBasic IDE.
2. You'll see a standard form window displayed, with its properties listed in the bottom right panel. Make sure that the Generate events procedure option is selected.
3. From the gadget toolbox, which should be located on the top right panel, look for the ButtonGadget(), drag it over to the form window on your left, and drop it at the desired position.
* Before the next step, make sure that the newly created button is selected. If is it selected, eight small square drag points should appear around it, and the Variable field in the properties window in the bottom right panel should display Button_0. If it is not, just click on the button once to select it.
4. The properties window in the bottom right panel will now display the properties of this newly created button. Type in the desired text for this button in the Caption field, and press Enter to apply. In this example, just enter Say Hello.
5. Further down the properties list, in the Event procedure field, enter a name for the procedure to handle this button's events. It can be any valid PureBasic procedure name, but for this example, enter onButtonClick, and press Enter to apply.
6. Now, again from the Form menu, select Switch Code/Design View, and you should see this listing:
myForm.pbf:
Code: Select all
Global Window_0
Global Button_0
Declare onButtonClick(EventType)
Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
Button_0 = ButtonGadget(#PB_Any, 230, 160, 100, 25, "Say Hello")
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
onButtonClick(EventType())
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
8. Next, open a new code tab (press Ctrl-N or select New from the File menu), and type in the following code:
myForm.pb:
Code: Select all
IncludeFile "myForm.pbf"
Procedure onButtonClick(event)
MessageRequester("Form Example", "Hello World!")
EndProcedure
OpenWindow_0()
Repeat
event = WaitWindowEvent()
result = Window_0_Events(event)
Until result = #False
10. That's all!
Hope it works well!

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: Simple Message box method?
TA994A I get the following error on the .pb part of your code on...
...
It comes up with the error message when I try to compile the .pb file:
Line 3: Declare doesn't match with a real Procedure.
I followed your insturctions exactly, the .pb files does not compile due to above error and the .pbf does compile but does not work.
...
Code: Select all
Line 3: Procedure onButtonClick(event)
Line 3: Declare doesn't match with a real Procedure.
I followed your insturctions exactly, the .pb files does not compile due to above error and the .pbf does compile but does not work.
Re: Simple Message box method?
Hello SomeT. That error message means that the parameters in the brackets of the procedure are not the same as what was declared in myForm.pbf. Please make sure that the declaration in myForm.pbf looks like this:SomeT wrote:It comes up with the error message when I try to compile the .pb file:
Line 3: Declare doesn't match with a real Procedure.
I followed your insturctions exactly, the .pb files does not compile due to above error and the .pbf does compile but does not work.
in myForm.pbf:
Code: Select all
Declare onButtonClick(EventType)
in myForm.pb:
Code: Select all
Procedure onButtonClick(event)

Alternatively, if you wouldn't mind, please cut & paste & post the two codes, so we can take a look.

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: Simple Message box method?
Your code works! Sorry, I was compiling from the form and not the code hence it was not working correctly when I did this.
Now although I can't get it to work when I compile from the form designer still, its ok because the code I have for it is linked and allows me to still change the form aswell as the code along side it which is exactly what I wanted, thanks!
Now although I can't get it to work when I compile from the form designer still, its ok because the code I have for it is linked and allows me to still change the form aswell as the code along side it which is exactly what I wanted, thanks!
Re: Simple Message box method?
Actually new problem! As soon as I change any aspect of the form in form designer it creates dual windows?! For example it opens two windows rather than one which both do the same thing? What code can I put in to stop this from happening?
Actually scrap that I figured it out,
For that piece of code I have taken it out of the .pb file now the window closes on the first attempt and another one does not open.
Therefore I would really appericiate it if someone could explain to me the following:
- What does that part of the code do exactly above? Is it meant to close the window?
- Do I need this bit of code in there, is it necessary?
- In future if making more complex programs would I have to put a piece of code like this in, in order to shut down a program correctly?
Actually scrap that I figured it out,
Code: Select all
OpenWindow_0()
Repeat
event = WaitWindowEvent()
result = Window_0_Events(event)
Until result = #False
Therefore I would really appericiate it if someone could explain to me the following:
- What does that part of the code do exactly above? Is it meant to close the window?
- Do I need this bit of code in there, is it necessary?
- In future if making more complex programs would I have to put a piece of code like this in, in order to shut down a program correctly?
Re: Simple Message box method?
Hello again SomeT. That portion, known as the message processing loop or events loop, is a very important and integral part of any program.SomeT wrote:...explain to me the following:- What does that part of the code do exactly above? Is it meant to close the window?Code: Select all
OpenWindow_0() Repeat event = WaitWindowEvent() result = Window_0_Events(event) Until result = #False
- Do I need this bit of code in there, is it necessary?
- In future if making more complex programs would I have to put a piece of code like this in, in order to shut down a program correctly?
In order to understand that, we'd have to take a quick look at how operating systems work. Most major OSes, including Windows, Linux and OSX, are event-based, which means that they operate through sending and receiving messages to and from the programs that run on them. For example, when a program opens a window, it sends a bunch of messages to the OS to tell it what type of window it wants to open; the size, position, title, color, etc. The OS in turn responds with another bunch of messages, confirming that it has received the program's messages, and ultimately giving the program the details of the newly created window. Thereafter, the exchange of messages between the OS and the program will continue, which has to be processed and executed. And this is exactly what the message processing loop does.
These messages are also referred to as events, because essentially, almost all messages are raised by some event, like the click of a button on a window, the movement or resizing of a window, or keyboard input from the user. But these events are not captured by the program itself, but rather by the OS, which will then dispatch them to the program for processing.
To illustrate this, imagine a user clicking on the button in your Hello World! program. The button click is actually simply a mouse click; the OS captures the exact location of that click, determines if there is any window or gadget at the point of that click, and then sends the click event to the program that owns the window or gadget (a gadget is technically also just a window, but that's another story). Then, it is up to the program to process and handle that event; in the case of your Hello World! example, it displays a message box.
Clearly oversimplified, but in a nutshell, that's it.
Here's how PureBasic does it:
Code: Select all
window = OpenWindow(#PB_Any, 100, 100, 200, 200, "Window Title")
;PureBasic sends a request to create a window at position 100,100
;with a size of 200,200 and title it "Window Title".
;The OS sends a success or failure response into the variable "window".
;If successfully created, the variable will contain the handle (identifier)
;to the newly created window, otherwise it will be zero (failed to create).
button = ButtonGadget(#PB_Any, 50, 80, 100, 30, "Click Me")
;PureBasic sends a request to create a gadget (actually a window) at position 50,80
;inside the previously created window, with a size of 100,30 and title it "Click Me".
;The OS sends a success or failure response into the variable "button", as before.
Repeat ;continuously run these set of instructions
event = WaitWindowEvent()
;listen for messages from the OS and take the current message and place it
;into the variable named "event" - then start evaluating the current message.
If event = #PB_Event_CloseWindow
;if the message is to close the window, then end the program.
End
ElseIf event = #PB_Event_Gadget
;if the message belongs to a gadget (child window), check which one.
gadget = EventGadget()
;re-process the current message to get the handle for the gadget (child window)
If gadget = button
;if the message is for the gadget with the handle "button", display this message box
MessageRequester("PureBasic", "Hello World!")
EndIf
EndIf
ForEver ;continue listening for messages
Sorry if the reply verges on pedantic, but I just wanted to be clear.

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: Simple Message box method?
Ok that makes sense what you have said.
The thing is now i stuck because if i leave that code in my program it creates two windows rather than one, if i take it out it works perfectly, i guess my question is how dp i leave this important bit of code in without it creating two window instances?
The thing is now i stuck because if i leave that code in my program it creates two windows rather than one, if i take it out it works perfectly, i guess my question is how dp i leave this important bit of code in without it creating two window instances?
Re: Simple Message box method?
That shouldn't be the case. Could you please cut & paste the code from the two files for us to have a look?SomeT wrote:...if i leave that code in my program it creates two windows rather than one, if i take it out it works perfectly...
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: Simple Message box method?
I think I figured it out myself, is this correct:
for myForm.pb I added to the end of your previously posted code, I believe this was the error, however I don't like to correct you because you are more knowledgable, haha. Let me know if this makes sense:
I just added and now it does not create two windows, I believed it was looping until I put this at the end hence creating more than one window. I am very happy I figured that out myself.
for myForm.pb I added
Code: Select all
End
Code: Select all
IncludeFile "myForm.pbf"
Procedure onButtonClick(event)
MessageRequester("Form Example", "Hello World!")
EndProcedure
OpenWindow_0()
Repeat
event = WaitWindowEvent()
result = Window_0_Events(event)
Until result = #False
End
Code: Select all
End
Re: Simple Message box method?
Actually it don't work, here is my code its still displaying two windows as soon as I change something in the design mode of the form:
myForm.pbf:
myForm.pb:
Overall problem being it always creates two windows when I compile and execute, have tried not creating the exe as unicode, restarted compiler, the program, using latest version of purebasic.
I am going on holiday until sunday so may take until next Monday to reply now.
Please help though! Thanks!
myForm.pbf:
Code: Select all
;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;
Enumeration FormWindow
#Window_0
EndEnumeration
Enumeration FormGadget
#Button_0
EndEnumeration
Declare onButtonClick(EventType)
Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
OpenWindow(#Window_0, x, y, width, height, "", #PB_Window_SystemMenu)
ButtonGadget(#Button_0, 0, 0, 600, 400, "CLICK HERE!")
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
onButtonClick(EventType())
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
OpenWindow_0()
Repeat
event = WaitWindowEvent()
Until Window_0_Events(event) = #False
End
Code: Select all
IncludeFile "myForm.pbf"
Procedure onButtonClick(event)
MessageRequester("Form Example", "Hello World!")
EndProcedure
OpenWindow_0()
Repeat
event = WaitWindowEvent()
result = Window_0_Events(event)
Until result = #False
End
I am going on holiday until sunday so may take until next Monday to reply now.
Please help though! Thanks!
Re: Simple Message box method?
Hi SomeT. The last block of code in the myForm.pbf is causing the problem. PureBasic's Form Designer would not automatically generate that code, so, how did it get there?SomeT wrote:myForm.pbf:myForm.pb:Code: Select all
; ; This code is automatically generated by the FormDesigner. ; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled. ; Event procedures needs to be put in another source file. ; Enumeration FormWindow #Window_0 EndEnumeration Enumeration FormGadget #Button_0 EndEnumeration Declare onButtonClick(EventType) Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400) OpenWindow(#Window_0, x, y, width, height, "", #PB_Window_SystemMenu) ButtonGadget(#Button_0, 0, 0, 600, 400, "CLICK HERE!") 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 onButtonClick(EventType()) EndSelect EndSelect ProcedureReturn #True EndProcedure ;############################################## ;how did this code get here? ;remove this block and you're good to go! OpenWindow_0() Repeat event = WaitWindowEvent() Until Window_0_Events(event) = #False End ;##############################################
Overall problem being it always creates two windows when I compile and execute...Code: Select all
IncludeFile "myForm.pbf" Procedure onButtonClick(event) MessageRequester("Form Example", "Hello World!") EndProcedure ;############################################## ;same code found in myForm.pbf ;keep this block and remove the one from myform.pbf OpenWindow_0() Repeat event = WaitWindowEvent() result = Window_0_Events(event) Until result = #False End ;##############################################
You're getting two windows because there are two calls to open the window (OpenWindow_0); one from within the myForm.pbf itself, and another from myForm.pb. Plus it'll be running two separate event loops; also a problem.
Just remove that last block from myForm.pbf and the program should run normally.

Also, based on some of your previous comments, it should be noted that execution should not start from within form files; they usually contain only procedures which should be called from external code, like myForm.pb. Always run the .pb file, and not the .pbf file.
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: Simple Message box method?
Hi,
Sorry for taking so long to reply just been on holiday.
So I removed that code from the .pbf file, it works perfectly now 100% however, there is a new problem, whenever I change anything on the form using the form designer it adds that code back into the pbf. file, I remove it and save and it works fine again,
Why is it doing this???? Is it a program bug and do I need to report it do you reckon, or is it something programming related?
Sorry for taking so long to reply just been on holiday.
So I removed that code from the .pbf file, it works perfectly now 100% however, there is a new problem, whenever I change anything on the form using the form designer it adds that code back into the pbf. file, I remove it and save and it works fine again,
Why is it doing this???? Is it a program bug and do I need to report it do you reckon, or is it something programming related?