MonthlyCalendar_SelectedDatesChanged event handler not setting property when a date is selected - c#

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

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);

Disable calendar dates in datetimepicker

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 ?

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..

Only update time on datetimepicker, not date

Ok so I want to constantly update the time on my datetimepicker and I'm doing so as follows:
private void timer1_Tick(object sender, EventArgs e)
{
dtpTime.Value = DateTime.Now;
}
The interval for timer1 is set to 1000 (milliseconds). The problem I am having is I also display the date on my datetimepicker and I do not want to change the date with the update, but by assigning DateTime.Now to dtpTime.Value, it updates the entire datetimepicker. I simply cannot find a way to only update the time.
Thanks for the help in advance.
So you need to compose the value from the date of picker and the time of now?
DateTime now = DateTime.Now;
dtpTime.Value = new DateTime(dtpTime.Value.Year, dtpTime.Value.Month, dtpTime.Value.Day, now.Hour, now.Minute, now.Second);
Set the CustomFormat property of the date picker to hh:mm:ss for time or dd/MM/yyyy hh:mm:ss for date and time
and select Custom under the Format Property of datetimepicker
dtpTime.Value = DateTime.Now;
It will display the value according to custom format, list of formats are available here at MSDN
Hope this helps..
To update to the current time, try using
DateTime.Now.TimeOfDay

Categories

Resources