Force DataTimePicker to open calendar by F12

A

Andrus

I need that WinForms DateTimePicker opens calendar when F12 key is pressed.
I tried code below but got error

Property or indexer 'System.Windows.Forms.KeyEventArgs.KeyCode' cannot be
assigned to -- it is read only

How to fix ?

Andrus.

public class NullableDateTimePicker : DateTimePicker {

protected override void OnKeyDown(KeyEventArgs e) {
base.OnKeyDown(e);
if (e.KeyCode == Keys.F12)
e.KeyCode = Keys.F4;
}
}
 
C

Cowboy \(Gregory A. Beamer\)

I am not understanding why you would need to set the KeyCode to something
else, if all you want to do is open the DateTimePicker.

If you absolutely need to pass down to base, you can create an EventArg

KeyPressEventArgs ea = new KeyPressEventArgs(KeyChar.F4)
base.OnKeyPress(ea);

But, I would think you can pop the control without intercepting the
keystroke.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
 
A

Andrus

Gregory,
I am not understanding why you would need to set the KeyCode to something
else, if all you want to do is open the DateTimePicker.

They is no method or property which can be used to open calendar so I tried
this as last effort.
If you absolutely need to pass down to base, you can create an EventArg

KeyPressEventArgs ea = new KeyPressEventArgs(KeyChar.F4)
base.OnKeyPress(ea);


I tried

protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.F12)
{
KeyPressEventArgs ea = new KeyPressEventArgs((char)Keys.F4);
base.OnKeyPress(ea);
return;
}

and

protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.F12)
{
KeyPressEventArgs ea = new KeyPressEventArgs((char)Keys.F4);
base.OnKeyPress(ea);
return;
}

but F12 still does not open calendar.
But, I would think you can pop the control without intercepting the
keystroke.

Which method or property I should use for this ?

Andrus.
 

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