UserControl does not save values when form closes. Edited with custom UITypeEditor

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
HI!
So what Im about to post is just a test and my first attempt in creating a custom control whit a custom design time editor so please be tolerant with the state of my code ;)
So I have created a GradientPanel, which inherits from panel:

<div style="color:black; background-color:white
<pre> <span style="color:blue public <span style="color:blue class GradientPanel : Panel
{

<span style="color:blue private Gradient _PanelGradient = <span style="color:blue new Gradient();

[Category(<span style="color:#a31515 "Gradient")]
[Editor(<span style="color:blue typeof(GradientEditor),<span style="color:blue typeof(System.Drawing.Design.UITypeEditor))]
[DesignerSerializationVisibility( DesignerSerializationVisibility.Content)]
<span style="color:blue public Gradient PanelGradient
{
<span style="color:blue get { <span style="color:blue return _PanelGradient; }
<span style="color:blue set { _PanelGradient = value;}
}
<span style="color:blue private Border _borderGradient = <span style="color:blue new Border();
[Category(<span style="color:#a31515 "Gradient")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
<span style="color:blue public Border BorderGradient
{
<span style="color:blue get { <span style="color:blue return _borderGradient; }
<span style="color:blue set { _borderGradient = value; }
}
<span style="color:green // and so on
}
[/code]


Here is the custom UITypeEditor

<div style="color:black; background-color:white
<pre>IWindowsFormsEditorService editorService = <span style="color:blue null;
<span style="color:blue public <span style="color:blue override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
<span style="color:blue return UITypeEditorEditStyle.Modal;
}
<span style="color:blue public <span style="color:blue override <span style="color:blue object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, <span style="color:blue object value)
{
<span style="color:blue if (provider != <span style="color:blue null)
{
editorService =
provider.GetService(
<span style="color:blue typeof(IWindowsFormsEditorService))
<span style="color:blue as IWindowsFormsEditorService;
}

<span style="color:blue if (editorService != <span style="color:blue null)
{
GradientEditorForm selectionControl =
<span style="color:blue new GradientEditorForm(
(Gradient)value,
editorService);

editorService.ShowDialog(selectionControl);

value = selectionControl.editingGradient;
<span style="color:green //((System.Windows.Forms.Control)context.Instance).Width += 1;
<span style="color:green //((System.Windows.Forms.Control)context.Instance).Width -= 1;
((System.Windows.Forms.Control)context.Instance).Invalidate();
selectionControl.Dispose();
selectionControl = <span style="color:blue null;
}

<span style="color:blue return value;
}
}
[/code]


<br/>
And here is the form for editing:

<div style="color:black; background-color:white
<pre><span style="color:blue public Gradient editingGradient;
<span style="color:blue private IWindowsFormsEditorService editorService = <span style="color:blue null;
<span style="color:blue private Rectangle PaintCanvas = <span style="color:blue new Rectangle(10, 10, 200, 75);

<span style="color:blue public GradientEditorForm(Gradient gradientToEdit, IWindowsFormsEditorService editorService)
{

InitializeComponent();
<span style="color:blue this.editingGradient = gradientToEdit;
<span style="color:blue this.editorService = editorService;

cb_Mode.DataSource = Enum.GetValues(<span style="color:blue typeof(LinearGradientMode));
cb_Mode.SelectedItem = editingGradient.GradientMode;

}
<span style="color:blue private <span style="color:blue void GradientEditorForm_Load(<span style="color:blue object sender, EventArgs e)
{
AttachMarkers();

}
<span style="color:blue private <span style="color:blue void AttachMarkers()
{
<span style="color:blue for (<span style="color:blue int i = 0; i < editingGradient.Positions.Count;i++ )
{
Marker m = <span style="color:blue new Marker(editingGradient.Positions,Color.FromArgb(editingGradient.Colors), PaintCanvas,i);

<span style="color:blue this.Controls.Add(m.PointMarker);
m.PointMarker.BringToFront();
m.OnDataChanged += <span style="color:blue new Marker.OnDataChangedEvent(m_OnDataChanged);
}
}

<span style="color:blue void m_OnDataChanged(Marker sender)
{
editingGradient.Positions[sender.Index] = sender.Position;
editingGradient.Colors[sender.Index] =sender.Color.ToArgb();
<span style="color:blue this.Invalidate();
}


<span style="color:blue protected <span style="color:blue override <span style="color:blue void OnPaint(PaintEventArgs e)
{
editingGradient.Draw(PaintCanvas, e.Graphics, 0, 0);
<span style="color:blue base.OnPaint(e);
}

<span style="color:blue private <span style="color:blue void cb_Mode_SelectionChangeCommitted(<span style="color:blue object sender, EventArgs e)
{
editingGradient.GradientMode = (LinearGradientMode)cb_Mode.SelectedItem;
<span style="color:blue this.Invalidate(<span style="color:blue true);
}


}
[/code]

And here is the Gradient.cs

<pre lang="x-c# public class Gradient
{
public delegate void OnPropertyChangedEvent();
public event OnPropertyChangedEvent OnChanged;
private void InvokeChanged()
{
if (OnChanged != null)
OnChanged();
}

private LinearGradientMode _gradientMode = LinearGradientMode.Vertical;
public LinearGradientMode GradientMode { get { return _gradientMode; } set { _gradientMode = value; } }

[NonSerialized]
private ColorBlend _colorBlend = new ColorBlend();

private List<int> _colors = new List<int>() { Color.White.ToArgb(), Color.Red.ToArgb(), Color.Black.ToArgb() };
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<int> Colors
{
get { return _colors; }
set { _colors = value; InvokeChanged(); }
}
private List<float> _positions = new List<float>() { 0, 0.5f, 1 };
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<float> Positions
{
get { return _positions; }
set
{
_positions = value; InvokeChanged();
}
}


private List<Color> r_Colors
{
get
{
List<Color> temp = new List<Color>();
for (int i = 0; i < _colors.Count; i++)
{
temp.Add(Color.FromArgb(_colors));
}
return temp;
}
}
public Gradient()
{
}

public void Draw(Rectangle Bounds, Graphics g, float BorderWidth, float CornerRadius)
{
try
{
if (Bounds.Width > 0 && Bounds.Height > 0)
{

_colorBlend.Colors = r_Colors.ToArray();
_colorBlend.Positions = _positions.ToArray();
LinearGradientBrush Brush = new LinearGradientBrush(Bounds, Color.White, Color.Black, GradientMode);
Brush.InterpolationColors = _colorBlend;
Art.Fill(g, Bounds, CornerRadius, Brush, BorderWidth);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}[/code]
<br/>
<br/>

<br/>
SO the problem is that it works perfect to change the gradient for the panel but neither the position of a gradient marker or the color gets stored, ie serialized....

As soon as I close the form all the changes made are gone.....

Any Ideas??
<br/>

View the full article
 
Back
Top