简体   繁体   English

C#:DateTimePicker在使用自定义格式的低宽度程序启动时出现下拉显示错误。 是什么原因造成的?

[英]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: 我想知道:

  1. What is causing this. 是什么原因造成的。
  2. How best to fix it. 如何最好地解决它。

Here's what it looks like: 看起来是这样的:

On Launch 发射时

DateTimePicker在启动时进行控制。下拉图标太宽。

After Resize 调整大小后

调整大小后的DateTimePicker控制。下拉图标就在右边。

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: 如果我根据用户输入添加新控件,它将正确显示:

DateTimePicker错误的动画正在进行中。

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上)发生:

  • The DateTimePicker.Format is set to 'Custom'. DateTimePicker.Format设置为“自定义”。
  • The DateTimePicker's width at startup is set to 109 or wider. 在启动时,DateTimePicker的宽度设置为109或更宽。
  • The right edge of the control is close enough to the formatted date that its icon collapses down to the arrow dropdown. 控件的右边缘距离格式化日期足够近,因此其图标会向下折叠至箭头下拉菜单。

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: 这留下了一些选择:

  1. Make the control big enough at startup. 在启动时使控件足够大。 This requires you to be mindful of the bug as you lay out the form, but it's simple and it works. 这要求您在布局表单时注意错误,但是它很简单并且可以正常工作。
  2. Wait until after the form has loaded to add the controls, using Form.Load . 等到窗体加载完毕后,使用Form.Load添加控件。 This solution is set and forget, but involves modifying the form object itself which might not work for everyone. 这个解决方案是一劳永逸的,但是涉及到修改表单对象本身,可能并不适合所有人。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM