PDA

View Full Version : NewB - Calling Function in Button Click Event


plinydogg
10-20-2006, 02:13 AM
Okay, second NewB question: I've got a procedure declaration in my main PPL file before my WinMain() function. It's fairly simple:

proc displayX
SetText(neditX$, x%);
end;

I try to call this function when a button (called "btnEast") on a form is pressed. To get to the place where I think I'm supposed to enter code, I double-clicked on the button and a new window opened with the following code:

func btnEast_OnClick(hWnd$, Msg$, wParam$, lParam$)
HandleEventParms
return (true);
end;

I inserted a call to the displayX function and so the code looked like so:

func btnEast_OnClick(hWnd$, Msg$, wParam$, lParam$)
HandleEventParms
displayX;
return (true);
end;

However, when I run the program and press the button I get a syntax error at position 3,3 (i.e., where the displayX function is called).

What am I doing wrong?

Thank you in advance!

PointOfLight
10-20-2006, 02:32 AM
Did you get the error when you pressed the button, or when you compiled the program? It sounds like the function is being declared after it's being called.

Does your main file look something like this:

#include "name_of_form.ppl"

proc displayX
end;

func WinMain
end;

If so, you should move the proc displayX procedure above the #include statement for the form.

David Chua
10-20-2006, 02:48 AM
You can also try using the "Forward" statement to declare your function/procedure: eg. Forward proc displayX.

It would be probably better you can show us how your whole program looks like, it maybe only arrangement of your code that's giving the problem.

Cheers.

plinydogg
10-20-2006, 02:55 AM
Thanks for your reply POL! I get the error when I compile (before I was just doing run instead of compiling first). And yet, my main file looked basically like you said:

#include "name_of_form.ppl"

proc displayX
code here
end;

func WinMain
end;

I did as you suggested and moved the displayX procedure declaration above the #include statement for the form. I'm still having the same problem, though.

I suspect now that the problem is that I'm initializing the variables in the wrong place.

x% is a global variable (I think it is anyway...that's what I was trying to do when I used "%") and I'm not sure where it belongs. I've tried placing it in the WinMain() function and in various other places.

Any help would be much appreciated. I promise not to ask any more questions tonight (it's almost my bedtime).

plinydogg
10-20-2006, 03:04 AM
POL and David:

I really must be tired. I won't even tell you what I've been doing wrong...all I will say that it wasn't even a newbie mistake....it was an "I'm really tired" mistake.

As far as where you can create and initialize variables, I think the main rule is just that you have to do so inside the WinMain function or the main procedure (or inside a class or something).

Thank you for your time both of you. Also, David, thanks for letting me know about the "Forward" statement. It will be very helpful in the future.

Now, time for bed....zzzzzzzzzzzzzzzz

David Chua
10-20-2006, 03:50 AM
OK, I missed that X% earlier. If I were you I would probably declare X$ (instead of X%) in the winmain as global variable and use it anywhere I want. By the way, why "X%" I've not notice this before! I use % for concatenation of strings. Did I miss something!

Cheers.

PointOfLight
10-20-2006, 08:04 AM
x$ and x% could both be global, as long as they were declared using the global function. The difference is that x$ is global in the scope of the current program only, whereas x% becomes global in the scope of the current execution of PPL. If you're running a program that has been compiled into an exectuable, this is basically the same thing. However, if you're running several .PPL scripts inside the same execution of PPL, they could all share a variable that was decalred using the % sign. Hope that makes sense.

[Edit]

By the way, plinydogg, I don't want to embarass you (and it really shouldn't, anyway), but it would be helpful to anyone reading this thread if you could tell us what fixed your problem.

kornalius
10-20-2006, 11:18 AM
Even though I have a good idea of what you did wrong (edited the .ppl file generated by the PIDE .frm code generator probably) we'd like to know what you did wrong to help other newbies as you like to call yourself. ;)

plinydogg
10-20-2006, 02:24 PM
I'll post here once I get off work...

plinydogg
10-21-2006, 08:19 PM
Sorry for the delay. When I said that I just made a mistake, I really just meant that I made a really stupid mistake.

My original problem was fixed by PointOfLight's advice to move the procedure declaration above the #include statement (I'm still not sure why this was necessary because the function isn't actually called until the button is pressed...I think I'll take David's advice and use forward from now on).

After I did this, the mistake that was preventing the program from working properly is that I mispelled the function call! (See, I told you it was stupid). I was just really tired.

kornalius
10-22-2006, 08:11 PM
The best place to declare functions on a form is in the Form Initialization Section. The function will then be available to the whole form code.

dennishea
12-16-2006, 03:58 PM
The best place to declare functions on a form is in the Form Initialization Section. The function will then be available to the whole form code.



What if your program has 5 forms and 3 of them use the same function. Do you put the one function in all 3 forms inialation section?

Denny

kornalius
12-16-2006, 04:02 PM
You can put them in the first form that is your main project. Or simpler, you put them in a .ppl file and include this file in all the 3 forms using #include "myfuncs.ppl"

dennishea
12-17-2006, 07:59 AM
Thanks Kornalius

Denny