Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Datetimepicker

Posted on 2011-03-23
7
Medium Priority
?
790 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

719 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