Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Applying MaskedTextBox control behavior to a property item field in PropertyGrid
 

Applying MaskedTextBox control behavior to a property item field in PropertyGrid

Hi all,

I amworking onC# and.NET. As we know that, the best way to stop users from entering invalid data into an input control is Masked TextBox by which we can assign a predefined mask such as Short DateTime mask (__/__/____).

I have a PropertyGrid and assigned it to an instance of a class which is having properties of DateTime type and some other (e.g., string, Bool ). How can I adopt MaskedTextBox(Which is having short date mask) behavior to a property item(of DateTime type) field in propertygrid?Can I use the instance of a MaskedTextBox and apply it to a Property item field of DateTime ?

Please, suggest me in the same. Thanks in advance.

Cheers,
Kumar.

Kumar...  Monday, February 23, 2009 9:09 AM
The .NET Framework types use the TypeConverter and UITypeEditor classes to provide most of the PropertyGrid editing support. You can implement a dropdown window in yourUITypeEditor like TextBox's Text property, but instead of a text box you add a masked textbox. Search "Getting the Most Out of the .NET Framework PropertyGrid Control" if you need code example.
MSMVP VC++
Sheng Jiang 蒋晟  Monday, February 23, 2009 11:32 PM
Hi Kumar,

That's why I said it's much more complexer.I can't think an easy way for this. It's very common to extend UITypeEditor to implment the DropDown or ModelForm Editor for the Property at design time. But seldom doI see it is used to customize the TextBox in the PropertyGrid.

I think the default behavior is easy enough for the user to input time format data. If you think it's very important in your product, then you should implement it no matter how complex thecase will be. Otherwise, you can search if there is a third party PropertyGrid has implemented this feature.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, March 06, 2009 9:32 AM
HiBruce Zhou,

Thank you for your suggestions. My opinion is also same as you. I should go forthird party PropertyGrid or else I should tryfor other way.

But, I have learnt a lot from this discussion. Thank you very much.......

Regards,
Kumar.
  • Marked As Answer byKumar... Friday, March 06, 2009 10:23 AM
  •  
Kumar...  Friday, March 06, 2009 10:22 AM
The .NET Framework types use the TypeConverter and UITypeEditor classes to provide most of the PropertyGrid editing support. You can implement a dropdown window in yourUITypeEditor like TextBox's Text property, but instead of a text box you add a masked textbox. Search "Getting the Most Out of the .NET Framework PropertyGrid Control" if you need code example.
MSMVP VC++
Sheng Jiang 蒋晟  Monday, February 23, 2009 11:32 PM

Hi Sheng Jiang

Thanks for your reply. You are correct. It seems to be a possible solution. I have tried implementing as you explained.

But, I am not able to get what I want. I have created a UItypeEditor class and I have mentioned masked text instead of textbox. But, after doing all these, I failed to see behavior of the masked text box in the Property item field. Can you please explain clearly? So that I can correct myself if I am wrong anywhere. If you have any source code with simple example, please shere with me. Thanks in advance.

Kumar...  Tuesday, February 24, 2009 12:43 PM
Don't know how to correct you since I don't know how you implemented it. Read the sample again and check where you are missing.
MSMVP VC++
Sheng Jiang 蒋晟  Tuesday, February 24, 2009 2:41 PM
Thanks for the sample code. I have created my own UITypeEditor class (MaskedEditor.cs) which has been inherited from UITypeEditor class and overriddenGetEditStyle function. In this function, I have returned UITypeEditorEditStyle.None. Because, being my property item field is of DateTime type, by default it will have a DateTimePicker on right side of the Property item field. I need DateTimePicker too in order to edit that field.

But, I am not able to override EditValue() function properly. Where should I mention masked text box and its format in the TypeEditor class? How GetPropertiesSupported() and GetProperties() functions should be in TypeConverter?

Please, suggest me. Thank you.

Kumar...  Wednesday, February 25, 2009 4:00 PM

Hi Kumar,

I am not sure if you understand Sheng Jiang's suggestion correctly. Actually, he suggested you implement a dropdown control as a MaskTextBox, and you can achieve this by extending UITypeEditor class.

By default, the PropertyGrid control has a DateTimePicker control for the property of DateTime type. This can ensure the user input valid. If you input invalid, the PropertyGrid also prompts the error. So I think the default behavior is enough.

Once you override the GetEditStyle method, and return UITypeEditorEditStyle.None, the Editor will lose the dropDown style. So you can't use the DateTimePicker any more.

If you want to change the DropDown control to a MaskTextBox, you can finish the work in the overriden EditValue method. I am just writing the following code for your reference.

publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
CultureInfoci=newCultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture=ci;
MyCustomObjobj=newMyCustomObj();
this.propertyGrid1.SelectedObject=obj;
}
}
//MyCustomObjclass
publicclassMyCustomObj
{
privateDateTimedt;
//constructor
publicMyCustomObj()
{
dt=DateTime.Now;
}
//ApplyEditorpropertyandTypeConvertrattributeforDtproperty.
[Editor(typeof(DateTimeEditor),typeof(UITypeEditor))]
[TypeConverter(typeof(DateTimeConverter))]
publicDateTimeDt
{
get{returndt;}
set{dt=value;}
}
}
//ExtendfromtheUITypeEditor
publicclassDateTimeEditor:UITypeEditor
{
publicoverrideUITypeEditorEditStyleGetEditStyle(ITypeDescriptorContextcontext)
{
//returnbase.GetEditStyle(context);
returnUITypeEditorEditStyle.DropDown;
}
publicoverrideobjectEditValue(ITypeDescriptorContextcontext,IServiceProviderprovider,objectvalue)
{
try
{
IWindowsFormsEditorServicewinFormEditorService=provider.GetService(typeof(IWindowsFormsEditorService))asIWindowsFormsEditorService;
MaskedTextBoxmtb=newMaskedTextBox();
mtb.Mask="00/00/0000";
winFormEditorService.DropDownControl(mtb);
DateTimedt;
CultureInfoci=CultureInfo.CreateSpecificCulture("en-US");
DateTime.TryParse(mtb.Text,ci,DateTimeStyles.AssumeLocal,outdt);
value=dt;
}
catch
{
System.Diagnostics.Debug.WriteLine("typeconversionerror");
}
returnvalue;
}
}
//ImplementtheConverterforDateTimepropertyintheMyCustomObjclass.
publicclassDateTimeConverter:TypeConverter
{
//overrideCanConvertFrommethod
publicoverrideboolCanConvertFrom(ITypeDescriptorContextcontext,TypesourceType)
{
if(sourceType==typeof(String))
returntrue;
returnbase.CanConvertFrom(context,sourceType);
}
//overridCanConvertTomethod
publicoverrideboolCanConvertTo(ITypeDescriptorContextcontext,TypedestinationType)
{
if(destinationType==typeof(DateTime))
returntrue;
returnbase.CanConvertTo(context,destinationType);
}
//overrideConvertTomethod
publicoverrideobjectConvertTo(ITypeDescriptorContextcontext,System.Globalization.CultureInfoculture,objectvalue,TypedestinationType)
{
if(destinationType==typeof(String)&&valueisDateTime)
{
stringformattedValue;
DateTimetemp=(DateTime)value;
formattedValue=temp.ToShortDateString();
returnformattedValue;
}
returnbase.ConvertTo(context,culture,value,destinationType);
}
//overrideConvertFrommethod
publicoverrideobjectConvertFrom(ITypeDescriptorContextcontext,System.Globalization.CultureInfoculture,objectvalue)
{
if(valueisstring)
{
try
{
DateTimedt;
CultureInfoci=CultureInfo.CreateSpecificCulture("en-US");
DateTime.TryParse((string)value,ci,DateTimeStyles.AssumeLocal,outdt);
returndt;
}
catch
{
MessageBox.Show("typeconversionerror");
}
}
returnbase.ConvertFrom(context,culture,value);
}
}

If you have any further problem, please feel free to let me know.

Best regards,

Bruce Zhou


Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, February 27, 2009 6:32 AM
Bruce Zhou,

Thank you very much for your reply. Actually, I got what I want from your suggestions explained above. But in the form of drop down box. What I want to say is, I want same masked text box controlin place of Property item field in the PropertyGrid. Can we not place masked text box control inside the property item field of PropertyGrid? So that, if I enter in the Masked text box control(i.e., Date Property field) the input will be taken to Date time property. Finally, date field editing in the propertygrid should be done only through masked text box.

I have tried to give a picture of it below.......
______________________________
|DateProperty Name |__/__/____ |
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Please, suggest in this. Thanks in advance.

Regard,
Kumar.
Kumar...  Friday, March 06, 2009 7:43 AM
Hi Kuma,

Actually, I know the very effect you want to achieve. It's exactly as the picture you give. I think it's possible to achieve that but much more complexer than the dropdown workaround.

To achieve the goal, we need to find the active TextBox in the PropertyGrid, and control the input. So we may need to extend the PropertyGrid and override OnSelectedGridItemChanged and OnSelectedObjectsChanged method.

Sure it requires lots of code to implement the logic. I just found anarticle in the codeproject which may guide you to the final success.

Best regards,
Bruce Zhou


Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, March 06, 2009 8:11 AM
Bruce Zhou,

Thanks for your quick reply.

Actually, I have tried by creating custom property grid by overrinding the functions as you said. I have used TextChanged event to control the text box input.I kept my logic in text box events such a way that it can handle only MM/DD/YYYY format. What if system date format changes? There are seven types of short date formats in regional and language options in control panel. In this case, I need to consider all the formats. Finally, I struck up here.

Instead of overriding events or raising text box events, cant we achieve this by using UITypeEditors and convertors? Is there any other way?

Please,suggest me.

Regards,
Kumar.
Kumar...  Friday, March 06, 2009 9:06 AM
Hi Kumar,

That's why I said it's much more complexer.I can't think an easy way for this. It's very common to extend UITypeEditor to implment the DropDown or ModelForm Editor for the Property at design time. But seldom doI see it is used to customize the TextBox in the PropertyGrid.

I think the default behavior is easy enough for the user to input time format data. If you think it's very important in your product, then you should implement it no matter how complex thecase will be. Otherwise, you can search if there is a third party PropertyGrid has implemented this feature.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, March 06, 2009 9:32 AM
HiBruce Zhou,

Thank you for your suggestions. My opinion is also same as you. I should go forthird party PropertyGrid or else I should tryfor other way.

But, I have learnt a lot from this discussion. Thank you very much.......

Regards,
Kumar.
  • Marked As Answer byKumar... Friday, March 06, 2009 10:23 AM
  •  
Kumar...  Friday, March 06, 2009 10:22 AM

You can use google to search for other answers

Custom Search

More Threads

• Win32 and C# Window Styles. Why different ?
• Codedomdesignerloader : Paste and redo problem
• Missing Flexgrids
• Hi I m Trying to publish my project using installShield with clickonce Deplyments project Problem in it
• Binary serialization to a shared store, is it possible?
• Error initializing form containing button with image
• Custom Property Editor Woes
• how to change border color to Gray for a Panel
• more than one value in listbox
• Can no longer drag Container-type controls with mouse in IDE