Question: I'm developing a C# component, I want to prevent the user from adding this component to the Form if the Form already has an instance of the component.

Answer: You can create a custom ComponentDesigner for your component, override its InitializeNewComponent() method to judge wether there's already an instance existes, if so, just return from this method. I write the following sample for your information:

[Designer(typeof(myComponentDesigner))]
class myComponent : Component
{
public myComponent(IContainer container)
{
// Add object to container's list so that
// we get notified when the container goes away
container.Add(this);
}
} class myComponentDesigner : ComponentDesigner
{
public override void InitializeNewComponent(System.Collections.IDictionary defaultValues)
{
IDesignerHost service = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (service == null) return;
ComponentCollection cc = service.Container.Components; int count = ;
foreach (Component c in cc)
{
if (c.GetType() == this.Component.GetType() )
{
count++;
if (count > )
{
service.Container.Remove(this.Component);
MessageBox.Show(
"You cannot add more than one instance of the myComponet!");
return;
}
}
} base.InitializeNewComponent(defaultValues);
}
}
 

Prevent Adding Component More than once的更多相关文章

  1. Blazor组件自做五 : 使用JS隔离封装Google地图

    Blazor组件自做五: 使用JS隔离封装Google地图 运行截图 演示地址 正式开始 1. 谷歌地图API 谷歌开发文档 开始学习 Maps JavaScript API 的最简单方法是查看一个简 ...

  2. GroupLayout 布局

    文档说明: 以下引自:Java™ PlatformStandard Ed. 7 public class GroupLayout extends Object implements LayoutMan ...

  3. [SinGuLaRiTy] COCI 2011~2012 #2

    [SinGuLaRiTy-1008] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 测试题目 对于所有的题目:Time Limit:1s   ...

  4. gitlab通过api创建组、项目、成员

    前戏 获取gitlab中admin用户的private_token Groups API 获取某个组的详细 curl --header "PRIVATE-TOKEN: *********&q ...

  5. 基于vue实现上下滑动翻页效果

    18年年底的时候,一直在做年度报告的H5页面,因为项目需要,需要实现上下滑动翻页,并且上滑的页面比正常页面的比例要缩小一定比例. 效果类似于http://www.17sucai.com/pins/de ...

  6. Vickers Vane Pump - Hydraulic Vane Pump Failure: Cavitation, Mechanical Damage

    One of our readers recently wrote to me about the following questions: “Recently, we purchased a sec ...

  7. hexo next主题深度优化(一),加入pjax功能。

    文章目录 背景: 进入正题 pjax初体验--instantclick 真正的pjax 第一步 第二步 第三步 第四步 专门基于hexo next主题的pjax(将丢失的js效果重现) 将下面讲到的提 ...

  8. Blazor组件自做八 : 使用JS隔离封装屏幕键盘kioskboard.js组件

    1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加kioskboard子文件夹,添加kioskboards.js文件 2.1 常规操作,懒加载js库, export function ...

  9. [Angular] Adding keyboard events to our control value accessor component

    One of the most important thing when building custom form component is adding accessbility support. ...

随机推荐

  1. 【转】PHP error_reporting() 错误控制函数功能详解

    定义和用法: error_reporting() 设置 PHP 的报错级别并返回当前级别.   函数语法: error_reporting(report_level)   如果参数 level 未指定 ...

  2. How to send Email using C#

    try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail. ...

  3. kindle相关工具

    里是与 Kindle 电子书相关的工具软件.它们可以帮助我们解决在日常使用电子书时所可能遇到的问题,比如 kindle 管理工具.kindle 转换工具.kindle电子书制作工具.kindle 推送 ...

  4. zookeeper Keepalived

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...

  5. smarty模板基础

    将前台后台隔离,前台控制显示,后台控制逻辑/内容,与cms类似 原理: 用户访问text.php页面,后台调用类smarty.class.php显示静态模板;

  6. .NET,Cookie,写Cookie,取Cookie

    Cookie是一段文本信息,在客户端存储 Cookie 是 ASP.NET 的会话状态将请求与会话关联的方法之一.Cookie 也可以直接用于在请求之间保持数据,但数据随后将存储在客户端并随每个请求一 ...

  7. IOS开发-UITextField代理常用的方法总结

    1.//当用户全部清空的时候的时候 会调用 -(BOOL)textFieldShouldClear:(UITextField *)textField: 2.//可以得到用户输入的字符 -(BOOL)t ...

  8. js为DIV动态设置id属性

    <script> var objs=document.getElementById("iproduct").getElementsByTagName("div ...

  9. 【Oracle学习笔记-4】内连接和外连接的区别

    参考链接(非常棒) 摘要 下面主要以两个例子进行说明: 例子1: 表A结构如下: select * from A | 表B结构如下: select * from B 两个表要做连接,就必须有个连接字段 ...

  10. 从IT匹配业务如何走向IT引领业务

    http://mp.weixin.qq.com/s?__biz=MjM5Njk2Mzg0MQ==&mid=200105892&idx=1&sn=cd9c155d09e8b975 ...