Prevent Adding Component More than once

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 = 0;
        foreach (Component c in cc)
        {
            if (c.GetType() == this.Component.GetType() )
            {
                count++;
                if (count > 1)
                {
                    service.Container.Remove(this.Component);
                    MessageBox.Show(
                       "You cannot add more than one instance of the myComponet!");
                    return;
                }
            }
        }

        base.InitializeNewComponent(defaultValues);
    }
}
时间: 2024-10-02 11:50:26

Prevent Adding Component More than once的相关文章

[SinGuLaRiTy] COCI 2011~2012 #2

[SinGuLaRiTy-1007] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 测试题目 对于所有的题目:Time Limit:1s  |  Memory:256 MB 第一题:考试得分(score)[najboljih] [题目描述] 某单位举办了一次考试,考试有8道题,每道题的得分不一样.选手可以随便做,但最后只统计5道题的得分.现在给出选手每道题的得分,求出他最后的得分最大是多少? [输入] 8行,每行一个正整数X(0<=X<

用匈牙利算法求二分图的最大匹配

转载大神的!! 什么是二分图,什么是二分图的最大匹配,这些定义我就不讲了,网上随便都找得到.二分图的最大匹配有两种求法,第一种是最大流(我在此假设读者已有网络流的知识):第二种就是我现在要讲的匈牙利算法.这个算法说白了就是最大流的算法,但是它跟据二分图匹配这个问题的特点,把最大流算法做了简化,提高了效率.匈牙利算法其实很简单,但是网上搜不到什么说得清楚的文章.所以我决定要写一下. 最大流算法的核心问题就是找增广路径(augment path).匈牙利算法也不例外,它的基本模式就是: 初始时最大匹

[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. The component should be focusable. we can achieve this by adding 'tabindex="0"': <div tabindex="0" > Add some css class for foucs

[React Router] Prevent Navigation with the React Router Prompt Component

In this lesson we'll show how to setup the Prompt component from React Router. We'll prompt with a static message, as well as a dynamic method with the message as a function. Finally we'll show that you can return true from the message as a function

Obfuscating computer code to prevent an attack

A method and system for obfuscating computer code of a program to protect it from the adverse effects of malware is provided. The obfuscation system retrieves an executable form of the computer code. The obfuscation system then selects various obfusc

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

Adding Cache-Control headers to Static Files in ASP.NET Core

Thanks to the ASP.NET Core middleware pipeline, it is relatively simple to add additional HTTP headers to your application by using custom middleware. One common use case for this is to add caching headers. Allowing clients and CDNs to cache your con

[Angular2 Form] Create custom form component using Control Value Accessor

//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, Validators} from '@angular/forms'; @Component({ selector: 'switch-control', templateUrl: './switch-control.componen

[AngularJS] &#39;require&#39; prop in Directive or Component

When use 'require', recommend to add some error check, for example: class ChildCtrl { constructor(){ // Get prop from parent ctrl if(this.parentCtrl){ this.childProp = this.parentCtrl.prop; } } } app.directive('someDirective', () => { return { requir