如何在组件(component中)模拟用户控件(UserControl)中FindForm()的功能

    使用Component编程是一项值得推崇的技术,它既具有可视化的界面编程模式,又不向UserControl那样体积庞大。但是,熟悉UserControl的朋友们都知道,在UserControl类中有一个FindForm()函数,返回UserControl所在的窗体的引用,这将大大方便我们对窗体的控制----尽管这可能有些越俎代庖的味道,但有时我们就需要这种控制能力。
    但是,在Component并没有提供这样的函数,你可以使用其它的一些技巧来取得Component所在的窗体的引用,比如在Component的构造函数中使用Application.AddMessageFilter(this),然后取出由窗体发来的消息的句柄,就可以得到窗体的引用,缺点是不能设计时刻就获得窗体引用;比如可以给Component加一个StyleForm的属性,然后你就可以在设计器中用鼠标选择一个,缺点是你必须手动来选择。
     今天,花了半天的时间,终于设计出了克服了以上两个缺点的方案,代码如下:
None.gif using  System;
None.gif
using  System.ComponentModel;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Diagnostics;
None.gif
using  System.Text;
None.gif
using  System.ComponentModel.Design;
None.gif
using  System.Windows.Forms;
None.gif
namespace  FindFormSimulation
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class BaseStyle : Component
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public BaseStyle()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public BaseStyle(IContainer container)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            container.Add(
this);
InBlock.gif
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 关键在这里,对基类的Site重载。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override ISite Site
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.Site;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (base.Site != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
base.Site = value;
InBlock.gif                    
//使用反射机制,在设计时刻取得你要控制的窗体。
InBlock.gif
                    IReferenceService referenceService = (IReferenceService)this.GetService(typeof(IReferenceService));
InBlock.gif                    
if (referenceService != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
/**////下面这句用于取得本组件所在的窗体对象。
InBlock.gif                        object[] parent = referenceService.GetReferences(typeof(Form));
InBlock.gif                        Form container 
= parent[0as Form;
InBlock.gif                        StyleForm 
= container;
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
/**////如下方法测试,可以知道parent.Length总是为1的。
InBlock.gif                        //StyleForm.Text = parent.Length.ToString();
ExpandedSubBlockEnd.gif
                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private Form styleForm = null;
InBlock.gif        [Description(
"本组件所要控制的窗体"), DefaultValue(null)]
InBlock.gif        
public Form StyleForm
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn styleForm; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (styleForm != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    styleForm 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/flappy/archive/2006/04/19/379649.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值