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 998497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:12:16+00:00 2026-05-16T07:12:16+00:00

I’m using PropertyGrid to edit an object containing a collection. Collection is edited using

  • 0

I’m using PropertyGrid to edit an object containing a collection.
Collection is edited using the CollectionEditor.
I have to make sure elements in collection are unique.

How can I add validation to CollectionEditor:

  1. By either overloading CollectionEditor’s OnFormClosing
  2. Or adding validation for creating/editing items?
  • 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-16T07:12:17+00:00Added an answer on May 16, 2026 at 7:12 am

    You can create your own collection editor, and hook into events on the default editor’s controls. You can use these events to, say, disable the OK button. Something like:

    public class MyCollectionEditor : CollectionEditor
    {
        private static Dictionary<CollectionForm, Button> okayButtons 
            = new Dictionary<CollectionForm, Button>();
    
        // Inherit the default constructor from CollectionEditor
        public MyCollectionEditor(Type type) 
            : base(type) 
        {
        }
    
        // Override this method in order to access the containing user controls
        // from the default Collection Editor form or to add new ones...
        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm collectionForm = base.CreateCollectionForm();
            collectionForm.FormClosed += 
                new FormClosedEventHandler(collectionForm_FormClosed);
            collectionForm.Load += new EventHandler(collectionForm_Load);
    
            if (collectionForm.Controls.Count > 0)
            {
                TableLayoutPanel mainPanel = collectionForm.Controls[0] 
                    as TableLayoutPanel;
                if ((mainPanel != null) && (mainPanel.Controls.Count > 7))
                {
                    // Get a reference to the inner PropertyGrid and hook 
                    // an event handler to it.
                    PropertyGrid propertyGrid = mainPanel.Controls[5] 
                        as PropertyGrid;
                    if (propertyGrid != null)
                    {
                        propertyGrid.PropertyValueChanged += 
                            new PropertyValueChangedEventHandler(
                                propertyGrid_PropertyValueChanged);
                    }
    
                    // Also hook to the Add/Remove
                    TableLayoutPanel buttonPanel = mainPanel.Controls[1] 
                        as TableLayoutPanel;
                    if ((buttonPanel != null) && (buttonPanel.Controls.Count > 1))
                    {
                        Button addButton = buttonPanel.Controls[0] as Button;
                        if (addButton != null)
                        {
                            addButton.Click += new EventHandler(addButton_Click);
                        }
                        Button removeButton = buttonPanel.Controls[1] as Button;
                        if (removeButton != null)
                        {
                            removeButton.Click += 
                                new EventHandler(removeButton_Click);
                        }
                    }
    
                    // Find the OK button, and hold onto it.
                    buttonPanel = mainPanel.Controls[6] as TableLayoutPanel;
                    if ((buttonPanel != null) && (buttonPanel.Controls.Count > 1))
                    {
                        Button okayButton = buttonPanel.Controls[0] as Button;
                        if (okayButton != null)
                        {
                            okayButtons[collectionForm] = okayButton;
                        }
                    }
                }
            }
            return collectionForm;
        }
    
        private static void collectionForm_FormClosed(object sender, 
            FormClosedEventArgs e)
        {
            CollectionForm collectionForm = (CollectionForm)sender;
            if (okayButtons.ContainsKey(collectionForm))
            {
                okayButtons.Remove(collectionForm);
            }
        }
    
        private static void collectionForm_Load(object sender, EventArgs e)
        {
            ValidateEditValue((CollectionForm)sender);
        }
    
        private static void propertyGrid_PropertyValueChanged(object sender,
            PropertyValueChangedEventArgs e)
        {
            ValidateEditValue((CollectionForm)sender);
        }
    
        private static void addButton_Click(object sender, EventArgs e)
        {
            Button addButton = (Button)sender;
            ValidateEditValue((CollectionForm)addButton.Parent.Parent.Parent);
        }
    
        private static void removeButton_Click(object sender, EventArgs e)
        {
            Button removeButton = (Button)sender;
            ValidateEditValue((CollectionForm)removeButton.Parent.Parent.Parent);
        }
    
        private static void ValidateEditValue(CollectionForm collectionForm)
        {
            if (okayButtons.ContainsKey(collectionForm))
            {
                Button okayButton = okayButtons[collectionForm];
                IList<MyClass> items = collectionForm.EditValue as IList<MyClass>;
                okayButton.Enabled = MyCollectionIsValid(items);
            }
        }
    
        private static bool MyCollectionIsValid(IList<MyClass> items)
        {
            // Perform validation here.
            return (items.Count == 2);
        }
    
    }
    

    You will also need to add an Editor attribute to you collection:

    class MyClass
    {
      [Editor(typeof(MyCollectionEditor), 
              typeof(System.Drawing.Design.UITypeEditor))]
      List<Foo> MyCollection
      {
        get; set;
      }
    }
    

    NOTE: I found that the value of items in removeButton_Click was not correct – so some tweaking may need to take place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.