I have two datetimepicker control , datetimepicker1 and datetimepicker2.
i have select the date 24-02-2018 in datetimepicker1, so please tell me how can i set max value for datetimepicker2 which shouldn't be maximum then datetimepicker1 value.
Datetimepicker2 date should always be less than datetimepicker1 value.
What I have tried:
dateTimePicker2.MaxDate = dateTimePicker1.Value;
You have to set the MaxDate every time the Value in dateTimePicker1 changes.
You have to listen to the ValueChanged-event
private void InitializeComponent()
{
// Some code
this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
// some code
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (this.dateTimePicker2.Value > this.dateTimePicker1.Value)
{
this.dateTimePicker2.MaxDate = this.dateTimePicker1.Value;
}
}
You should let Visual Studio do the work for you: Select dateTimePicket1 in your form. Select events (the little lightning in the properties-box) search for ValueChanged-event. Doubleclick on the name to add a method which is run on the event. Add you max-date-code there.
Explaination: If you set this.dateTimePicket2.Maxdate = this.dateTimePicker1.Value it'll be a copy of your Value. If the Value of dateTimePicker1 changes later on, the MaxDate will be still the same. You have to set it again.
Related
I wanted to ask how can I display selected month from calendar and display into my text box? I tried to use ToString(), but it still didn't work. I'm thinking there's difference in date picker and calendar, still not sure. Can anyone please help me out here? Thanks alot. Here's my coding;
In xaml;
Calendar Name ="dteSelectedMonth" DisplayMode="Year" SelectionMode="SingleDate" DisplayModeChanged="dteSelectedMonth_DisplayModeChanged" DisplayDateChanged="monthCalendar_DataChanged"
In xaml.cs;
private void monthCalendar_DataChanged(object sender, CalendarDateChangedEventArgs e)
{
monthDisplay.Text = dteSelectedMonth.SelectedDate.ToString();
}
You could get the selected month using the DisplayDate property. Make sure that the IsLoaded property returns true before you try to set the Text property:
private void monthCalendar_DataChanged(object sender, CalendarDateChangedEventArgs e)
{
if (IsLoaded && dteSelectedMonth.DisplayDate != null)
monthDisplay.Text = dteSelectedMonth.DisplayDate.ToString("MMM");
}
Use CalendarDateChangedEventArgs object and its property AddedDate. When the event is fired, it will contain the previously selected day in the new month. Then you can convert it to any string format, e.g. to get the month.
Please check your monthDisplay textbox is null or not. This happens because the displayDteChanged event fires before the initialization of the textbox (If you have the declaration of TextBox before that of Calendar). Add a null check to handle this.
if (monthDisplay != null)
monthDisplay.Text = e.AddedDate?.Month.ToString();
I have a datetimepicker values in datagridview. (Arrival date and departure date)
Here is screen shoot.
Also, I have an edit form for the values in datagrid view.
How can I pass the values of datetimepicker from datagridview to the second form as datetimepicker value.
Here is my try:
private void arrivaldgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
edit edt = new edit();
edt.label12.Text = this.label2.Text;
edt.label13.Text = this.arrivaldgv.CurrentRow.Cells[0].Value.ToString();
edt.textBox1.Text = this.arrivaldgv.CurrentRow.Cells[1].Value.ToString();
edt.textBox2.Text = this.arrivaldgv.CurrentRow.Cells[2].Value.ToString();
edt.textBox3.Text = this.arrivaldgv.CurrentRow.Cells[3].Value.ToString();
/* edt.dateTimePicker1.Value = this.arrivaldgv.CurrentRow.Cells[4].Value; problem here */
edt.ShowDialog();
}
Try the System.Convert.ToDateTime method:
edt.dateTimePicker1.Value = System.Convert.ToDateTime(this.arrivaldgv.CurrentRow.Cells[4].Value);
You can use the DateTime.Parse method:
edt.dateTimePicker1.Value = DateTime.Parse(this.arrivaldgv.CurrentRow.Cells[4].Value);
Most of the .ToX methods use some instance methods internally, such is the case here too, which means that this should work faster than Convert.ToDateTime()
datetimepicker.Value or datetimepicker.Text is always returning the current DateTime, not the selected one. Is there any property that I need to set to get the selected date time.
Below is my code:
private void button1_Click(object sender, EventArgs e)
{
c1.cName = textBox1.Text;
c1.dlv_Time = dateTimePicker1.Value;
string temp = dateTimePicker1.Text;
textBox1.Text = "";
dateTimePicker1.Text = "";
}
In the Form UI, I am changing the time, but in the code Im always receiving the present time.
Thanks in advance
dateTimePicker1.Value.TimeOfDay.ToString()
It will return you the selected time of the date picker control in the string format.
I was editing a different timepicker and reading from a different timepicker.
I have resolved it by changing datetimepicker1 to datetimepicker2.
I'm probably missing something obvious but I can't see it.....
I have a DateTimePicker control (Winforms) to display just the Time HH:mm:ss of the DateTime. The properties are as follows
Checked = False
Format = Time
ShowCheckbox = False
ShowUpDown = True
Value = 28/10/2014 08:00
ApplicationSettings.PropertyBinding.Value = pickTime1
Where pickTime1 is a user setting where Properties.Settings.Default.pickTime1 = 28/10/2014 08:00
I'm expecting the control to display 08:00:00 when the form first loads but it displays the current time. How do I ensure the user setting is displayed when first initialized?
Subscribe to the Form.Load event and set it in there:
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Value = Properties.Settings.Default.pickTime1;
}
This will display 8:00:00 AM.
If you want 08:00:00, change Format to Custom, and set the CustomFormat property to "hh:mm:ss".
The DateTimePicker.Value must be changed either by code or by user input, otherwise it will set to the current date and time (i.e. DateTime.Now)
I have a C# WinForms project targeting .NET 3.5 with two DataGridViews. Visual Studio 2010 Pro on Win 7 64 bit.
I'm using two DateTimePicker controls (one for time and one for date) created at run-time which are floating on top of the appropriate DateTime cells in the gridview. The pickers are only displayed when a time or date cell gets focus.
System time format is 24 hours.
private DateTimePicker timePicker;
timePicker = new DateTimePicker();
timePicker.Format = DateTimePickerFormat.Custom;
timePicker.CustomFormat = "HH:mm";
timePicker.MaxDate = new DateTime(9998, 12, 31);
timePicker.MinDate = new DateTime(1753, 01, 01);
timePicker.ShowUpDown = true;
timePicker.Width = 60;
timePicker.Hide();
timePicker.ValueChanged += new EventHandler(timePicker_ValueChanged);
timePicker.KeyPress += new KeyPressEventHandler(timePicker_KeyPress);
timePicker.LostFocus += new EventHandler(timePicker_LostFocus);
dgTxSummary.Controls.Add(timePicker);
void timePicker_ValueChanged(object sender, EventArgs e)
{
// dgTxSummary is the DataGridView containing the dates and times
dgTxSummary.CurrentCell.Value = timePicker.Value;
}
void timePicker_LostFocus(object sender, EventArgs e)
{
timePicker.Hide();
}
// Used for debugging; shows a message in a text field when 10, 20 etc
// where entered
void timePicker_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsDigit(prevChar) && e.KeyChar == '0')
{
string correctHour = prevChar.ToString() + '0';
Message = "Should have been " + correctHour;
}
prevChar = e.KeyChar;
}
When a time cell is clicked, the timepicker is displayed
private void ShowTimePicker(Point cellPos, DateTime dt)
{
timePicker.Location = cellPos;
timePicker.Value = dt;
timePicker.Show();
timePicker.Focus();
}
The problem only occurs when 2 conditions are met:
1) the first time a number is entered after the picker gets focus
2) a valid number ending with 0* is manually entered (keyboard)
*0, 10 or 20 in the hour slot, 0, 10, ... , 50 in the minute slot
Using the updown buttons works fine.
The result is that at first the previous time or date is shown and the updown buttons disappear the control (timepicker) is either hidden or closed, and the underlying cell in the datagridview has focus and is in edit mode with the previous time or date. When clicking inside or outside the cell, 01 is shown if 10 was entered.
I've created a design-time datetimepicker on the same form, and it works normally.
What am I missing?
I've finally found the culprit in the code:
dgTxSummary.Controls.Add(timePicker);
It seems the DateTimePicker needs to be added to the form, not the DataGridView.
After changing the code to this.Controls.Add(timePicker); and using datePicker.BringToFront(); in ShowTimePicker(...) (to avoid the control being hidden behind the DataGridView), all is good :-)
FWIW I use the following code to place the DateTimePicker on top of the corresponding cell (code stripped of error handling, null checking etc):
private void dgTxSummary_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
Rectangle cellRect = dgTxSummary.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
Point dgPos = dgTxSummary.Location;
Point cellPos = new Point(dgPos.X + cellRect.X, dgPos.Y + cellRect.Y);
DateTime dt = DateTime.Parse(row.Cells[e.ColumnIndex].FormattedValue.ToString());
ShowDatePicker(cellPos, dt);
}