Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Datetimepicker

Posted on 2011-03-23
7
Medium Priority
?
808 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
  • 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
Independent Software Vendors: 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!

 

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 2000 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

Independent Software Vendors: 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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

783 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