Solved

C# Datetimepicker

Posted on 2011-03-23
7
773 Views
Last Modified: 2012-05-11
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
Comment
Question by:GRChandrashekar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35198205
>>  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
 

Author Comment

by:GRChandrashekar
ID: 35198302
Time is coming as 00:00:00
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 35198594
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:GRChandrashekar
ID: 35198663
I am not using custom control
Just drag and drop control which is available
what should be the format?
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 35198939
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
 

Author Comment

by:GRChandrashekar
ID: 35198984
In properties, the value is 23/03/2011 8:01 PM
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35199111
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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question