gpt4 book ai didi

c# - 为什么 DateTimePicker BackColor 禁用手动输入?

转载 作者:太空狗 更新时间:2023-10-29 23:30:03 27 4
gpt4 key购买 nike

我重写 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; }
}

}

我提供了有关“手动输入”的更多详细信息:当您按下 Tab 键并继续 DateTimePicker 时。然后您可以使用键盘输入新日期。

像那样:

enter image description here

最佳答案

键盘输入没有被禁用,突出显示功能被禁用,因为您的 OnPaint 实现很简单。最初我们有:

enter image description here

然后单击控件以获得焦点并键入,比方说,“07/04/1776”(重要:包括反斜杠),我们得到:

enter image description here

最后,选择下拉按钮,只是为了确认:

enter image description here

这是代码:

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:

See animated GIF here

编辑 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 的实现比操作系统所做的要简单得多。但键盘输入并未被禁用,并且仍在运行。

关于c# - 为什么 DateTimePicker BackColor 禁用手动输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33436162/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com