如何设置"最大和最小日期/时间选取器"的范围



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

为了简要介绍该程序正在做什么,在我的课程中,我必须制作一个带有sql表的系统,该系统可以执行各种功能,这个范围问题是针对项目中的预订表单,这样用户就可以为他们想要使用的预订时段选择日期和时间,所以我想有一个限制——理想情况下,用户不应该创建一个带有过去日期的预订,也不应该创建未来荒谬时间的预订,比如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:的最小和最大属性

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

最新更新