locked
Set dateTimePicker initial value RRS feed

  • Question

  • Hello,

    I have a dateTimePicker in a Windows Form which I would like to have the initial value set to Today + 1 day.

    I have added this code in after my InitializeComponent(); line

                var plusdays = DateTime.Now.AddDays(1);
                dateTimePicker24.Value = Convert.ToDateTime(plusdays);
                MessageBox.Show(dateTimePicker24.Value.ToString());

    I've also tried

      dateTimePicker24.Value = DateTime.Now.AddDays(1);
                MessageBox.Show(dateTimePicker24.Value.ToString());

    and the messagebox displays the correct date (ie today, plus 24hrs), but dateTimePicker24 still displays today's date.

    can someone point out what I am doing incorrectly?

    thank you

    Thursday, March 29, 2018 4:36 PM

Answers

  • Hello, all.

    What I found works, was to delete dateTimePicker24 and add a new one in (with the same name), then my original code works.

    The original dateTimePicker24 was a "cut'n'Paste" from another section, so I must have also copied over some settings.

    Thank you, everyone, for your help.

    Lesson Learnt: Don't Cut and Paste control :-)

    • Marked as answer by G-Oker Wednesday, April 18, 2018 8:36 AM
    Friday, March 30, 2018 10:34 AM

All replies

  • Maybe you are using “Data Binding” too. Then you should change the value of the source object instead of this control.

    Thursday, March 29, 2018 7:05 PM
  • Hi,
    How about using AddDays(n).
    dateTimePicker24.Value = Today.AddDays(1);

    Regards,

    Ashidacchi -- http://hokusosha.com/

    Thursday, March 29, 2018 10:41 PM
  • For a clean slate, this will ensure there is nothing else affecting the outcome.

    private void Form1_Load(object sender, EventArgs e)
    {
        Controls.Add(new DateTimePicker
        {
            Parent = this,
            Value = DateTime.Now.AddDays(1)
        });
    }

    Or drop a DateTimePicker on the form, don't setting any properties

    private void button2_Click(object sender, EventArgs e)
    {
    
        dateTimePicker1.Value = DateTime.Now.AddDays(1);
    
    }

    Both of the above report tomorrow's date.

    Design time, runtime


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Fei Hu Friday, March 30, 2018 7:19 AM
    Thursday, March 29, 2018 11:32 PM
  • Hi @Ashidacchi, I am confused, sorry.

    How is this different to 

    dateTimePicker24.Value = DateTime.Now.AddDays(1)
    ..Plus if I use "today" in VS I get "The name 'Today' doesn't exist in the current context'.

    Friday, March 30, 2018 10:29 AM
  • Hello, all.

    What I found works, was to delete dateTimePicker24 and add a new one in (with the same name), then my original code works.

    The original dateTimePicker24 was a "cut'n'Paste" from another section, so I must have also copied over some settings.

    Thank you, everyone, for your help.

    Lesson Learnt: Don't Cut and Paste control :-)

    • Marked as answer by G-Oker Wednesday, April 18, 2018 8:36 AM
    Friday, March 30, 2018 10:34 AM
  • Hi G-Oker,

    Sorry, I'm not a C# programmer, am writing code with VB.NET.
    Please translate 'Today' according to your language.
    What is needed for you would be "AddDays()" function, isn't it?

    Regards,

    Ashidacchi -- http://hokusosha.com/

    Friday, March 30, 2018 11:29 AM
  • Hello, all.

    What I found works, was to delete dateTimePicker24 and add a new one in (with the same name), then my original code works.

    The original dateTimePicker24 was a "cut'n'Paste" from another section, so I must have also copied over some settings.

    Thank you, everyone, for your help.

    Lesson Learnt: Don't Cut and Paste control :-)

    There is nothing wrong with copy/paste as long as you know what properties where set from the default when first placed on a form.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 30, 2018 12:47 PM