Windows Develop Bookmark and Share   
 index > Windows Forms General > How to disable DateTimePicker's drop down calendar?
 

How to disable DateTimePicker's drop down calendar?

I need to disable a custom component that has been inherited from DateTimePicker class. I haveadded a masked text box in this component which overlaps the DateTimePicker's editable region.

What I require is to disable(or change it to readonly)this customized DateTimePicker control without affecting its visualaspects i.e. its forecolor and backcolor. If I setEnable = false; the control will be disabled with change in its forecolor and backcolor.

I can change the backcolor property of the added text box and set its read only property to true. But I cannot prevent the date picker control's dropdown calendar from showing.

Is there a cure for this illness?

Thanks in advance

rageit  Wednesday, June 27, 2007 11:09 AM

Hi, rageit,

Did you add base.Enabled in your codes?

It should be somethinglike

Code Snippet

[Category("Behavior")]
[DefaultValue(true)]
public new bool Enabled
{
get
{

//.............
return base.Enabled;
}
set
{

//............
base.Enabled = value;
}
}

Hope this helps

Regards

Yu Guo â€?MSFT  Monday, July 02, 2007 3:21 AM

Hi, rageit,

Did you add base.Enabled in your codes?

It should be somethinglike

Code Snippet

[Category("Behavior")]
[DefaultValue(true)]
public new bool Enabled
{
get
{

//.............
return base.Enabled;
}
set
{

//............
base.Enabled = value;
}
}

Hope this helps

Regards

Yu Guo â€?MSFT  Monday, July 02, 2007 3:21 AM

Thanks forthe reply.

I added

base.Enabled = value;

in the Enabled property. But when this property is set to false it also disabled the masked edit box and converted its text to grey. I need to prevent this.

Any suggestions.

Thanks and regards

rageit  Friday, July 06, 2007 6:07 AM

Hi, rageit,

You are welcome.

I'm not sure how you added your MaskedTextBox,

but I guess you can try to enable it by add some logic while the enabled property is set to false.

For example,

Code Snippet

set
{

//............
yourDateTimePicker.Enabled = value;

yourMaskedText.Enabled=true;

}

However, I think its against the logic, because when diabled, it means that the control is not editable anymore...

Hopes this helps

Regards

Yu Guo â€?MSFT  Friday, July 06, 2007 8:55 AM

Sorry for my mirky posts which are confuddling you.

Following code is placed after the maskedTextBox1 has been dropped on to the custom datepicker component.

Code Snippet

partial class DatePicker1 : System.Windows.Forms.DateTimePicker

{

//...........

private void InitializeComponent()

{

//......

this.Controls.Add(this.maskedTextBox1);

//.....

}

//..........

}

Sorry, but your suggestions haven't been of much use. Whenever I disable the component, including usage of base.Enabled, entire component is disabled and the text forecolor changes to grey which I need to prevent.

As a temporary solution I hid the calendar control by masked text box in the new enabled property for this component and used masked text box's ReadOnly property.

Code Snippet

public new bool Enabled

{

get { return this.enabled; }

set

{

try

{

this.enabled = value;

if (this.enabled)

{

this.maskedTextBox1.ReadOnly = false;

this.maskedTextBox1.BackColor = SystemColors.Window;

this.maskedTextBox1.ForeColor = SystemColors.WindowText;

this.ShowCalendar = true; //shows the drop down button for calendar

}

else

{

this.maskedTextBox1.ReadOnly = true;

this.ShowCalendar = false; //hides the drop down button for calendar

//is there a way to disable only the calendar's dropdown button

this.maskedTextBox1.BackColor = SystemColors.Window;

this.maskedTextBox1.ForeColor = SystemColors.WindowText;

this.BackColor = SystemColors.Window;

this.ForeColor = SystemColors.WindowText;

}

}

catch (Exception) { }

}

}

Regards

rageit  Friday, July 06, 2007 10:26 AM

Hi, rageit,

The way you write this user control is something different than I expected.

In fact, I think you can do it in a simpler way.

1. Add new User Control.

2. Drag a DateTimePicker and a MaskedTextBox onto the designer, and put MasketTextBox inside the DateTimePicker.

3. Add a small codesegment in InitializeComponent method

Code Snippet

private void InitializeComponent()

"Text", this.dateTimePicker1, "Text");

4. Add your own logic and the Enabled part.

Code Snippet

public new bool Enabled

get

return dateTimePicker1.Enabled;

set

value;

true;

As you can see, only a few lines of codes are needed here.

Hopes this helps,

Regards

Yu Guo â€?MSFT  Monday, July 09, 2007 9:18 AM

Oh wow that was so simple and effective.

But I need to retain invalid dates and prompt the user with a warning message and as well as support null dates. That is why I have incorporated the masked edit box into my date picker component. Binding the maskededit box with the datepicker's text property will not allow this but definitely was a good workaround for my earlier toubles.

Since I have a component derived from datepicker control with masked text box added on it, disabling the datepicker control inevitably disables the entire control including the masked text box.

Thanks and regards.

rageit  Tuesday, July 10, 2007 4:32 AM

Hi, rageit,

I think itwould be difficult to achieve this by directlyinheriting from DateTimePicker,

However,there is still some easy workaround.

First, you can inherit from the DateTimePicker to Build your own DateTimePicker(we say myDateTimePicker),

and then, you can add your specified functions related to DateTimePicker, such as allowingnull dates.

Second, you can use user control to contain your myDateTimePicker and MaskedTextBox,

after that, what you should do is to connect them with some codes.

And the Enable thing could be easily done.

In my point of view, adding controls seperately would be abetter way tocreatecustom controls.

Hopes this helps,

Regards

Yu Guo â€?MSFT  Tuesday, July 10, 2007 6:27 AM

Thanks for your replies.

I have come across another problem.

I have placed following code in my datepicker's validating event.

Code Snippet: Validating event of custom datepicker component

private void myDatePicker1_Validating(object sender, CancelEventArgs e)

{

e.Cancel= false;

if (!IsValid())

{

MessageBox.Show(this.FindForm(),this.maskedTextBox1.Text + " is not a valid date","Invalid Date");

e.Cancel= true;

}

}

When a date is invalid (assessed by IsValid method for which there are several criteria) user should be prompted with a message and focus should be retained in the control. Using e.Cancel as in the above code works fine for me.

But when this control is on a tab page, clicking on another tab page is paralyzing the drop down calendar button. Not only the drop down button but all the control stop working except the ones having cause validation property set to false. Yet the masked edit box is editable. I have checked it with another component derived from datepicker control with just above code being appended, save problem persists in this case too with only the textual area being editable. The drop down button works (i.e.drop down calendaris shown)only when the tab pages are clicked again.

Am I doing something wrong here?

Regards.

rageit  Tuesday, July 10, 2007 8:49 AM

Hi, rageit,

Since your new question "Custome DateTimePicker with Validating Issue" is not directly related to the original "How to disable DateTimePicker's drop down calendar?" issue, it would be best if you open up a new thread for the new question. In this way, our discussion here will not deviate too much from the original issue. This will make answer searching in the forum easier and be beneficial to other community members as well.

Thank you for your understanding and consideration.

Regards

Yu Guo â€?MSFT  Tuesday, July 10, 2007 10:22 AM

Following your suggestion I've created a new thread:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1844382&SiteID=1

Regards,

rageit  Wednesday, July 11, 2007 8:50 AM

You can use google to search for other answers

Custom Search

More Threads

• Invoking components
• system.indexOutOfRangeException in windows.forms.dll
• Resource injection
• ListBox question
• Closing Form1 and opening Form2
• DataGridViewTextBoxColumn bounded to child table
• Is it possible to paint in the non-client area using a Graphics object?
• What is the best way to provide update option to an input form?
• how to save properties i've changed in propertygrid control
• VB2005: ?'s about Tab control and Namespaces