Focus problem with .net 2.0 DateTimePicker with checkbox enabled

M

micklang

When using the datetimepicker with the checkbox visible, before
receiving the focus, the checkbox is colored as if it already has the
focus (even though it does not). The consequent user experience is
that when the user tabs to the datetimepicker control it appears as if
no control on the form has focus, even though the datetimepicker has
received focus (as it does not change appearance). After tabbing off
the datetimepicker the color of the datetimepicker behaves normally
when focus is received and lost.

My question is how can you stop the checkbox in the datetime picker
from appearing like it has focus when the form is first loaded?

My work around at this stage would be to disable the checkbox on the
datetimepicker, add my own checkbox then disable/enable the
datetimepicker when the checkbox is checked (unecessary and messy).

Thanks in advance...

Michael
 
C

ClayB

Here is something you can try. In Form.Load, you can set the form's
ActiveControl property to some control other than your DataTimePicker
(you could set it to the one you want to have input focus when the
form first pops up).

//in Form.Load
this.ActiveControl = this.SomeControlOtherThanMyDateTimePicker1;

===============
Clay Burch
Syncfusion, Inc.
 
M

micklang

I think I need to supply some sample code to illustrate the issue. My
datetimepicker is not the first control on the form. I've tried
playing around with ActiveControl and setting focus to the
DateTimePicker's in the form load.

Form1.cs:-
---------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

Form1.Designer.cs:-
---------------------------------------------------------------------------------------------------------------------------------------------------
namespace DateTimePickerFocusIssue
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.dateTimePicker1 = new
System.Windows.Forms.DateTimePicker();
this.label3 = new System.Windows.Forms.Label();
this.dateTimePicker2 = new
System.Windows.Forms.DateTimePicker();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(55, 13);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(13, 41);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 2;
this.label2.Text = "label2";
//
// dateTimePicker1
//
this.dateTimePicker1.Checked = false;
this.dateTimePicker1.Format =
System.Windows.Forms.DateTimePickerFormat.Short;
this.dateTimePicker1.Location = new
System.Drawing.Point(55, 41);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.ShowCheckBox = true;
this.dateTimePicker1.Size = new System.Drawing.Size(134,
20);
this.dateTimePicker1.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(13, 76);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(35, 13);
this.label3.TabIndex = 4;
this.label3.Text = "label3";
//
// dateTimePicker2
//
this.dateTimePicker2.Checked = false;
this.dateTimePicker2.Format =
System.Windows.Forms.DateTimePickerFormat.Short;
this.dateTimePicker2.Location = new
System.Drawing.Point(55, 72);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.ShowCheckBox = true;
this.dateTimePicker2.Size = new System.Drawing.Size(134,
20);
this.dateTimePicker2.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(458, 390);
this.Controls.Add(this.dateTimePicker2);
this.Controls.Add(this.label3);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.DateTimePicker dateTimePicker2;
}
}
 
M

micklang

I'll answer my own post.

The solution is to set focus to the datetimepickers in a method
handling the form Shown event, and then set the focus back to the
field you want the initial focus on.

In the example i posted that would be:-

privare void Form1_Shown(object sender, EventArgs e)
{
dateTimePicker1.Focus();
dateTimePicker2.Focus();
textBox1.Focus();
}

I can't see the rationale for having the date time picker behave this
way. It seems like a bug hopefully someone from Microsoft will see
this and get it fixed in a service pack.
 

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