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)
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 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.
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 have some datefields in my datasource I would like to format these dates in a textbox on screen in a different format then they are stored in the db. I also want the user to be able to change the date in this format. But this is not possible. The textboxes won't accept the inputed date.
For formatting in the textbox I use:
tbSysDateFrom.DataBindings.Add("Text", myBindingSource, "SysDateFrom", true, DataSourceUpdateMode.OnValidation, "", "dd-MM-yyyy");
This works. But when the user tries to enter a date, i.e. 23-12-2015 the textbox won't accept it. I can understand this because in the database the format is MM/dd/yyyy in this example the datasource thinks that 23 is a month value.
The same problem I have with presenting the data in a GridView. I can format the date to dd-MM-yyyy, but the user is forced to enter the date in a format like MM-dd-yyyy
I'm not sure how to solve this, so does anybody have an idea. Maybe overwriting some methods? But which ones and when? Can anyone put me in the right direction?
B.t.w. I have to use textboxes (cannot use a datepicker)
[Update]
Ok, I was able to figure out how to solve this issue for a textbox. I did this by adding a validation event for the textbox using this code:
private void tbSysDateTo_Validating(object sender, CancelEventArgs e)
{
DateTime dateValue;
if (!String.IsNullOrEmpty(tbSysDateTo.Text))
{
if (DateTime.TryParseExact(tbSysDateTo.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
tbSysDateTo.Text = String.Format("{0:MM/dd/yyyy}", dateValue);
}
}
This works, now I have to do the same for the gridview. Here I'm still stuck. When I change the format in the CellValidating event, I get an error on when the bindingsource wants to save the date.
The error is related to the date: "String was nog recognized as a valid DateTime".
Any suggestions how to solve this issue? TIA
Oke, I was able to solve my issues. First the TextBox-issue (BTW all controle are bound using BindingSources):
The TextBox had to be formatted as dd-MM-yyyy. To do this I added binding tot the TextBox like this
tbSysDateTo.DataBindings.Add("Text", myBindingSource, "SysDateTo", true, DataSourceUpdateMode.OnValidation, "", "dd-MM-yyyy");
This works fine, but when I tried to save the date, an exception was thrown because of the wrong format. I was able to solve this by reformat the text of the TextBox in the validating-event of this TextBox, like:
private void tbSysDateTo_Validating(object sender, CancelEventArgs e)
{
DateTime dateValue;
if (!String.IsNullOrEmpty(tbSysDateTo.Text))
{
if (DateTime.TryParseExact(tbSysDateTo.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
tbSysDateTo.Text = String.Format("{0:MM/dd/yyyy}", dateValue);
}
}
As for the GridView, where also datefields are shown formatted as dd-MM-yyyy, I had to do something similar. But I didn't know which event to use. Row_Validation or Cell_validation didn't do the trick.
I ended up using the CellParsing event which is working fine for me.
private void dgv_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
DateTime dateValue;
// Confirm that the cell is not empty.
if (!string.IsNullOrEmpty(e.Value.ToString()))
{
if (DateTime.TryParseExact(e.Value.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
{
e.Value = dateValue;
e.ParsingApplied = true;
}
}
}
See: This For More info
(corrected the link in More Info, because MS obviously moved the content again)