Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I restrict my Container Control to parent only certain types of Controls and vice-versa during design-time?

To restrict your Container Control to parent only certain types of controls, override as follows in your designer:

public class CustomContainerControlDesigner : ParentControlDesigner 
{ 
  public override bool CanParent( Control control ) 
  { 
    return control is TextBox; // only allow TextBox children 
  } 
}

To restrict your Control to get parented to by a certain type, do so in your Control's designer:

class CustomControlDesigner : ControlDesigner 
{ 
  public override bool CanBeParentedTo( IDesigner parentDesigner ) 
  { 
    // CustomControl can be only parented to CustomParent 
    return parentDesigner is CustomParentDesigner; 
    
    // alternatively: 
    return parentDesigner.Component is CustomParent; 
  } 
}  

Contributed from George Shepherd's Windows Forms FAQ