locked
how do disable the buttons in datetime picker RRS feed

  • Question

  • hi

    The control date time picker has two buttons in it "<" (previous month) and ">" (next month), i hope every one knows this.
    I have many serious issues with these two buttons when i register on valuechanged event for this control.

    I have registered valuechanged event on a datetimepicker , so when i press on either of the two buttons mentioned above, the control falls in infinite loop and does not stop till I click on any other control which are on my application.

    Is there any way where-in i can disable these two buttons?

    please guide me how to make visible false or atleast make them disable these two buttons for ever.

    i hope i get the replies with the solution.

    thanks in advance
    suman bathula

    Monday, December 10, 2007 9:19 AM

Answers

  •  

    Hi suman,

    Another workaround is to use the Control.BeginInvoke method. This will execute a delegate asynchronously on the thread that the control's underlying handle was created on. Try something like the following

    Code Block

        public partial class Form4 : Form

        {

            public Form4()

            {

                InitializeComponent();

            }

     

            private void dateTimePicker1_ValueChanged(object sender, EventArgs e)

            {

                this.dateTimePicker1.BeginInvoke(new MethodInvoker(ShowMessage));

            }

     

            void ShowMessage()

            {

                MessageBox.Show("its sucking...");

            }

        }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Thursday, December 13, 2007 8:22 AM
  • Hi

    Yes, you are 100% correct about the infinite loop. This is a well know issue in the framework as described here https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=236715&wa=wsignin1.0 

    A workaround would be to set a TextBox.Text in the ValueChanged event and then showing the MessageBox from the TextChanged event of the TextBox.
    Monday, December 10, 2007 10:32 AM

All replies

  • Hi. May I ask why you would want to do this?
    Monday, December 10, 2007 9:37 AM
  • Hi

    datetimepicker's valuechanged event is being called for ever, the problem is with these two buttons only. as event is fired all the time it is falling in infinite loop.

    Try this one.
    create a new project(DtTmPckrDemo), drag and drop the datetimepicker onto the form, in the Valuechanged event for this control add a single message box statement (eg: messagebox.show("its sucking our life"). now run the application and click on the two buttons "<" or ">" , it would not stop at any point of time.

    so i guess this is the problem with datetimepicker event, thats why trying to disable the buttons.

    hope you get me.

    thanks
    suman
    Monday, December 10, 2007 9:51 AM
  • Hi

    I have create a project, added a DateTimePicker to the form and clicked the < and > buttons several times. The control behaves exactly as expected. No infinate loop. The value changed event is called once for every time the button is clicked.
    Monday, December 10, 2007 10:01 AM
  • i think you have not added any message box to the valuechanged event, if you dont then it works as expected.

    Form1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
            {
                MessageBox.Show("its sucking...");
            }
        }
    }

    Form1.Designer.cs

    namespace WindowsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
                this.SuspendLayout();
                //
                // dateTimePicker1
                //
                this.dateTimePicker1.Location = new System.Drawing.Point(55, 73);
                this.dateTimePicker1.Name = "dateTimePicker1";
                this.dateTimePicker1.Size = new System.Drawing.Size(200, 20);
                this.dateTimePicker1.TabIndex = 0;
                this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.dateTimePicker1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);

            }

            #endregion

            private System.Windows.Forms.DateTimePicker dateTimePicker1;
        }
    }


    now try my code you would definitely get the message boxes n number of times.

    thanks for you support.

    Monday, December 10, 2007 10:14 AM
  • i am using C# 2.0 dot net 2005.
    Monday, December 10, 2007 10:17 AM
  • Hi

    Yes, you are 100% correct about the infinite loop. This is a well know issue in the framework as described here https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=236715&wa=wsignin1.0 

    A workaround would be to set a TextBox.Text in the ValueChanged event and then showing the MessageBox from the TextChanged event of the TextBox.
    Monday, December 10, 2007 10:32 AM
  • thanks van for the link.

    but i guess there would be some solution to hide / disable the buttons.

    Monday, December 10, 2007 10:57 AM
  •  

    Hi suman,

    Another workaround is to use the Control.BeginInvoke method. This will execute a delegate asynchronously on the thread that the control's underlying handle was created on. Try something like the following

    Code Block

        public partial class Form4 : Form

        {

            public Form4()

            {

                InitializeComponent();

            }

     

            private void dateTimePicker1_ValueChanged(object sender, EventArgs e)

            {

                this.dateTimePicker1.BeginInvoke(new MethodInvoker(ShowMessage));

            }

     

            void ShowMessage()

            {

                MessageBox.Show("its sucking...");

            }

        }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Thursday, December 13, 2007 8:22 AM