限制在Panel上添加控件
如何限制在 C# 窗口控件的面板中添加控件? 我必须限制用户在设计时在面板中添加控件。
How can i restrict adding controls in Panel in C# window controls? I have to restrict user to add controls in a panel at design time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要限制可以添加到面板的控件类型或控件数量,您可以创建自己的面板子类,并在 Controls.Add 方法的重载中检查控件类型或控件计数。
编辑:重载 Controls.Add 方法并不像我想象的那么容易,但是您可以创建一个扩展 Panel 类的新类并重写 OnControlAdded 方法来检查添加的控件的类型。 像这样的东西应该有效:
If you want to limit the types of controls or number of controls one can add to the panel you can make your own subclass of the panel and check the Control type or Control count in an overload of the Controls.Add method.
Edit: Overloading the Controls.Add method was not as easy as I thought, but you can make a new class that extends the Panel class and override the OnControlAdded method to check the type of control that was added. Something like this should work:
将AllowDrop 设置为 false。
Set AllowDrop to false.