Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build a Property Editor That Can Edit Any .NET Type : Page 5

This article describes how to create a Windows Forms control that leverages the power of the .NET Framework's UITypeEditor and TypeConverter classes at runtime to create a control that can display and edit the values of all .NET types.


advertisement
Changing the Edited Value
You still have to write code to drop the UITypeEditor when a user clicks the button. But before that, you must first define a property to hold the edited value. The following Value property is of type object because it must be able to hold any kind of value.

private object currentValue; public object Value { get { return currentValue; } set { if (value != null && !EditedType.IsAssignableFrom( value.GetType())) throw new InvalidCastException( "Bad value type."); currentValue = value; UpdateTextBoxWithValue(); if (paintValueSupported) Invalidate(true); OnValueChanged(EventArgs.Empty); } }

The property first verifies that the entered or selected value can be assigned to the type stored in the EditedType property—if not, the control throws an exception. Whenever the value changes, the property code updates the text in the text box using the UpdateTextBoxWithValue method. Then, if the current value supports being painted, it repaints the control by calling the Invalidate method. Finally, it raises a ValueChanged event to notify control users that the value has changed.

To set the correct text in the text box, the UpdateTextBoxWithValue method uses the TypeConverter (stored in the converter field) to convert the value to a string:

private void UpdateTextBoxWithValue() { textBox.Text = GetValueAsText(currentValue); } string GetValueAsText(object value) { if (value == null) return string.Empty; if (value is string) return (string)value; try { if (converter != null && converter.CanConvertTo(typeof(string))) return converter.ConvertToString(value); } catch (Exception) {} return value.ToString(); }

The type converter's ConvertToString method returns a text value for that type. If the TypeConverter cannot convert the value to a string, the code uses the ToString method that is defined for every .NET object as a fallback solution.

The TypeConverter converts the stored current value to a string for display purposes, but when the user enters or changes the text in the property text box you also have to convert that text back into a valid object. You can do that with the TypeConverter as well.

Validate the text entered by the user in the text box by overriding the OnValidating method of the control. Validating there ensures that the Value property will be up-to-date when the control loses the focus, and lets you cancel the validation when the text is not valid. Here's the OnValidating method code:



protected override void OnValidating( CancelEventArgs e) { ... base.OnValidating(e); if (!ValidateText()) e.Cancel = true; }

The ValidateText method used above is defined as:

bool ValidateText() { if (!ValidateText(textBox.Text)) { UpdateTextBoxWithValue(); return false; } else return true; } private bool ValidateText(string text) { object value = null; try { if (converter != null && converter.CanConvertFrom(typeof(string))) value = converter.ConvertFromString(null, CultureInfo.CurrentCulture, text); } catch (Exception) {} return value != null ? ValidateValue(value) : false; } private bool ValidateValue(object value) { try { ... Value = value; } catch (Exception) { return false; } return true; }

The call to ValidateText() with no parameters calls the second version of the ValidateText method—the one that takes a string parameter—passing the text from the TextBox as the parameter value. This method again uses the type converter to try to convert the text into an object using the TypeConverter.ConvertFromString method. If the result is valid it stores the value back into the Value property.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date