Ofer Gal

I am building a form for rapid data entry with keyboard only.

the second control is a DateTimePicker and as is it will focus on the month whenever the control gets the focus.

I tryed to SendKey({Left}) in the Enter event, but it is not working right most of the time.

Is there a way to make it first focus on the month part




Re: Windows Forms General DateTimePicker focus on the day part

nobugz

Your question is a bit confusing, you want to focus on the month part when it already does this I'll assume you've got a problem because DTP will re-focus on whatever part had the focus when it last lost the focus. There is no way to control the focus inside the DTP. But you can play a rude trick by destroying and recreating the native DTP window, thus forcing it to forget which part had the focus last.

Add a new class to your project and paste this code:

using System;
using System.Windows.Forms;

public class MyPicker : DateTimePicker {
protected override void OnEnter(EventArgs e) {
this.RecreateHandle();
SendKeys.Send("{right}");
base.OnEnter(e);
}
}

Build your project, then drop MyPicker from the top of the toolbox onto your form. I sent a "Right" key to focus on the day part, change it as needed.





Re: Windows Forms General DateTimePicker focus on the day part

Ofer Gal

You answered the right question!

I tried your code but whenever the OnEnter fires I get a Win32Exception "Error creating window handle."

So the theory is great, it just doesn't work.

If there was a way to tell which part (day month or year) the focus is on when the OnEnter fires, then I could use the right

SendKeys.Send

Thank any way!






Re: Windows Forms General DateTimePicker focus on the day part

nobugz

Sorry, it worked as expected when I tried it out before posting. Trying to guess what might be wrong in your case: are you using .NET 2.0





Re: Windows Forms General DateTimePicker focus on the day part

Ofer Gal

My mistake!

It works fine if there is an event handler method that does something.

BTW I am on .NET 2.0

Got it to do exactly what I wanted! Thank you much!






Re: Windows Forms General DateTimePicker focus on the day part

Ofer Gal

After a while it turns out that whenever a user clicks the new control with the mouse, it fire the Win32Exception and quits the application.

I tried to trap the mouse enter events but it did not help.

Back to squre one

It was a nice try anyway!






Re: Windows Forms General DateTimePicker focus on the day part

Fat Fairy

Hi,

I am trying to get a datetimepicker to load with the day in focus. Do you have any suggestions

Thanks,

j





Re: Windows Forms General DateTimePicker focus on the day part

Ofer Gal

Try nobugz's Sugestion from 01-27-2007, 3:51 PM UTC

I had some issues with it, but it may work for you!






Re: Windows Forms General DateTimePicker focus on the day part

Prathamesh_k

Hi,
Register the LostFocus event as -

private void NoKeyUpDateTimePicker_LostFocus(object sender, EventArgs e)
{
DateTimePickerFormat fmt;
fmt = this.Format;
if(fmt == DateTimePickerFormat.Short)
this.Format = DateTimePickerFormat.Long;
else
this.Format = DateTimePickerFormat.Short;
this.Format = fmt;

}

Hope this is what u want...




Re: Windows Forms General DateTimePicker focus on the day part

fghdfhdfhdhf

Or only like this :

protected override void OnLostFocus(EventArgs e)

{

base.RecreateHandle();

}