Functions

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Functions

Postby AlBro » Tue Jun 02, 2015 11:57 am

Here goes - normally within every program we have objects such as Buttons/menus etc and when we work on the onclick event handler we can address other objects on the particular form and within the scope of the fuction created. What I want to know is - is there a function I can access / generate without clicking on a button or menu etc which allows me to access and alter and access the other objects / button captions edit boxes etc within the confines of the form. I have used an alternative method for now but I would like to know if its allowed / possible and the correct way of creating / generating the function. Ive looked at a lot of functions but none of the documentation seems to cover this question - or maybe Im just not taking it in properly. The purpose of doing such is so that where say half a dozen or so event handler functions have the same code accessing the same say 20, 30 40 lines of code whereas I would like just to have the small sub function which accesses and sets the majority of the items and just call it as and when needed.
AlBro
 
Posts: 4
Joined: Mon Apr 13, 2015 9:00 am

Re: Functions

Postby smd » Tue Jun 02, 2015 7:01 pm

If you want to periodically update stuff without waiting for the user to click something, put a TTimer component on your form. Set the interval to however often you want to update. then set the event to do whatever it is you want to do.

If you just want to eliminate a bunch of redundant code. Just make a function. As it is C++, you need to throw the TMyForm:: in front of the name (or whatever that module is called), and add the name to the private: or public: section of the header for that form. Now you can simply call that function as needed. Used just like any other C type function.

To call that function from a different file, you just throw the form name in front of it like

int TMyForm::MyFunction(int x); /* or declared however you need it like any other C function */
{
....
}

void TMyForm::aDifferentFunction(void)
{
...
MyFunction(3); /* just use it like any other function */
...
}


in the header for the form it is decalared within, private: or public: section (use public if you want to access it from other files

int MyFunction(int x);

To call it from within another file. Just add an #include for the header of the form it is declared to the file you want to use the function in.

...
MyForm->MyFunction(x); /* used just like any other function */
....
-----------------------------
Scott
smd
BCBJ Guru
BCBJ Guru
 
Posts: 130
Joined: Sat Nov 29, 2014 8:02 pm
Location: Las Vegas


Return to Technical

Who is online

Users browsing this forum: No registered users and 11 guests

cron