在C#Winforms中显示空日期的方法

发布于 2025-02-04 06:08:21 字数 838 浏览 3 评论 0原文

我正在使用Visual Studio 2022修改C#Windows表单程序,并正在寻找一种显示空日期字段的方法(最好使用两个“/”以分开一天,月和年)。

以前的开发人员使用了一个蒙版的文本框,该文本框显示“ _ /__/em> ___”,当日期未写入日期时,但客户端想要一种更加直观,更简单的写作方式来编写日期,这使光标放在在第一个位置会自动检查日期是否正确。

我想到使用DateTimePicker,因为它会自动检查书面日期是否正确,并且还具有日历以使其更容易,但客户端不喜欢默认情况下显示日期的事实。我尝试使用自定义空白格式,但是用户需要选择带日历的日期,而不是手动工作,我知道她不会喜欢它。

这是我编写的代码:( DTP的默认格式是自定义的,显示“ / /”

private void dtp_birthdate_ValueChanged(object sender, EventArgs e)
{
    dtp_birthdate.Format = DateTimePickerFormat.Short;
}

private void btn_enter_Click_1(object sender, EventArgs e)
{
    if (dtp_birthdate.Format == DateTimePickerFormat.Custom)
    {
        lbl_errorMessage.Visible = true;
        errorProvider6.SetError(dtp_birthdate, "The birth date must be written!");
        return;
    }
}

。我应该使用另一种类型的文本框吗?

谢谢

I'm modifying a C# Windows Form program using Visual Studio 2022 and am looking for a way to display an empty date field (preferably with two "/" to separate the day, month, and year).

The previous developer used a masked textbox which displayed "_/__/___" when a date isn't written, but the client wanted a more intuitive and easier way to write the date, which puts the cursor at the first position automatically, while also checking if the date is correct.

I thought of using a DateTimePicker since it automatically checks if the date written is correct or not, and it also features a calendar to make it easier, but the client doesn't like the fact that a date is displayed by default. I tried using a custom blank format but then the user need to select a date with the calendar and not manually for it to work and I know she will not like it.

Here is the code that I wrote : (the default format of the dtp is a custom one, displaying " / / ".)

private void dtp_birthdate_ValueChanged(object sender, EventArgs e)
{
    dtp_birthdate.Format = DateTimePickerFormat.Short;
}

private void btn_enter_Click_1(object sender, EventArgs e)
{
    if (dtp_birthdate.Format == DateTimePickerFormat.Custom)
    {
        lbl_errorMessage.Visible = true;
        errorProvider6.SetError(dtp_birthdate, "The birth date must be written!");
        return;
    }
}

Is there a way to make my dtp display an empty date while not using a custom format ? Should I use another type of textbox?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2025-02-11 06:08:22

使用此自定义DateTimePicker,我没有编码,我在stackoverflow上发现它。

简单的用法

nullableDateTimePicker1.Value = DateTime.MinValue;

控制

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsLibrary.Controls
{

    public class NullableDateTimePicker : DateTimePicker
    {
        private DateTimePickerFormat originalFormat = DateTimePickerFormat.Short;
        private string originalCustomFormat;
        private bool isNull;

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new DateTime Value
        {
            get => isNull ? DateTime.MinValue : base.Value;
            set
            {
                // incoming value is set to min date
                if (value == DateTime.MinValue)
                {
                    // if set to min and not previously null, preserve original formatting
                    if (!isNull)
                    {
                        originalFormat = Format;
                        originalCustomFormat = CustomFormat;
                        isNull = true;
                    }

                    Format = DateTimePickerFormat.Custom;
                    CustomFormat = " ";
                }
                else // incoming value is real date
                {
                    // if set to real date and previously null, restore original formatting
                    if (isNull)
                    {
                        Format = originalFormat;
                        CustomFormat = originalCustomFormat;
                        isNull = false;
                    }

                    base.Value = value;
                }
            }
        }

        protected override void OnCloseUp(EventArgs eventArgs)
        {
            // on keyboard close, restore format
            if (MouseButtons == MouseButtons.None)
            {
                if (isNull)
                {
                    Format = originalFormat;
                    CustomFormat = originalCustomFormat;
                    isNull = false;
                }
            }

            base.OnCloseUp(eventArgs);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            // on delete key press, set to min value (null)
            if (e.KeyCode == Keys.Delete)
            {
                Value = DateTime.MinValue;
            }
        }

        /// <summary>
        /// Indicates if Value has a date or not
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public bool IsNullValue => Value == DateTime.MinValue;
    }
}

Use this custom DateTimePicker, I didn't code it, found it I believe here on Stackoverflow.

Simple usage

nullableDateTimePicker1.Value = DateTime.MinValue;

Control

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsLibrary.Controls
{

    public class NullableDateTimePicker : DateTimePicker
    {
        private DateTimePickerFormat originalFormat = DateTimePickerFormat.Short;
        private string originalCustomFormat;
        private bool isNull;

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new DateTime Value
        {
            get => isNull ? DateTime.MinValue : base.Value;
            set
            {
                // incoming value is set to min date
                if (value == DateTime.MinValue)
                {
                    // if set to min and not previously null, preserve original formatting
                    if (!isNull)
                    {
                        originalFormat = Format;
                        originalCustomFormat = CustomFormat;
                        isNull = true;
                    }

                    Format = DateTimePickerFormat.Custom;
                    CustomFormat = " ";
                }
                else // incoming value is real date
                {
                    // if set to real date and previously null, restore original formatting
                    if (isNull)
                    {
                        Format = originalFormat;
                        CustomFormat = originalCustomFormat;
                        isNull = false;
                    }

                    base.Value = value;
                }
            }
        }

        protected override void OnCloseUp(EventArgs eventArgs)
        {
            // on keyboard close, restore format
            if (MouseButtons == MouseButtons.None)
            {
                if (isNull)
                {
                    Format = originalFormat;
                    CustomFormat = originalCustomFormat;
                    isNull = false;
                }
            }

            base.OnCloseUp(eventArgs);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            // on delete key press, set to min value (null)
            if (e.KeyCode == Keys.Delete)
            {
                Value = DateTime.MinValue;
            }
        }

        /// <summary>
        /// Indicates if Value has a date or not
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public bool IsNullValue => Value == DateTime.MinValue;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文