[英]C#: DateTimePicker has dropdown display errors on program startup at low width with custom format. What is causing this?
DateTimePicker has a calendar drop-down button on its right side. DateTimePicker在右侧具有日历下拉按钮。 Usually it displays a little calendar icon, but if the control is resized to the point where the icon would overlap the text, it changes to a thinner arrow dropdown.
通常它会显示一个小的日历图标,但是如果将控件的大小调整到该图标将与文本重叠的位置,它将变为较细的箭头下拉列表。
The problem: If the DateTimePicker is below that width when the program is launched, the dropdown icon is very wide and overlaps the text. 问题:启动程序时,如果DateTimePicker小于该宽度,则下拉图标非常宽,并且与文本重叠。 Resizing the window horizontally corrects the width, and after that it stays corrected.
水平调整窗口大小可校正宽度,然后保持校正状态。
I would like to know: 我想知道:
Here's what it looks like: 看起来是这样的:
On Launch 发射时
After Resize 调整大小后
Edit #1: 编辑#1:
This display issue only seems to happen on startup. 此显示问题似乎仅在启动时发生。 If I add a new control based on user input it displays properly:
如果我根据用户输入添加新控件,它将正确显示:
Edit #2: 编辑#2:
I've further narrowed down what's causing the issue. 我进一步缩小了导致问题的原因。 It only occurs (on my PC, running Windows 8.1) when:
它仅在以下情况下(在运行Windows 8.1的PC上)发生:
If I set the control's custom format to "MMMM d, yyyy", I get the issue. 如果将控件的自定义格式设置为“ MMMM d,yyyy”,则会出现问题。 This formatting is exactly the same as when DateTimePicker.Format is set to 'Long', but without a custom format the control displays properly.
此格式与将DateTimePicker.Format设置为“长”时的格式完全相同,但是如果没有自定义格式,则控件将正确显示。
As Hans pointed out in the comments above: 正如汉斯在上述评论中指出的那样:
It is just a plain bug, nothing you can do about it because it is baked into the OS.
这只是一个普通的错误,您无能为力,因为它已植入操作系统。
This answers the first part of the question, and means that we're stuck working around it. 这回答了问题的第一部分,这意味着我们一直在努力解决它。 This leaves a few options:
这留下了一些选择:
Option 2 worked well for my purposes. 选项2很好地达到了我的目的。 The code looks something like this:
代码看起来像这样:
public class EventForm : Form
{
public EventForm()
{
this.Load += AddControls;
}
private void AddControls (object sender, EventArgs e)
{
DateTimePicker startDate = new DateTimePicker();
startDate.Format = DateTimePickerFormat.Custom;
startDate.CustomFormat = "MMM dd, yyyy — h:mm tt";
startDate.Dock = DockStyle.Top;
this.Controls.Add(startDate);
// Add the rest of your controls here.
}
}
I'm not sure whether there are any drawbacks to this method, but I'm sure I'll find out soon if there is. 我不确定这种方法是否有缺点,但是我敢肯定,如果有的话,我会尽快发现。
Other answers are appreciated, as is feedback. 赞赏其他答案,反馈也是如此。 For now I'm going to mark this as the answer;
现在,我将其标记为答案; hopefully it helps someone down the road!
希望它可以帮助某人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.