좋은 하루.C에서 사용자가 내 컨트롤의 컨트롤 컬렉션을 보호합니다.
public class MySuperControl : Control
{
private List<MySmallControl> _smallControls;
public MySuperControl()
{
//_smallControls = ... //creating of small controls
this.Controls.AddRange(_smallControls);
}
class MySmallControl : UserControl
{
//contains button and checkbox
}
}
그것은 모든 권리이지만, MySuperControl의 사용자 컨트롤 속성에 액세스 할 수있는 작은 컨트롤의 위치를 변경할 수 있습니다, 심지어 : 나는 예를 들어, 다른 추가 된 UserControls의 그룹을 포함하는 내 컨트롤을 만들 컬렉션에서 삭제하십시오! 캡슐화가 깨졌습니다! 사용자의 나쁜 행동의 예 :
방법 MySuperControl의 내부 컨트롤을 보호하기 위해 :MySuperControl c=new MySuperControl();
c.Controls.Clear();
또는
MySuperControl c=new MySuperControl();
foreach (Control smallControl in c.Controls)
{
smallControl.Location = new Point(999, 999);
}
이 MySuperControl를 충돌이 발생할 것인가? 감사.
그러나이 방법은 하위 컨트롤의 Control.Parent 속성을 변경하거나 PropertyGrid를 통해 ControlCollection에 액세스하는 것으로 간주되지 않습니다. 너무 많은 문제 ... 예를 들어, MySuperControl.Controls_2 속성을 사용하는 것이 훨씬 더 좋지만 구현 방법을 모르겠습니다. – Feofilakt