How to prevent adding of controls in design time

hvsecs

New member
Joined
Feb 14, 2019
Messages
4
Programming Experience
3-5
Good Morning - maybe you have another idea?

What I have:
I have created a form. This is the baseform, and another new form is based on this one.
Now I want to prevent controls from being added to the inherited form.

For this I have trapped the ControlAdded-event, and say this.Controls.Remove(e.Control). This works, but I get an error message saying, that the child is not a member ... I understand, that after the ControlAdded-Event some settings are done, but if the control is gone, the changes run into nirvana.

So I have added an async task, which removes the newly created control from the form without errors. But the place, where the control previously was added, is still highlighted, and in the properties-window the removed control is shown.

I have tried to set the focus to another control of the form, but this seems not possible.

Any idea, what I could do? I would prefer to not use the async task, but the eventargs have no cancel-property. I would be content, if I could "select" another control or the form itself in designview?

Many thanks !
 
Ok, I found a solution for the second part.

So I use the async task to remove it and then it's ok.

For those intereseted:


C#:
private ISelectionService  m_selectionService;
public override ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                base.Site = value;
                m_selectionService  = (ISelectionService)this.GetService( typeof( ISelectionService ) );
            }
        }
        public async void RemoveAddedControl(int Waittime, Control ctr)
        {
            await Task.Delay(Waittime);
            this.Controls.Remove(ctr);
            m_selectionService.SetSelectedComponents(new Form[] { this });

        }
        private void Form1_ControlAdded(object sender, ControlEventArgs e)
        {
            if (!(e.Control is AllowedControl))
            {
                e.Control.Visible = false;
                MessageBox.Show("Only allowed controls can be added...");
                RemoveAddedControl(1, e.Control);
            }
        }

Have a nice time
 
Not sure if this applies, but there is a way to filter Toolbox to only show items that can be added, this is done with a root designer and a custom ToolboxItemFilter, here is an example of that:
C#:
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
//add reference System.Design
namespace csWindowsFormsApp1
{
    [ToolboxItemFilter("", ToolboxItemFilterType.Custom)]
    public class MyDocumentDesigner : DocumentDesigner
    {
        protected override bool GetToolSupported(ToolboxItem tool)
        {
            return tool.TypeName == "System.Windows.Forms.Button";
        }
    }
}
The designer is applied like this:
C#:
[Designer(typeof(MyDocumentDesigner), typeof(IRootDesigner)), DesignerCategory("Form")]
public partial class Baseform : Form
In this example the designer for forms derived from Baseform will only show the Button control in Toolbox.
 
Back
Top Bottom