I'm more or less looking for a best way to provide some of the common functionality for an UltraTextEditor in my application. I wanted the functionality to draw the rounded corners of the text box from another post on the forums so I'd thought I'd subclass the text box.
However, I want a different appearance when the control has focus. So to do this a different border colour will be used. The following code provides this functionality, but the way I create the Appearance in the constructor, and the visibility of the Appearances collection, the designer generates the code for this appearance in a form. It throws the following exception when the program is executed - as the Appearances is auto-generated:
An error occurred creating the form. See Exception.InnerException for details. The error is: Key already exists
I am looking for a best practice on how to handle my appearances - or other setting of other properties in the subclass.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Infragistics.Documents.Reports.Graphics; using Infragistics.Win; using Infragistics.Win.UltraWinEditors; namespace MyApp.Win.Controls { public class WMSUltraTextEditor : UltraTextEditor { private AppearanceBase defaultAppearence; public WMSUltraTextEditor() { Appearance appearenceFocus = new Appearance(); appearenceFocus.BorderColor = System.Drawing.Color.Green; appearenceFocus.Key = "Focus"; this.Appearances.Add(appearenceFocus); this.BorderStyle = Infragistics.Win.UIElementBorderStyle.Rounded3; this.UseOsThemes = DefaultableBoolean.False; } protected override void OnCreateControl() { base.OnCreateControl(); FixBorder(); defaultAppearence = this.Appearance; } private void FixBorder() { System.Drawing.Rectangle textRect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, this.Size); this.Region = DrawUtility.CreateBorderRegion(textRect, this.BorderStyle); } protected override void OnResize(EventArgs e) { base.OnResize(e); FixBorder(); } protected override void OnEnter(EventArgs e) { this.Appearance = this.Appearances["Focus"]; base.OnEnter(e); } protected override void OnLeave(EventArgs e) { base.OnLeave(e); this.Appearance = defaultAppearence; } } }
Any thoughts?
Thanks
Andez
Hi Andez,
The Appearances collection is public and gets serialized at design-time. So you are adding the appearance to the collection in the constructor and then when you bring up the form designer, the designer tries to add it again.
I think your best bet in this case is - don't use the Appearances collection. Just store your Appearance in a local member variable. If you need your users to have access to it, then expose it as a separate property.