为什么 DateTimePicker BackColor 禁用手动输入?
Why DateTimePicker BackColor Disable manual typing?
我重写 OnPaint 方法是为了将颜色放入 DateTimePicker 控件的文本框中,它禁用了在文本框中手动输入?
你有解决这个问题的想法吗?
public class BCDateTimePicker : DateTimePicker
{
public BCDateTimePicker()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 20, 0, 20, 20);
Brush bkgBrush;
ComboBoxState visualState;
if (this.Enabled)
{
bkgBrush = new SolidBrush(this.BackColor);
visualState = ComboBoxState.Normal;
}
else
{
bkgBrush = new SolidBrush(this.BackColor);
visualState = ComboBoxState.Disabled;
}
g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);
ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);
g.Dispose();
bkgBrush.Dispose();
}
[Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
}
我提供有关 "Manual typing" 的更多详细信息:
当您按下 Tab 键并继续 DateTimePicker 时。然后您可以使用键盘输入新日期。
像这样:
键盘输入未被禁用,突出显示功能被禁用,因为您的 OnPaint
实施过于简单。最初我们有:
然后单击控件以获得焦点并键入,比方说,“07/04/1776”(重要:包括反斜杠),我们得到:
最后,选择下拉按钮,只是为了确认:
这是代码:
public class BCDateTimePicker : DateTimePicker
{
public BCDateTimePicker()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 20, 0, 20, 20);
Brush bkgBrush;
ComboBoxState visualState;
if (this.Enabled)
{
bkgBrush = new SolidBrush(this.BackColor);
visualState = ComboBoxState.Normal;
}
else
{
bkgBrush = new SolidBrush(this.BackColor);
visualState = ComboBoxState.Disabled;
}
g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);
ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);
g.Dispose();
bkgBrush.Dispose();
}
[Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
}
Form
包含一个常规 DateTimePicker
和一个 BCDateTimePicker
,背景为绿色(通过 VS Designer 设置)。
所以,它按预期工作。文本框甚至会随着日期的输入而动态更新。
编辑 1:此 GIF 太大无法上传到 SO:
编辑 2:关于 ControlStyles.UserPaint - MSDN
的注释
If true, the control paints itself rather than the operating system doing so. If false, the Paint event is not raised. This style only applies to classes derived from Control.
请注意,BCDateTimePicker
失去了文本框编辑突出显示功能。那是因为您对 OnPaint
的实现比操作系统所做的要简单得多。但是键盘输入没有被禁用并且仍然可以使用。
我重写 OnPaint 方法是为了将颜色放入 DateTimePicker 控件的文本框中,它禁用了在文本框中手动输入?
你有解决这个问题的想法吗?
public class BCDateTimePicker : DateTimePicker
{
public BCDateTimePicker()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 20, 0, 20, 20);
Brush bkgBrush;
ComboBoxState visualState;
if (this.Enabled)
{
bkgBrush = new SolidBrush(this.BackColor);
visualState = ComboBoxState.Normal;
}
else
{
bkgBrush = new SolidBrush(this.BackColor);
visualState = ComboBoxState.Disabled;
}
g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);
ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);
g.Dispose();
bkgBrush.Dispose();
}
[Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
}
我提供有关 "Manual typing" 的更多详细信息: 当您按下 Tab 键并继续 DateTimePicker 时。然后您可以使用键盘输入新日期。
像这样:
键盘输入未被禁用,突出显示功能被禁用,因为您的 OnPaint
实施过于简单。最初我们有:
然后单击控件以获得焦点并键入,比方说,“07/04/1776”(重要:包括反斜杠),我们得到:
最后,选择下拉按钮,只是为了确认:
这是代码:
public class BCDateTimePicker : DateTimePicker
{
public BCDateTimePicker()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 20, 0, 20, 20);
Brush bkgBrush;
ComboBoxState visualState;
if (this.Enabled)
{
bkgBrush = new SolidBrush(this.BackColor);
visualState = ComboBoxState.Normal;
}
else
{
bkgBrush = new SolidBrush(this.BackColor);
visualState = ComboBoxState.Disabled;
}
g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);
ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);
g.Dispose();
bkgBrush.Dispose();
}
[Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
}
Form
包含一个常规 DateTimePicker
和一个 BCDateTimePicker
,背景为绿色(通过 VS Designer 设置)。
所以,它按预期工作。文本框甚至会随着日期的输入而动态更新。
编辑 1:此 GIF 太大无法上传到 SO:
编辑 2:关于 ControlStyles.UserPaint - MSDN
的注释If true, the control paints itself rather than the operating system doing so. If false, the Paint event is not raised. This style only applies to classes derived from Control.
请注意,BCDateTimePicker
失去了文本框编辑突出显示功能。那是因为您对 OnPaint
的实现比操作系统所做的要简单得多。但是键盘输入没有被禁用并且仍然可以使用。