Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:26:00+00:00 2026-05-28T14:26:00+00:00

I have a WinForm with a DateTimePicker control on it showing a specific date

  • 0

I have a WinForm with a DateTimePicker control on it showing a specific date from my SQL database.

When the user clicks it, I have a custom form open to ask if they are sure they want to change the date.

If they click OK they can proceed to change the date using the DateTimePicker and the changes are posted back to my SQL Server database.

If the user clicks Cancel instead, the method exits so no changes are saved to the database.

However, what is annoying me, is even when the user clicks Cancel, the DateTimePicker drop down calendar stays open, which a user may interpret as the action having failed to Cancel. How can I get the DateTimePicker to close an open drop down calendar?

private void datDOA_ValueChanged(object sender, EventArgs e)
    {
        frmConfirmBox frmCB = new frmConfirmBox("Are you sure you want to change the Date of Accident?");
        frmCB.ShowDialog();
        if (frmCB.intButton == 0)
        {                
            return;
        }

        datDOA.Format = DateTimePickerFormat.Long;

        //...Connect to Database...//

        string strCaseNo = txtCaseNo.Text;
        string strConnect = BuildConnectionString();
        SqlConnection linkToDB = new SqlConnection(strConnect);
        linkToDB.Open();

        //...Send User Input to Database...//

        DateTime dateNewDate = datDOA.Value;
        string commandText = "UPDATE tblCases SET DOA = @DOAVal WHERE CaseNo = @CaseNoVal;";
        SqlCommand sqlCom = new SqlCommand(commandText, linkToDB);
        sqlCom.Parameters.Add("@DOAVal", SqlDbType.DateTime);            
        sqlCom.Parameters.Add("@CaseNoVal", SqlDbType.VarChar);
        sqlCom.Parameters["@DOAVal"].Value = dateNewDate;
        sqlCom.Parameters["@CaseNoVal"].Value = strCaseNo;
        sqlCom.ExecuteNonQuery();

        linkToDB.Close();
    }        
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:26:01+00:00Added an answer on May 28, 2026 at 2:26 pm

    I’m not sure what are your requirements, but perhaps you could subscribe to the DateTimePicker ValueChanged event and after they changed the selected date, you can ask them if they’re sure that they want to change the date from X to Y. in case they choose cancel you can always roll back to the previous selected date.

    EDIT:
    Assume the name of your DateTimePicker is dateTimePicker1:

    Here is a sample class named DTP which will do just that

         public partial class DTP : Form
         {
            DateTime lastSelectedValue;
    
            public DTP()
            {
             InitializeComponent();
    
             lastSelectedValue = dateTimePicker1.Value;
             dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
    
             }
    
        void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
    
            DialogResult di =  MessageBox.Show("Change " + lastSelectedValue.ToString() + " to " + dateTimePicker1.Value.ToString(), "?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
    
            if(di== System.Windows.Forms.DialogResult.OK)
            {
                SetSqlDateTimeValue( dateTimePicker1.Value );
                lastSelectedValue =  dateTimePicker1.Value;
            }
            else
            {
                 dateTimePicker1.ValueChanged  -= dateTimePicker1_ValueChanged;
                //Setting back the dtp to the old value
                dateTimePicker1.Value = lastSelectedValue;
                dateTimePicker1.ValueChanged += dateTimePicker1_ValueChanged;
            }
        }
    
    
        private void SetSqlDateTimeValue(DateTime dateTime)
        {
            throw new NotImplementedException();
        }
    
    
    
    }
    

    Notice that I have declared a DateTime varible named lastSelectedValue in the top. then after the value is being change I’ll ask the user if he wishes to proceed, if he dose I do the SQL thing, if he dosen’t I will set the Value property of the dateTimePicker1 to the lastSelectedValue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForm application built with VS 2008 (C#) and SQL Server Express
I have a Winform application with 2 DateTimePicker's on it, initialized as follows: private
I have Winform app I am writing in C#. On my form, I have
I have a Winform that has fields need to be filled by a user.
I have a WinForm app that displays results in a Gridview control. If a
I have a winform with 2 WebBrowser control. Is there a way for one
I have a winform app that needs to be aware of when a SQL
I have a winForm application that generates an .aspx file based on the user
I have a winform and a WebBrowser control and I am changing an option
I have a winform datagridview and it forces the user to select by rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.