none
Is it possible to create menu items that display the current date and time? RRS feed

  • Question

  • I am working on a small program to learn various things since I am new to C#. I have a windows form menu with over 20 different combinations of the date and time formatted in different ways. Clicking on any of them will copy the current date and time to the clipboard in the format shown on the menu. So in other words, the menu labels are static but the event will copy the current time to the clipboard.

    Unfortunately the date time formats show on the actual menus are static : (

    If possible I would really love to display the current date and time for each item rather than show static information. Is this possible somehow?




    • Edited by _Sniffles_ Sunday, June 10, 2018 7:19 AM
    Sunday, June 10, 2018 7:16 AM

Answers

  • You can handle the DropDownOpening event of the parent menu item (that contains your various date-time child menu items), and set the text of each item, for example:

    private void parentMenuItem_DropDownOpening( object sender, EventArgs e )

    {

           var now = DateTime.Now;

     

           menuItem1.Text = now.ToLongDateString();

           menuItem2.Text = now.ToLongTimeString();

           // etc. in other languages

           // . . .

    }

     

    This handler can be added in Form Designer.

    But if you want to display and refresh the current time, then add a Timer component to your form, set Interval to 1000. Handle DropDownOpening and DropDownClosed events of the parent menu item, and Tick event of the timer:

    private void parentMenuItem_DropDownOpening( object sender, EventArgs e )

    {

           timer1.Start();

    }

     

    private void timer1_Tick( object sender, EventArgs e )

    {

           var now = DateTime.Now;

     

           menuItem1.Text = now.ToLongDateString();

           menuItem2.Text = now.ToLongTimeString();

           // etc. in other languages

           // . . .

    }

     

    private void parentMenuItem_DropDownClosed( object sender, EventArgs e )

    {

           timer1.Stop();

    }

     

    These menu items were added manually in Form Designer. It is also possible to do this when menu items are added programmatically.




    • Edited by Viorel_MVP Sunday, June 10, 2018 8:42 AM
    • Marked as answer by _Sniffles_ Sunday, June 10, 2018 1:26 PM
    Sunday, June 10, 2018 8:01 AM

All replies

  • You can handle the DropDownOpening event of the parent menu item (that contains your various date-time child menu items), and set the text of each item, for example:

    private void parentMenuItem_DropDownOpening( object sender, EventArgs e )

    {

           var now = DateTime.Now;

     

           menuItem1.Text = now.ToLongDateString();

           menuItem2.Text = now.ToLongTimeString();

           // etc. in other languages

           // . . .

    }

     

    This handler can be added in Form Designer.

    But if you want to display and refresh the current time, then add a Timer component to your form, set Interval to 1000. Handle DropDownOpening and DropDownClosed events of the parent menu item, and Tick event of the timer:

    private void parentMenuItem_DropDownOpening( object sender, EventArgs e )

    {

           timer1.Start();

    }

     

    private void timer1_Tick( object sender, EventArgs e )

    {

           var now = DateTime.Now;

     

           menuItem1.Text = now.ToLongDateString();

           menuItem2.Text = now.ToLongTimeString();

           // etc. in other languages

           // . . .

    }

     

    private void parentMenuItem_DropDownClosed( object sender, EventArgs e )

    {

           timer1.Stop();

    }

     

    These menu items were added manually in Form Designer. It is also possible to do this when menu items are added programmatically.




    • Edited by Viorel_MVP Sunday, June 10, 2018 8:42 AM
    • Marked as answer by _Sniffles_ Sunday, June 10, 2018 1:26 PM
    Sunday, June 10, 2018 8:01 AM
  • WOW. Thank you so much, it works! This is amazing, you guys are amazing. How do you guys come up with this stuff? I didn't even think about looking at events. I won't forget now! Thank you again, this is perfect. I'm very grateful for your help. I've learned a lot from this.
    • Edited by _Sniffles_ Sunday, June 10, 2018 1:29 PM
    Sunday, June 10, 2018 1:28 PM