OnKeyDown event doesn't response

L

Leo

I am writing a customized control to draw a ellipse. I want it to
response key events and mouse events. However only mouse events got
fired. Key events doesn't response. Any idea? Thanks very much in
advance.

Jingcheng

public class GraphNode : Control
{

protected override void OnPaint(PaintEventArgs e)
{
using (Pen blackPen = new Pen(Color.Black, 1))
{
using( Brush blueBrush = new SolidBrush(Color.LightSkyBlue),
blackBrush = new SolidBrush(this.ForeColor))
{
string test = "this is a test";

SizeF stringSize = e.Graphics.MeasureString(test, this.Font);
float xText = (width - stringSize.Width) / 2.0F;
float yText = height / 2.0F - 5.0F;

e.Graphics.DrawEllipse(blackPen, 5, 5, width, height);
e.Graphics.FillEllipse(blueBrush, 5, 5, width, height);
e.Graphics.DrawString(test, this.Font, blackBrush, xText, yText);
}
}

base.OnPaint(e);
}

protected override void OnMouseDown(MouseEventArgs e)
{
...
}

protected override void OnKeyDown(KeyEventArgs e)
{
int i = 0;
if (e.KeyCode == Keys.Delete)
{
this.Dispose();
}
}
 
G

Guest

I believe overridden event delegates should always call the base delegate before exiting, e.g.

protected override void OnKeyDown(KeyEventArgs e

// Your code her
. . . . .

base.OnKeyDown(e)


If you don't, results will be unpredictable
 
L

Leo

Crater said:
I believe overridden event delegates should always call the base delegate before exiting, e.g.:

protected override void OnKeyDown(KeyEventArgs e)
{
// Your code here
. . . . .

base.OnKeyDown(e);
}

If you don't, results will be unpredictable.

Yes you are asboluately right that I missed the base delegate. But the
problem is that the OnKeyDown delegate is never fired. Any further
idea? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top