469,889 Members | 1,465 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

keydown event


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

Nov 17 '05 #1
3 1443
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" <Peter S@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...

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


Nov 17 '05 #2
To be pedantic, System::Void (with caps 'V') is correct. The rest of the
answer is spot on though :-)

"Peteroid" <pe************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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" <Peter S@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...

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

Nov 17 '05 #3
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" <sjm AT fixerlabs DOT com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
To be pedantic, System::Void (with caps 'V') is correct. The rest of the
answer is spot on though :-)

"Peteroid" <pe************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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" <Peter S@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...

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


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by bardo | last post: by
4 posts views Thread by Tony | last post: by
4 posts views Thread by Anne | last post: by
4 posts views Thread by ShaneO | last post: by
1 post views Thread by fripper | last post: by
3 posts views Thread by MLM450 | last post: by
3 posts views Thread by win | last post: by
2 posts views Thread by Tony Johansson | last post: by
14 posts views Thread by raylopez99 | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.