c# - 如何设置最大和最小日期/时间选择器的范围

标签 c# winforms datetime datetimepicker

我想知道如何在 C sharp windows 窗体中为日期时间选择器元素设置最大日期时间选择。我想限制日期可以过去或将来多长时间,但我不确定该怎么做。我不知道我的代码是否有用,但我还是把它附在这里了。提前致谢。

为了简要说明程序正在做什么,对于我的类(class)作业来说,我必须制作一个带有可以执行各种功能的 sql 表的系统,这个范围问题是针对项目中的预订表单的,因此用户可以为他们想要使用的预订时段选择日期和时间,所以我想有一个限制 - 理想情况下,用户不应该能够创建过去日期的预订,他们也不应该能够为 future 荒谬的时间创建一个预订,比如 50 年。

非常感谢任何帮助,我理解对于一些更有经验的编码人员来说,这是否是一个需要解决的简单问题。谢谢。

public partial class AddBooking : Form
{
    private int count;
    private Boolean IsEmpty = false;
    private static string _connectionstring = ConfigurationManager.ConnectionStrings["DoggieConnectionString"].ConnectionString;
    
    public AddBooking()
    {
        InitializeComponent();
        CenterToScreen();
        GenerateBookingNumber();
        IDDisplay.Text = "" + count;
        dateTimePicker2.Format = DateTimePickerFormat.Custom;
        dateTimePicker2.CustomFormat = "HH:mm tt";
        dateTimePicker2.ShowUpDown = true;
        DateTime now = DateTime.Now;
    }

    private void AddBooking_Load(object sender, EventArgs e)
    {
    }

    private int GenerateBookingNumber()
    {
        string smt = "SELECT COUNT(*) FROM dbo.Booking";
        count = 0;

        using (SqlConnection connection = new SqlConnection(_connectionstring))
        {
            using (SqlCommand cmdCount = new SqlCommand(smt, connection))
            {
                connection.Open();
                count = (int)Convert.ToInt32(cmdCount.ExecuteScalar());
            }
        }

        count = count + 4;
        return count;
    }

    private void PresenceCheck()
    {
        if (string.IsNullOrEmpty(WalkLocationtxt.Text) || string.IsNullOrEmpty(StaffIDtxt.Text))
        {
            IsEmpty = true;
        }
        else
        {
            IsEmpty = false;
        }
    }

    private void SubmitInfobtn_Click(object sender, EventArgs e)
    {
       // MessageBox.Show("welcome " + dateTimePicker1.Value.ToShortDateString());
       // MessageBox.Show("Goodddd" + dateTimePicker2.Value.ToShortTimeString());
        PresenceCheck();

        if (IsEmpty == false)
        {
            int rowsareaffected = ClassDatabase.AddBookingDetails(Convert.ToInt32(IDDisplay.Text), dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortTimeString(), Convert.ToInt32(StaffIDtxt.Text), WalkLocationtxt.Text);

            if (rowsareaffected > 0)
            {
                MessageBox.Show("New Booking Added Sucessfully", "Sucess!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                StaffIDtxt.Clear();
                WalkLocationtxt.Clear();
                
                GenerateBookingNumber();

                IDDisplay.Text = "" + count;
            }
            else
            {
                MessageBox.Show("An Error Occurred", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    private void returnToMenuToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new MenuScreen().Show();
        this.Close();
    }

    private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new LoginScreen().Show();
        this.Close();
    }

    private void exitSystemToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DialogResult Result = MessageBox.Show("Are you sure you want to Exit the JD Dog Care Program?", "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);

        if (Result == DialogResult.Yes)
        {
            Application.Exit();
        }
        else
        {
        }
    }

    private void addClientToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new AddClient().Show();
        this.Close();
    }

    private void manageClientsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new ViewClient().Show();
        this.Close();
    }

    private void addDogsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new AddDog().Show();
        this.Close();
    }

    private void manageDogsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new ViewDog().Show();
        this.Close();
    }

    private void addStaffToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new AddStaff().Show();
        this.Close();
    }

    private void manageStaffToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new ViewStaff().Show();
        this.Close();
    }

    private void addBookingToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new AddBooking().Show();
        this.Close();
    }

    private void manageBookingToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new ViewBooking().Show();
        this.Close();
    }

    private void addDogToBookingToolStripMenuItem_Click(object sender, EventArgs e)
    {
        new DogToWalk().Show();
        this.Close();
    }
}

最佳答案

您可以设置 DateTimePicker 的 Min 和 Max 属性:

DateTime now = DateTime.Now;
DateTime minDate = now.AddYears(-50)
DateTime maxDate = now.AddYears(50)

dateTimePicker.MinDate = minDate 
dateTimePicker.MaxDate = maxDate 

关于c# - 如何设置最大和最小日期/时间选择器的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67180325/

相关文章:

C# Unix 时间戳到 DateTime

c# - 在 Windows Phone 应用程序中格式化 MVVM 中的数据

C# - 文件编码问题

winforms - 从另一个程序集(Winforms)加载的继承用户控件中未触发加载事件

c# - Windows 窗体中的默认按钮点击(试图找到最佳解决方案)

php - DB2 时间戳 PHP 日期时间

python - 在 Python 中镜像日期

c# - LINQ 中的返回模态平均值(模式)

c# - 无法加载 'MySql.Data' 或其依赖项之一。

c# - C# 中的多线程启动画面?