游民家园

leafyoung v.s. dotnet

导航

[C#]组件的例子代码<-备忘

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsControlLibrary1
{
    [DefaultProperty(
"Active")]
    
public class Component1 : System.ComponentModel.Component, ISupportInitialize
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
private DateTime _alarm = DateTime.Now;
        
private bool _showDigitalTime = true;

        [Category(
"Behavior")]
        [Description(
"This is alarm time")]
        
public DateTime Alarm
        
{
            
get return _alarm; }
            
set { _alarm = value; }
        }


        [DefaultValue(
true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(
false)]
        
public bool ShowDigitalTime
        
{
            
get return _showDigitalTime; }
            
set { _showDigitalTime = value; }
        }


        
public Component1(System.ComponentModel.IContainer container)
        
{
            
///
            
/// Windows.Forms 类撰写设计器支持所必需的
            
///

            container.Add(this);
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
public Component1()
        
{
            
///
            
/// Windows.Forms 类撰写设计器支持所必需的
            
///

            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary> 
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if(components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }



        
组件设计器生成的代码

        [Browsable(
false)]
        
public Form Owner
        
{
            
// 用于在设计期向InitializeComponent添加代码, 产生的代码类似:
            
// this.component1.Owner = this;
            get 
            

                
if ((_owner  == null&& (this.DesignMode))
                
{
                    
// 访问设计器宿主并获取根组件的引用
                    IDesignerHost designer = 
                        
this.GetService(typeof(IDesignerHost)) as IDesignerHost;
                    
if (designer != null)
                    
{
                        _owner 
= designer.RootComponent as Form;
                    }

                }


                
return _owner;
            }

            
// 在运行期由InitializeComponent设置
            set 
            
{
                
if (! this.DesignMode)
                
{
                    
// 不要在运行期间修改Owner
                    if ((_owner != null&& (_owner != value))
                    
{
                        
throw new InvalidOperationException("Cann't set Owner at run time.");
                    }

                }

                _owner 
= value; 
            }

        }

        
private Form _owner = null;

        [DefaultValue(
false)]
        
public bool Active
        
{
            
get return _active; }
            
set 
            
{
                
if (! _initializing)
                
{
                    
if (Owner == nullthrow new Exception("Owner cann't be null.");
                }


                _active 
= value; 
            }

        }

        
private bool _active = false;

        
ISupportInitialize 成员
    }

}

posted on 2005-03-09 20:25  游民一族  阅读(531)  评论(1)    收藏  举报