DateTimePicker - Disable user from using arrow keys?

  • Thread starter Thread starter Artie
  • Start date Start date
A

Artie

Hi,

I want to force the user to only use the drop down and calendar to
select a date in the DatetimePicker control.

I do NOT want them to select the control, then use the up and down
arrow keys to change the date.

How can I prevent them using the arrow keys?

Any help much appreciated.

Cheers

Artie
 
You can try to handle the key down event and discard any keys you
want. Look at this code. Create a form and add datetimepicker control.
HTH.

Alcides Schulz.(http://alsql.blogspot.com).



public partial class TestDatePicker : Form
{
public TestDatePicker()
{
InitializeComponent();
dateTimePicker1.KeyDown += new
KeyEventHandler(event_KeyDown);
}

private void event_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
Console.WriteLine("KeyDown=" + e.KeyCode.ToString());
if (e.KeyCode.ToString() == "Down" || e.KeyCode.ToString()
== "Up")
{
e.SuppressKeyPress = true;
return;
}

}
}
 
private void dateTimePicker1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{

if (e.KeyCode.ToString() == "Down" || e.KeyCode.ToString() == "Up")
{
e.Cancel;
}

}
 
You can try to handle the key down event and discard any keys you
want. Look at this code. Create a form and add datetimepicker control.
HTH.

Alcides Schulz.(http://alsql.blogspot.com).

public partial class TestDatePicker : Form
{
public TestDatePicker()
{
InitializeComponent();
dateTimePicker1.KeyDown += new
KeyEventHandler(event_KeyDown);
}

private void event_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
Console.WriteLine("KeyDown=" + e.KeyCode.ToString());
if (e.KeyCode.ToString() == "Down" || e.KeyCode.ToString()
== "Up")
{
e.SuppressKeyPress = true;
return;
}

}
}

Thanks for your response Alcides.

I'd got one of the Event Handlers, but not the other.

So now I'm implementing the KeyDown and KeyPress event handlers and
doing "e.Handled = true" in each one.

Thanks again.

Artie
 

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

Back
Top