Solved

C# Datetimepicker

Posted on 2011-03-23
7
782 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

635 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