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. unity 4 Please check your configuration file and verify this type name.

    The problem is in you config file. You are mixing two concepts with some incorrect syntax. The <a ...

  2. childNodes、nodeName、nodeValue 以及 nodeType

    nodeName.nodeValue 以及 nodeType 包含有关于节点的信息. nodeName 属性含有某个节点的名称. 元素节点的 nodeName 是标签名称属性节点的 nodeName ...

  3. python file operation

    file.open(name[,mode[,buffering]]) 模式的类型有: r 默认只读 w     以写方式打开,如果文件不存在则会先创建,如果文件存在则先把文件内容清空(truncate ...

  4. Send an email with format which is stored in a word document

    1. Add a dll reference: Microsoft.Office.Interop.Word.dll 2. Add the following usings using Word = M ...

  5. 《腾讯敏捷框架TAPD》研究

    1         框架结构 1.1         产品 TAPD采用FDD模式开发,FDD即特征驱动开发. FDD的核心是面向产品的功能点,但这个功能点是从客户角度出发的,并不是从系统角度出来的. ...

  6. Spring Data Solr教程(翻译) 开源的搜索服务器

    Solr是一个使用开源的搜索服务器,它采用Lucene Core的索引和搜索功能构建,它可以用于几乎所有的编程语言实现可扩展的搜索引擎. Solr的虽然有很多优点,建立开发环境是不是其中之一.此博客条 ...

  7. C#.Net中的转义字符

    当声明一个字符串变量时有一些字符是不能以平常的方式包含在变量中的.为了解决这个问题,C#提供了两种不同的方法. 第一种方法是使用’转义序列’.例如,我们想得到如下的字符串 “Hello World H ...

  8. 手机App开发

    /* * 登录:输入 */ public void login(String user, String pwd, TextHttpResponseHandler responsehandler) { ...

  9. Linux启动过程详解 (转)

    启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...

  10. Oracle监控代理安装ITM(IBM Tivoli Monitoring)

    1 监控代理安装 2 1.1 安装 2 1.1.1 解压安装包 2 1.1.2 安装 2 1.2 配置 5 1.2.1 给Agent授权 5 1.2.2 配置Oracle Agent 10 目录 1  ...