locked
Problem with click event in custom UITypeEditor RRS feed

  • Question

  • Hi!

    I have created a custom UITypeEditor for creating a gradient backcolor on a component.

    The Editor shows up nicely in the propertygrid. 

    But for some reason each time i open the editor from the property window it duplicates my events.

    I have a label in the editor in which on click will display a color select dialog but when o close the editor and opens it again it show the color select dialog a second time hence duplicated my onclick event for the label.

     

    This is my code for the usercontrol displayed by the Editor:

     public GradientEditorControl(Gradient EditGradient, IWindowsFormsEditorService editorService)
            {
                InitializeComponent();
                // Cache the gradient value provided by the 
                // design-time environment.
                this.gradientValue = EditGradient;
                this.editorService = editorService;
    
                First_Marker.BackColor = GradientValue.Markers[0].GradientColor;
                Last_Marker.BackColor = GradientValue.Markers[GradientValue.Markers.Count - 1].GradientColor;
                First_Marker.Click += new EventHandler(First_Marker_Click);
                Last_Marker.Click += new EventHandler(Last_Marker_Click);
                for (int i = 1; i < GradientValue.Markers.Count - 1; i++)
                {
                    //GradientValue.Markers[i].Editable
                    this.Controls.Add(GradientValue.Markers[i]);
                    GradientValue.Markers[i].BringToFront();
                    GradientValue.Markers[i].Location = new Point(Convert.ToInt32(GradientValue.Markers[i].Position*194+34), 36);
                    GradientValue.Markers[i].Move += new EventHandler(GradientEditorControl_Move);
                    GradientValue.Markers[i].DoubleClick += new EventHandler(GradientEditorControl_DoubleClick);
                    
                }
                this.Invalidate(true);
            }
    
            void GradientEditorControl_DoubleClick(object sender, EventArgs e)
            {
                GradientMarker m = (GradientMarker)sender;
                  ColorSelect.Color = m.GradientColor;
                  if (ColorSelect.ShowDialog() == DialogResult.OK)
                  {
                      m.GradientColor = ColorSelect.Color;
                      this.Invalidate(true);
                  }
            }
    
    
    
    

    How come it register the event each time i open the editor, and how do i get rid of it...?

    Friday, October 14, 2011 9:23 AM

Answers

  • Hmmm i think i solved it by adding a function call from the derived UITypeEditor class..

      selectionControl.UnregisterEvents();

    That calls the usercontrol and unregisters any events...

     

     

    internal class GradientUIEditor : UITypeEditor
        {
           
           public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
           {
               IWindowsFormsEditorService editorService=null;
               if (provider != null)
               {
                   editorService =
                       provider.GetService(
                       typeof(IWindowsFormsEditorService))
                       as IWindowsFormsEditorService;
               }
    
               if (editorService != null)
               {
                   GradientEditorControl selectionControl =
                       new GradientEditorControl(
                       (Gradient)value,
                       editorService);
    
                   editorService.DropDownControl(selectionControl);
    
                   value = selectionControl.GradientValue;
                   
                   selectionControl.UnregisterEvents();
               }
    
               return value;
               //return base.EditValue(context, provider, value);
           }
           public override bool GetPaintValueSupported(ITypeDescriptorContext context)
           {
               return true;
           }
           public override void PaintValue(PaintValueEventArgs e)
           {
               
               (ico_gradient).Draw(e.Bounds, e.Graphics, 0, 0);
               base.PaintValue(e);
           }
        }
    


     

    • Marked as answer by Martin_Xie Tuesday, October 18, 2011 2:37 AM
    Friday, October 14, 2011 9:42 AM

All replies

  • Hmmm i think i solved it by adding a function call from the derived UITypeEditor class..

      selectionControl.UnregisterEvents();

    That calls the usercontrol and unregisters any events...

     

     

    internal class GradientUIEditor : UITypeEditor
        {
           
           public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
           {
               IWindowsFormsEditorService editorService=null;
               if (provider != null)
               {
                   editorService =
                       provider.GetService(
                       typeof(IWindowsFormsEditorService))
                       as IWindowsFormsEditorService;
               }
    
               if (editorService != null)
               {
                   GradientEditorControl selectionControl =
                       new GradientEditorControl(
                       (Gradient)value,
                       editorService);
    
                   editorService.DropDownControl(selectionControl);
    
                   value = selectionControl.GradientValue;
                   
                   selectionControl.UnregisterEvents();
               }
    
               return value;
               //return base.EditValue(context, provider, value);
           }
           public override bool GetPaintValueSupported(ITypeDescriptorContext context)
           {
               return true;
           }
           public override void PaintValue(PaintValueEventArgs e)
           {
               
               (ico_gradient).Draw(e.Bounds, e.Graphics, 0, 0);
               base.PaintValue(e);
           }
        }
    


     

    • Marked as answer by Martin_Xie Tuesday, October 18, 2011 2:37 AM
    Friday, October 14, 2011 9:42 AM
  • Hi Tobias.K,

    I’m very glad to hear that you worked out this issue.

    Thanks for sharing your solutions here. It will be very beneficial for other community members who have similar questions.

     

    If you have any difficulty in future programming, you are welcome to post here again.

     


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 17, 2011 5:27 AM