checking value changed or not in datetimepicker - c#

I have 2 datetimepicker on form having joining and leaving date ,if i dont change these values than it throws exception on total_button_click,since its default value is today's date ,so i want to enable total_button only when datetimepicker value gets changed,can i use any property of datetimepicker for this purpose ,below is the code for total_button
private void Total_button_Click(object sender, EventArgs e)
{
int remain, re, d, r;
string oday;
decimal sum = dataGridView1.Rows.OfType<DataGridViewRow>()
.Sum(row => Convert.ToDecimal(row.Cells["Money"].Value));
// total amnt drawn
textBox1.Text = sum.ToString();
d = int.Parse(textBox3.Text);
int div = d / 30;
// 1 day payment
oday = div.ToString();
textBox6.Text = (dateTimePicker2.Value - dateTimePicker1.Value).TotalDays.ToString("#");
re = int.Parse(textBox6.Text) * int.Parse(oday);
// total days paymnt
textBox7.Text = re.ToString();
r = int.Parse(textBox7.Text) - int.Parse(textBox1.Text);
// total payment -drawn i.e to b payed
textBox8.Text = r.ToString();
}

Use the DateTimePickerEvent ValueChanged http://msdn.microsoft.com/de-de/library/system.windows.forms.datetimepicker.valuechanged.aspx . If the event gets invoked, you can enable your button.

To get this working you would need the ValueChanged event of the datetimepicker
dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
Now this would fire in general even if you select the current date from the Calender displayed, to avoid that set the format to only Date and avoid the time component like
dateTimePicker1.Value = DateTime.Today;
Now this would fire only if you change the date and within this you can enable your required button

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

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 ?

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

C# Winforms DateTimePicker Custom Formatted to Tenths of a Second / Default Selection to Seconds

My application currently uses the DateTimePicker custom formatted to display HH:mm:ss.
How can I format it so to allow for tenths of a second?
My second question is that when I tab into the datetimepicker, the Hours section is selected first by default, is there anyway to change this so that seconds are selected first by default? I noticed that If you click the updown arrows without tabbing into the control the seconds section is selected by default.
Any experts on the datetimepicker out there? Or does anyone have an ideas on an alternative I can use to implement these features?
Below is a picture of how it is formatted as well as the properties:
You could try to force the DateTimePicker to display milliseconds, but it will be a painful experience. You are better off using a MaskedTextBox. You can download TimePicker.cs from sourceforge: https://sourceforge.net/projects/time-picker/
Here is some sample code:
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Form f = new Form();
var dp = new TimePicker("hh:mm:ss.f", true) { Location = new Point(100, 100) };
dp.ByValue[2] = false;
f.Controls.Add(dp);
var bt1 = new Button { Text = "Now", Location = new Point(110, 130) };
bt1.Click += delegate {
dp.Value = DateTime.Now;
};
int k = 0;
dp.ValueChanged += delegate {
bt1.Text = "Now " + (k++);
};
f.Controls.Add(bt1);
Application.Run(f);
}
This could work
Add a DomainUpDown control
Add a bunch of datetimes that vary by .1 seconds and display them as strings formatted to show miliseconds. Your user can then click up and down to see .1 second deltas
string dateString = "7/9/2015 9:32:45.126 AM";
DateTime dateValue = DateTime.Parse(dateString);
for (int i = 0; i < 5; i++)
{
dtPicker.Items.Add(dateValue.AddSeconds(.1*i).ToString("MM/dd/yyyy hh:mm:ss.fff tt"));
}
If you want to let the user pick say the hour first and then generate your datetime deltas from there it won't work as well. You could create multiple numeric controls, but at that point you might want to go down the path of extending the datetime picker like here.

dateTimePicker1.Value < dateTimePicker2.Value = true when values are identical on runtime

I have dateTimePicker1 and dateTimePicker2 controls loading on Form1. They both have the same date and time on load.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy-MM-dd hh:mm:ss";
dateTimePicker2.Format = DateTimePickerFormat.Custom;
dateTimePicker2.CustomFormat = "yyyy-MM-dd hh:mm:ss"
When I check if they have different values using
if (dateTimePicker1.Value < dateTimePicker2.Value) {
Console.WriteLine(dateTimePicker1.Value + " is earlier than " + dateTimePicker2.Value);
}
the statement returns true and writes to the console. This is not what I would expect. I would expect this to return false.
If I increase each control's value by 1 second, causing them to still match, the statement returns false as expected and nothing is written to the console.
Why does the less than evaluation return true on load when both values are identical?
Do not know how you are loading the values. But, depending on what precision you are looking for (eg. in hours, minutes or second) you can subtract the two values and compare. Example: If you need precision in seconds then you can do something similar to below:
dateTimePicker1.Value = DateTime.Now;
dateTimePicker2.Value = DateTime.Now.AddMilliseconds(999);
var timeSpan1 = dateTimePicker1.Value - dateTimePicker2.Value;
if (Math.Abs(timeSpan1.TotalSeconds) > 1) {
MessageBox.Show(dateTimePicker1.Value + " is not same as " + dateTimePicker2.Value);
} else {
MessageBox.Show(dateTimePicker1.Value + " is same as " + dateTimePicker2.Value);
}
The answer is given by setting the two values equal to each other on load. This is because the controls load at different times. They are not really equal.
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker2.Value = dateTimePicker1.Value;
}
I'm not sure how to give credit here, it belongs to two commenters.

Categories

Resources