I'm developing a query marking system in Windows Forms
and I had the idea of including a DateTimePicker
where only the dates with available places should be "clickable" by the user.
Is there a way to do this?
I'm developing a query marking system in Windows Forms
and I had the idea of including a DateTimePicker
where only the dates with available places should be "clickable" by the user.
Is there a way to do this?
I have not found an option to restrict certain dates in the DateTimePicker component, you may have to use a third party component to do what you want as suggested by @rodrigorf's comment or create your own.
However, there is an alternative that is to handle the ValueChanged event of the component and check the dates that are being selected. You can do the check through a list of dates like this:
public List<DateTime> datasComConsultas = new List<DateTime>();
Then just check the dates that are being selected in the ValueChanged
event:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (datasComConsultas.Any(d => d == DateTime.Parse(dateTimePicker1.Text))) MessageBox.Show("Esta data nao esta disponivel");
else MessageBox.Show("Data disponivel");
}
This way you can check the dates that do not have available places or the other way around, but this is not the perfect way, but it can help you.
Here is the complete example code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DatePickerExCustom
{
public partial class Form1 : Form
{
public List<DateTime> datasComConsultas = new List<DateTime>();
public Form1()
{
InitializeComponent();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (datasComConsultas.Any(d => d == DateTime.Parse(dateTimePicker1.Text))) MessageBox.Show("Esta data nao esta disponivel");
else MessageBox.Show("Data disponivel");
}
private void Form1_Load(object sender, EventArgs e)
{
datasComConsultas = new List<DateTime>
{
new DateTime(2017, 02, 04),
new DateTime(2017, 02, 05),
new DateTime(2017, 02, 10)
};
}
}
}
Solution source based on this post .