By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 2,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

keydown event

P: n/a

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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.