Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 837
  • Last Modified:

C# Datetimepicker

I am using windows application in C# and .net framework 2.0

I have datepicker with check box

When saving to DB, i want to save both date and time.

Problem:
I want this datepicker to return both and time.
While saving, I dont want to write if checked=true in every form. I need this in class level so that I can just pass the control and return date time if it is checked
0
GRChandrashekar
Asked:
GRChandrashekar
  • 3
  • 2
  • 2
1 Solution
 
käµfm³d 👽Commented:
>>  so that I can just pass the control and return date time if it is checked

You can simply create a function that is accessible to all forms and takes a parameter of DateTimePicker:
public Nullable<DateTime> GetDTPValue(DateTimePicker picker)
{
    if (picker.Checked)
    {
        return picker.Value;
    }
    else
    }
        return null;
    }
}

Open in new window

0
 
GRChandrashekarAuthor Commented:
Time is coming as 00:00:00
0
 
angus_young_acdcCommented:
Are you using a custom control or have you changed to a custom format for the value?  As the code provided works perfectly for me.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
GRChandrashekarAuthor Commented:
I am not using custom control
Just drag and drop control which is available
what should be the format?
0
 
angus_young_acdcCommented:
By default it will have the date and time as the value (although the time wouldn't be displayed on the control, which you should see in the Value property when you right click it and select "Properties".

If that property only has the date or whatever you can try to alter the forms Design with this:
this.dateTimePicker1.Value = new System.DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day, System.DateTime.Now.Hour, System.DateTime.Now.Minute, System.DateTime.Now.Second, System.DateTime.Now.Millisecond);
0
 
GRChandrashekarAuthor Commented:
In properties, the value is 23/03/2011 8:01 PM
0
 
käµfm³d 👽Commented:
Here is my example:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetDTPValue(this.dateTimePicker1).Value.ToString());
        }

        public Nullable<DateTime> GetDTPValue(DateTimePicker picker)
        {
            if (picker.Checked)
            {
                return picker.Value;
            }
            else
            {
                return null;
            }
        }
    }
}

Open in new window

untitled.PNG
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now