Solved

C# How to override Fom.Controls collection

Posted on 2010-08-15
10
1,102 Views
Last Modified: 2013-12-17
Hi Everyone,

I have a custom form component I have written that has a docked panel in the form's body.

When an object is added to the Form.Controls collection it currently adds it to the same collection that contains the panel, what I want is when someone calls Form.Controls.Add(object) it adds the supplied object to the Panel's Controls collection.

I am aware that I could just write Form.Panel.Controls.Add but that defeats what i am trying to accomplish.

Thanks,

Xavier.
0
Comment
Question by:locdang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 33443286
I'm not sure if it worth doing what you want to achieve. You may try doing something like overriding the Controls property. Really, you can't overide it, you may shadow it - see the snippet 1. In other words, you have to inherit from the Form class, e.g., MyForm class.

The reason why I added a delegate - this MyForm class has no idea about Panel 1 yet - so you may set the delegate after Panel is added. You may have a look at the snippet 2. MyForm1 class is a subclass of MyForm.

I didn't get it working - I believe, too much complexity and troubles. But this is one of possible ways.

Not sure why you need this but may I propose a simpler solution? OK, just add a "Add(Control ctrl)" method to your form class, and invoke in it :

this.Panel1.Controls(ctrl);
===snippet 1
    public class MyForm : System.Windows.Forms.Form
    {
        public delegate Control.ControlCollection getControlsCollectionDelegate(); 
        private getControlsCollectionDelegate controlsDelegate = null;

        public new Control.ControlCollection Controls 
        {
            get
            {

                if (controlsDelegate != null)
                {
                    Control.ControlCollection ctrls = controlsDelegate.Invoke();
                    return ctrls;
                }
                else
                {
                    return this.Controls;
                }
            }
        }

        public void setControlsDelegate(getControlsCollectionDelegate ctrlDelegate)
        {
            controlsDelegate = ctrlDelegate;
        }

        public MyForm() : base()
        {
        }

    }


// snippet 2

        public MyForm1()
        {

            MyForm.getControlsCollectionDelegate controlDelegate = new getControlsCollectionDelegate( getMyFormControls);
            this.setControlsDelegate(controlDelegate);
            InitializeComponent();
        }

        private System.Windows.Forms.Control.ControlCollection getMyFormControls()
        {
            return (System.Windows.Forms.Control.ControlCollection)this.panel1.Controls;
        }

Open in new window

0
 
LVL 3

Accepted Solution

by:
yanoch earned 500 total points
ID: 33443305
Hope this help
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
          
        }

        protected override Control.ControlCollection CreateControlsInstance()
        {

            return new CustomControlCollection(this);
        }


        public class CustomControlCollection : Control.ControlCollection
        {

            public CustomControlCollection(Control owner) : base(owner)
            {
            }

            public override void Add(System.Windows.Forms.Control value)
            {
                foreach (Control ctrl in this)
                    if (ctrl is Panel) // Panel should be replaced by your panel class type
                    {
                        ctrl.Controls.Add(value);
                        return;
                    }


                base.Add(value);
            }

            public override void Remove(System.Windows.Forms.Control value)
            {
                base.Remove(value);
            }
        }       

    }

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 33443335
yanoch, how you override the Form. Controls so that it returns panel controls?
0
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
LVL 1

Author Comment

by:locdang
ID: 33443342
I dont want to return / add panel controls, I want to override all Form.Controls.Add method involkes and add the control to the Panel's control collection rather than the Form's control collection.

Sorry for the misunderstanding. :)

Xavier.
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33443354
Test my snippet, this is exactly what it is doing. If you have something of the type panel on the form and you call yourform.controls.add it will be added to the panel and not the form.
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33443367
In fact, I replaced the current control collection by my custom contr collection to by able to get an hand on the controls.add
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33443371
Sorry, i forgot, you just can't override controls.add.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 33443412
Agree, "you just can't override controls.add".
Moreover, I understood really what you wanted but you do not understand consequences and implications of what you want to do. Try to think: if you add controls to the panel, how you add the panel to the form?! If you can't do that - you won't see any controls on the form. Also - you can't replace the Controls collection with any custom collection - it's "readonly" . And so on. So the only possible way is to override the form class but I'm noit 100 % sure it will be successful.
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33443446
It is working. Try it. The panel is added to the form. And the controls are added to the panel. Look at the override Add method.
0
 
LVL 1

Author Closing Comment

by:locdang
ID: 33443454
Works perfectly, Thanks :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question