Forum > General

onkeydown event question

(1/7) > >>

I tried this a year ago and never got through it so I'll ask again to see if anyone can point me in the right direction.

I'm using a keydown event for a form that I want to send to a general routine in an included program.  I'm trying to keep all of my functions in a file called, surprisingly, Functions.pas  so all I have to do is add 'Functions' to the uses clause and I have the commands I need.

I can use the following on the main form program to figure out what control just had focus

if form1.activecontrol = form1.ControlByName('Controlname') then .....

This works as expected so that if I trap the tab key I can then direct the code to do something based on which tedit it's in.

While looking stepping through the code I can see that the 'Sender' in the FormKeyDown appears to be the form itself so I tried to send that to the function and then use it as the basis for the same if statement but just trying

Checkfield(Sender); in the main program and then

Function Checksender(Sender:Tobject);
if Sender.activecontrol = Sender.ControlByName('Controlname') then .....

Gets me an error saying something to the effect of there isn't an property or function like that in Sender.

If I try tform(Sender).activecontrol .......

I don't get the error but I also don't seem to get the active control so the if statement fails.

What am I doing wrong????? 

Am I using the Sender object incorrectly??

I just had a thought which I'll try out while waiting to see if someone can answer, I'm going to try to get the active control and send it with the tobject and see if I can work with that, not as neat but maybe it will work.

 %) %) %) %) %) %) %) %) %)

Now I'm really confused.

I was playing around and decided to try to copy the TObject into a form variable so I made a form variable and the used

form1 := tform(Sender);

in the function program.

Worked great, then just to be thorough I wrote the same statement using tform(sender). and that worked this time  :o

I'm sure I did something wrong but I really hate it when I don't know WHAT  :(

Maybe you should do a safer cast. Something like:

--- Code: ---if Sender is TForm then begin
   with Sender as TForm do begin
      activecontrol .......
if Sender is TEdit then begin
   with Sender as TEdit do begin

--- End code ---

if you put a code example of your code it would be easyer to undertand whats wrong in the code...

Right now there is no code, I keep changing it, and now I'm back to the Tobhect not working. 

If the sender tobject is a form and you send that as a tobject to another furnction and then use tform(tobject) to force it into a tform object will you have access to the names of the controls on the form directly or will you have to access them indirectly??  What I mean is if I have a form that the key down event is triggered and I send the tobject/tform to a function can I just use the format tform(tobject).controlname where controlname is the name of the control on the form or will I have to do something else.  I think I'm doing something wrong in how I'm trying to access the tobject and thats why it seems to work sometimes then stops. 

Maybe this will be a bit clearer on what I'm stuck on.  If I have Form1 with PageControl1, Tabsheet1 with one TEdit1 on it I can access the Tedit1 on the Form1 code page using Form1.Tedit1.   If I send Form1 from the Form1 code page to a function on another code page as Sender:Tobject I can access the Form1 by using tform(Sender).   but I do not see the controls like I do after I add the dot like I do on the Form1 Page.  Are these available and I have to access them some other way or can't I get to them at all???


[0] Message Index

[#] Next page

Go to full version