keydown event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have function :
System::Void Form1_KeyDown(Object* sender, KeyEventArgs* e)

but this:
this->KeyDown += new KeyEventHandler(this, Form1_KeyDown);

doesnt work..
how I can change it to run?
please help
 
First, I assume you didn't spell 'void' with a capital 'V'. The 2nd
statement you said doesn't work uses the keyword 'this' twice. In the first
case, it is what the KeyDown event handler is being added to. In the second
case, it's where the handler is located. Therefore, your code will only work
if:

(1) Both of your code lines are included in the same class definition.
(2) The class they are both defined in MUST be DERIVED from the Control*
class (based on the handlers name, probably Form*).

The reason for (1) is so that both of the 'this''s refer to the same thing
(which they obviously must), and (2) is because you can only add a KeyDown
handler to a Control* (or a derived class).

If (2) is NOT the case, it must be that some member of the class is a
Control* that should be given the handler, let's call it My_Control (a
pointer to the Control*). Then:

this->My_Control->KeyDown += new KeyEventHandler( this, Form1_KeyDown) ;

should work.

[==Peteroid==]
 
To be pedantic, System::Void (with caps 'V') is correct. The rest of the
answer is spot on though :-)

Peteroid said:
First, I assume you didn't spell 'void' with a capital 'V'. The 2nd
statement you said doesn't work uses the keyword 'this' twice. In the
first case, it is what the KeyDown event handler is being added to. In the
second case, it's where the handler is located. Therefore, your code will
only work if:

(1) Both of your code lines are included in the same class definition.
(2) The class they are both defined in MUST be DERIVED from the Control*
class (based on the handlers name, probably Form*).

The reason for (1) is so that both of the 'this''s refer to the same thing
(which they obviously must), and (2) is because you can only add a KeyDown
handler to a Control* (or a derived class).

If (2) is NOT the case, it must be that some member of the class is a
Control* that should be given the handler, let's call it My_Control (a
pointer to the Control*). Then:

this->My_Control->KeyDown += new KeyEventHandler( this, Form1_KeyDown) ;

should work.

[==Peteroid==]

Peter S said:
I have function :
System::Void Form1_KeyDown(Object* sender, KeyEventArgs* e)

but this:
this->KeyDown += new KeyEventHandler(this, Form1_KeyDown);

doesnt work..
how I can change it to run?
please help
 
Yeah, I wondered about that ('V'oid), and realized later it might have been
correct. I have only ever used 'void' myself, and never knew this other form
(System::Void ) worked as well. But you learn something every day! :)

[==Peteroid==]

Steve McLellan said:
To be pedantic, System::Void (with caps 'V') is correct. The rest of the
answer is spot on though :-)

Peteroid said:
First, I assume you didn't spell 'void' with a capital 'V'. The 2nd
statement you said doesn't work uses the keyword 'this' twice. In the
first case, it is what the KeyDown event handler is being added to. In
the second case, it's where the handler is located. Therefore, your code
will only work if:

(1) Both of your code lines are included in the same class definition.
(2) The class they are both defined in MUST be DERIVED from the Control*
class (based on the handlers name, probably Form*).

The reason for (1) is so that both of the 'this''s refer to the same
thing (which they obviously must), and (2) is because you can only add a
KeyDown handler to a Control* (or a derived class).

If (2) is NOT the case, it must be that some member of the class is a
Control* that should be given the handler, let's call it My_Control (a
pointer to the Control*). Then:

this->My_Control->KeyDown += new KeyEventHandler( this, Form1_KeyDown) ;

should work.

[==Peteroid==]

Peter S said:
I have function :
System::Void Form1_KeyDown(Object* sender, KeyEventArgs* e)

but this:
this->KeyDown += new KeyEventHandler(this, Form1_KeyDown);

doesnt work..
how I can change it to run?
please help
 
Back
Top