Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4332638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:17:03+00:00 2026-05-21T10:17:03+00:00

I’m experimenting around with writing custom WinForms components and I wrote a couple of

  • 0

I’m experimenting around with writing custom WinForms components and I wrote a couple of simple validator components for use with a subclass of ErrorProvider that automatically hooks up validation events. All these components can be added to a form and hooked up using only the designer, thanks to IExtenderProvider.

Now in trying to go up one level, I’m trying to get a composite validator to be usable with the designer. I can get it up and working with code, but that’s really easy. I’d like to get it to work in a designer-only way.

My difficulty resides in exposing a property that is a collection of other validators that are in the same form. The validators all inherit directly from Component, and implement a IControlValidator interface. I’m open to changing this to have them inherit from a ValidatorComponent base class if it helps.

I thought of a couple solutions, but either I don’t like them, or I can’t get them to work:

  1. Make the validators into invisible controls, and the have composite validator contain them, similar to what a Panel does;

    This one I don’t like because it is more of a hack, and having to juggle them among true controls just feels wrong;

  2. Use a collection editor, as you use for toolbars;

    I looked around the web and found a couple of articles about this, but I couldn’t get it to work. At least without building my own editor form, which would be too much of a hassle for an experiment project.

    I admit I didn’t spend much time trying this, because I realized using the standard CollectionEditor would lock me down to using a fixed set of validator types (it would, wouldn’t it?).

    I also thought of creating a simple ValidatorReference class with a single property of type IControlValidator and use that as the element type for a simple collection editor. I would then add one of these, and in its property grid set the property to an existing validator component. This one seems easy to get working, but loses its appeal because it is such an obvious hack.

Anyone has any other ideas? Is there something I’m missing and this is actually something simple?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:17:03+00:00Added an answer on May 21, 2026 at 10:17 am

    Why not creating an editor to do this???
    You think it sounds an overkill, but actually it is not.

    I will demonstrate with a sample.

    Sample description

    In this sample I will be creating a control named ButtonActivityControl that is abled to make multiple references to other controls in the same form, using a property called Buttons, that is an array of type Button (i.e. Button[]).

    The property is marked with a custom editor, that makes it easy to reference the controls in the page. The editor shows a form that consists of a checked list box, that is used to select multiple controls that are in the very same form.

    Steps to create the sample

    1) a Form called ReferencesCollectionEditorForm

    • place a CheckedListBox inside it,
    • place an ‘Ok’ button
    • place the following code in the form class

    Code of ReferencesCollectionEditorForm:

    public partial class ReferencesCollectionEditorForm : Form
    {
        public ReferencesCollectionEditorForm(Control[] available, Control[] selected)
        {
            this.InitializeComponent();
            List<Control> sel = new List<Control>(selected);
            this.available = available;
            if (available != null)
                foreach (var eachControl in available)
                    this.checkedListBox1.Items.Add(new Item(eachControl),
                        selected != null && sel.Contains(eachControl));
        }
    
        class Item
        {
            public Item(Control ctl) { this.control = ctl; }
            public Control control;
            public override string ToString()
            {
                return this.control.GetType().Name + ": " + this.control.Name;
            }
        }
    
        Control[] available;
    
        public Control[] Selected
        {
            get
            {
                List<Control> selected = new List<Control>(this.available.Length);
                foreach (Item eachItem in this.checkedListBox1.CheckedItems)
                    selected.Add(eachItem.control);
                return selected.ToArray();
            }
        }
    }
    

    2) an UITypeEditor

    Code of ReferencesCollectionEditor:

    public class ReferencesCollectionEditor : UITypeEditor
    {
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            List<Control> available = new List<Control>();
    
            ButtonActivityControl control = context.Instance as ButtonActivityControl;
            IDesignerHost host = provider.GetService(typeof(IDesignerHost)) as IDesignerHost;
            IComponent componentHost = host.RootComponent;
            if (componentHost is ContainerControl)
            {
                Queue<ContainerControl> containers = new Queue<ContainerControl>();
                containers.Enqueue(componentHost as ContainerControl);
                while (containers.Count > 0)
                {
                    ContainerControl container = containers.Dequeue();
                    foreach (Control item in container.Controls)
                    {
                        if (item != null && context.PropertyDescriptor.PropertyType.GetElementType().IsAssignableFrom(item.GetType()))
                            available.Add(item);
                        if (item is ContainerControl)
                            containers.Enqueue(item as ContainerControl);
                    }
                }
            }
    
            // collecting buttons in form
            Control[] selected = (Control[])value;
    
            // show editor form
            ReferencesCollectionEditorForm form = new ReferencesCollectionEditorForm(available.ToArray(), selected);
    
            form.ShowDialog();
    
            // save new value
            Array result = Array.CreateInstance(context.PropertyDescriptor.PropertyType.GetElementType(), form.Selected.Length);
            for (int it = 0; it < result.Length; it++)
                result.SetValue(form.Selected[it], it);
            return result;
        }
    }
    

    3) a control that uses other controls in the same form

    Code of custom control:

    public class ButtonActivityControl : Control, ISupportInitialize
    {
        [Editor(typeof(ReferencesCollectionEditor), typeof(UITypeEditor))]
        public Button[] Buttons { get; set; }
    
        Dictionary<Button, bool> map = new Dictionary<Button, bool>();
    
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.White, e.ClipRectangle);
            if (this.Site != null) return; // this code is needed otherwise designer crashes when closing
            int h = e.ClipRectangle.Height / this.Buttons.Length;
            int top = 0;
            foreach (var button in this.Buttons)
            {
                e.Graphics.FillRectangle(map[button] ? Brushes.Black : Brushes.White, new Rectangle(0, top, e.ClipRectangle.Width, h));
                top += h;
            }
            base.OnPaint(e);
        }
    
        void ISupportInitialize.BeginInit()
        {
        }
    
        void ISupportInitialize.EndInit()
        {
            if (this.Site != null) return; // this is needed so that designer does not change the colors of the buttons in design-time
            foreach (var button in this.Buttons)
            {
                button.Click += new EventHandler(button_Click);
                button.ForeColor = Color.Blue;
                map[button] = false;
            }
        }
    
        void button_Click(object sender, EventArgs e)
        {
            map[(Button)sender] = !map[(Button)sender];
            this.Invalidate();
        }
    }
    

    Now create a form that will contain the custom control, place some buttons on it, and then place a ButtonActivityControl on it. The custom control has a property called Buttons, that is editable.

    That’s it!!

    No reason to fear custom Editors… and not so complex….
    dit it in half an hour.

    I think this is the answer… that is, I think it is! =) Maybe I didn’t understand the question well… but thats the best one can do: trying to help others!

    EDIT

    This code is needed in the ReferencesCollectionEditor:

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.