Maybe this helps :
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void MyButton_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == anarrowkey)
{
// Stop the character from being entered into the control.
e.Handled = true;
}
}
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
You can only handle Keydowns etc. for a control who has the focus.
Seems logical : if you have 2 textboxes 1 will have the focus so it can accept key input.
When you open an empty form it will have the focus, but as soon as you put lets say a button on it, the button will have the focus. You can set the focus with the Focus() method. Hope this helps a bit.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
It is a mere coincidence that I happen to arrive in the same situation as you. My calculator has a form, a textbox and some flowlayout panels I use to lay out my custom(round) calculator buttons. Clicking with the mouse works fine, except I am now trying to add some keyboard functionality just as you are trying to do. Still working on it for the moment, we'll keep in touch!
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
I don't program for money, It's not my job, I'm not in school, I don't intend to rule the world
I see we have even other things in common!
May I point your attention to this snippet, I intend to use it in some way in my calculator : http://www.daniweb.com/code/snippet1094.html
I don't code on a daily basis and I'm working on different projects so this might take a while. I don't know, I have no deadlines:cool:
Let you know of my progress so I can show you code how I did it. Might give you some ideas.
Until then, happy coding!
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
If you're not catching a key in the KeyDown event then it is probably a command key, and you need to catch it another way. Try this:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData == Keys.Right) || (keyData == Keys.Left) ||
(keyData == Keys.Up) || (keyData == Keys.Down))
{
//Do custom stuff
//true if key was processed by control, false otherwise
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
I don't think that you can just drop a user control on a form and have it take control of the keys like that. I cannot think of any instance of this being done by third party software or microsoft... but it may be possible. One thing you could do is descend from a : Form and write the event passing code, then use that as your form base. As to find the parent form for a control you can keep crawling up .Parent until you find a form:
private void simpleButton4_Click(object sender, EventArgs e)
{
Control ctrl = (sender as Control);
while (!(ctrl is Form))
{
Console.WriteLine(ctrl.GetType().Name);
ctrl = ctrl.Parent;
}
Form f = (ctrl as Form);
MessageBox.Show(f.Text);
}
Here is my output:
SimpleButton
PanelControl
XtraTabPage
XtraTabControl
Then you see a message box with the form's caption
Be cautious on crawling the parent of controls. The .NET framework uses delayed initialization and the control is not completely created until the _Load event is fired. So if your control is on the 2nd tab page and you run code against the control you may run in to problems. In places where I do this I iterate all the tab pages and force them to show, then show the first tab page again all in the form load event. This forces all of the form controls to be created when the form loads.
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
With user controls visual studio actually runs the user control's load event. So in this case the UC is trying to find a parent form, which cannot be found. Use a modification of my code that should solve the problem:
Control ctrl = (sender as Control);
while (!(ctrl is Form))
{
if (ctrl.Parent != null)
ctrl = ctrl.Parent;
else
break;
}
f = (ctrl as Form);
if (f != null) //when in design time it won't find the form
{
f.KeyPreview = true;
f.KeyDown += new KeyEventHandler(this.KeyHandler);
}
I don't have a compiler in front of me so it may have a syntax error....
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735