Disable calendar dates in datetimepicker - c#

I am using two datetimepickers in my form. I want to grey out future dates and dates previous to (sysdate -60) in both the datetimepickers .
I have set min and max for both ddatetimepickers when the form loads. This only shows me dates in the set min & max range.
private void form_main_Load(object sender, EventArgs e)
{
dateTimePicker1.MaxDate = DateTime.Today;
dateTimePicker2.MaxDate = DateTime.Today;
dateTimePicker1.MinDate = DateTime.Today.AddDays(-60);
dateTimePicker2.MinDate = DateTime.Today.AddDays(-60);
}
But I want all the dates to be visible but as said above, future and dates previous to (sysdate -60) should be greyed out. Is it possible ?

Related

How to display date in datetimepicker where days is base in numbers of label.text control?

I have example, if datetimepicker1 value is 19/06/2020 then I want datetimepicker2 value is 29/06/2020 because of label shows text is "10" and 10 is the date range. Correct me if I'm wrong, I want to display datetimepicker1 19/06/2020 then datetimepicker2 is 29/06/2020 since label shows 10. How?
You've provided too little information to go on, but since you did tag your question with C#, then date manipulation should be fairly straightforward.
int days = Int32.Parse(someLabel.Text); //no validation; if the label contains a non-number this will throw an exception
DateTime date2 = date1.AddDays(days); //assuming date1 is DateTime
Assuming WinForms:
private void button1_Click_1(object sender, EventArgs e)
{
int daysToAdd;
if (int.TryParse(label1.Text, out daysToAdd))
{
dateTimePicker2.Value = dateTimePicker1.Value.AddDays(daysToAdd);
}
}

How can I limit DatetimePicker in windows form c#?

I have two datetimePickers in my form.
First Start date.
Second End Date.
I want to limit second datetimepicker to the end of the month of the year.
If we are in 2020, its range should be till 31.12.2020.
private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateTimePicker1.MinDate=?
}
You should do something like this. There is no special event for setting the MaxDate you set it once you create an instance of DateTimePicker.
Also you should be setting datepicker'sMaxDate property not MinDate
DateTimePicker dateTimePicker2 = new DateTimePicker();
// Set the MaxDate.
dateTimePicker2.MaxDate= new DateTime(DateTime.Now.Year,12,31);
DateTimePicker has a Value Changed event which is called once you change any dates on the datepicker
You can use something like this
var maxDate = new DateTime(DateTime.Now.Year,12,31);

MonthlyCalendar_SelectedDatesChanged event handler not setting property when a date is selected

so I have two variables
DateTime date1 = DateTime.Now;
DateTime selected;
and an event handler that should change the "selected" variable to the day selected on the calendar. time is a text box where a user can insert the time as well.
private void MonthlyCalendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
{
selected = Calendar.SelectedDate.Value;
selected = DateTime.Parse(time.Text);
}
How can I set the time portion of the selected variable? I know that since DateTime is immutable it can not be changed as easy as I would wish. currently the Parse call resets the selected varriable back to the current date.
You can't exactly change the time but you can try using timespan to add the time to the date
DateTime selected;
selected = Calendar.SelectedDate.Value;
TimeSpan ts = new TimeSpan(09, 15, 0);
selected = selected.Date + ts;
This would make it the same date but 9.15am

how to enable/disable specific dates in DateTimePicker winforms c#

I am programming a C# Windows application for a clinic and i stored days of works for every doctor for example
Dr.John works every Monday and Tuesday how i can enable dates in DateTimePicker for dates that only match the specific days and disable other days .
I don't know what are the methods and functions can help in that
Instead of the DateTimePicker you can
create a form on the fly
add a MonthCalendar to it
add either valid or invalid dates to the BoldDates collection
code the DateChanged event
test to see if a valid date was selected
add it to the list of dates picked
Details depend on what you want: A single date or a range, etc.
Make sure to trim the time portion, mabe like this for adding dates:
List<DateTime> bold = new List<DateTime>();
for (int i = 0; i < 3; i++)
bold.Add(DateTime.Now.AddDays(i*3).Date);
monthCalendar1.BoldedDates = bold.ToArray();
To select only valid dates maybe code like this:
List<DateTime> selected = new List<DateTime>();
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
for (DateTime dt = monthCalendar1.SelectionStart.Date;
dt.Date <= monthCalendar1.SelectionEnd.Date;
dt = dt.AddDays(1))
{
if (!monthCalendar1.BoldedDates.Contains(dt)
&& !selected.Contains(dt)) selected.Add(dt.Date);
}
}
Unfortunately the options to set any stylings are limited to bolding dates. No colors or other visual clues seem to be possible.
So for anything really nice you will have to build a date picker yourself..

How to get week of the month when selecting a date in datetimepicker?

I have a datetimepicker control in winform which I am creating in c#.
How would I get the week number of the month when I select a date in datetimepicker control in c#?
That means, If I select the date as 09/09/2014 then the result should be shown in next textbox control as 2nd week of the month in the winform.
Please help me out.
There is a simpler way to do this :
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
txtweek.Text= (1+(dateTimePicker1.Value.Day / 7)).ToString("0");
}
var month = datetimepicker.Value.Month;
var weekNumberThisMonth = (datetimepicker.Value.Day + (7 - (int)datetimepicker.Value.DayOfWeek)) / 7;
txtweek.Text= (1+(dateTimePicker1.Value.Day / 7)).ToString("0");
This is my answer

Categories

Resources