Panel and KeyDown event problem

  • Thread starter Marko Bozikovic
  • Start date

M

Marko Bozikovic

Hi all,

(I'm reposting this, original title was misleading)

I'm new to .NET, and I have a silly little problem. My application's layout is
simple: a window with one splitter with a treeview control in the left pane
and a Panel for rendering a DirectX scene in the right pane.

My problem is the KeyDown event. Before adding splitter and treeview,
everything worked fine - rendering panel received key events (I'm interested
in up/down/left/right/pageup/pagedown)

However, after I have added splitter and the treeview, my rendering panel
doesn't receive KeyDown events for up/down/left/right (they seem to be routed
to TreeView controls (render panel still receives pageup/pagedown events)

I'm aware of the focus problems with Panels, and I set main form's
ActiveControl to my render panel in panel's MouseDown event handler (actually,
this seems to set *splitter's* ActiveControl property, but it seems to be
working). The treeview control loses focus (as expected) and my render panel
handles pageup/pagedown keys. But, if I press one of up/down/left/right keys,
the treeview steals focus and responds to the key event.

I have even added a handler for PreviewKeyDown event in the rendering panel,
and it gets called for all keypresses, but I still don't know how to prevent
treeview from stealing focus.

Any ideas? (I'm using .NET 2.0)

Thank you,
--
Marko
ICQ: 5990814

I'm not under the alkafluence of inkahol
that some thinkle peep I am.
It's just the drunker I sit here the longer I get.
 
Ad

Advertisements

C

ClayB

Try overriding the form's ProcessDialogKey and catching the keystroke
there.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = this.panel1;
this.panel1.KeyDown += new KeyEventHandler(panel1_KeyDown);
}

void panel1_KeyDown(object sender, KeyEventArgs e)
{
ProcessKey(e.KeyCode);
}

public void ProcessKey(Keys keyCode)
{
this.Text = "handle " + keyCode.ToString() + " on panel";
}

protected override bool ProcessDialogKey(Keys keyData)
{
Keys keyCode = keyData & Keys.KeyCode;
if (keyCode == Keys.Down || keyCode == Keys.Up
|| keyCode == Keys.Left || keyCode == Keys.Right)
{
ProcessKey(keyCode);
return true;
}
return base.ProcessDialogKey(keyData);
}
}

============
Clay Burch
Syncfusion, Inc.
 
Ad

Advertisements

M

Marko Bozikovic

ClayB said:
Try overriding the form's ProcessDialogKey and catching the keystroke
there.
-snip-

Thanks for the tip, but I think I have found a simpler solution. It seems that
it's enough to handle panel's PreviewKey event and set
PreviewKeyDownEventArgs.IsInputKey property to true for key's we're interested in.
--
Marko
ICQ: 5990814

I'm not under the alkafluence of inkahol
that some thinkle peep I am.
It's just the drunker I sit here the longer I get.
 

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