自定义控件 之 树型控件

要过年了,自己却要离开这个地方了....想着没什么好留下的...打算花一个周的时间..编写个树控件出来.留给公司吧.....也顺便学学编写控件.....

介绍:
前段时间, 做移动集团综合告警的时候,由于要实时的显示现有告警,所以,有开发过一个树控件,支持了结点的动态生成....但是,由于针对性太强,不能够得到复用,以至经理要求做一个新的树型控件出来,因此,也就有了这篇文章...

废话:
由于是第一次弄这样的东西,如果有什么不正确的地方,还希望各个老大帮忙指出....

第一步:编写一个容器控件ContentControl..
问题描述:
是从Control派生,还是从UserControl派生,且,是否支持用户使用本控件后,在vs ide 中可以直接拖控件到该控件中.....
解决方法:由于编写该控件的目的,是自定一个树控件,因此,没必要支持把别的控件拖动到本控件中....如果实在是要支持这样的,可以这样设置...

Code
 [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design")]
    
public class ContentControl : Control
    
{
    }
当然,需要引用 using System.ComponentModel;

第二步:抽象出一个树型控件的一些接口 ITreeControl(现阶段还没规划完成...先记录一下)
树型控件的一些接口
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace BOCO.Controls
{
    
public interface ITreeControl
    
{
        
/**//// <summary>
        
/// 节点选中事件
        
/// </summary>

        event EventHandler<TreeNodeEventArgs> NodeSelected;

        
/**//// <summary>
        
/// 选中的节点项
        
/// </summary>

        TreeItem SelectedItem get; }

        
/**//// <summary>
        
/// 当前选中的树节点
        
/// </summary>

        TreeNode SelectedNode getset;}
    }


}


第三步:正如上面看到的, TreeItem,这个东西是什么?TreeItem,表示的是树节点的tag,同时,也是一个逻辑上的树(好了,难点来了)
 
首先,怎么设计这个逻辑意义上的树..
树节点,一般有两种形式,一是叶子节点,二是树枝节点(不知道组合模式可不可以用到这个地方)
树叶子节点接口: IChild<T>(因为在看范型的东西,所以,使用下下)
IChild
public interface IChild<T> where T: class, IChild<T>
{
    
// Properties
    int Index get; }
    T NextBrother 
get; }
    IParent
<T> Parent getset; }
    T PrevBrother 
get; }
}


 

 

树树枝节点接口:
IParent
using System;
using System.Collections.Generic;
using System.Text;

namespace BOCO.Controls
{
    
public interface IParent<T> : IChild<T> where T : class, IChild<T>
    
{
        CollectionList
<T> Children
        
{
            
get;
        }

    }

}


如上,CollectionList是什么东西呢?这个是实现接口 IBindingList的东西,那 IBindingList这个又是什么呢?好象是实现了这个接口的,就可以在后台模型更改后,反映到前台来的吧.....
(未完....)
第四步:节点事件的参数: TreeNodeEventArgs
TreeNodeEventArgs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace BOCO.Controls
{
    
/**//// <summary>
    
/// 树节点的事件参数
    
/// </summary>

    public class TreeNodeEventArgs:EventArgs
    
{
        
public TreeNodeEventArgs(TreeNode treeNode, TreeItem treeItem)
        
{
            
this.mTreeNode = treeNode;
            
this.mTreeItem = treeItem;
        }


        
private TreeItem mTreeItem;
        
public TreeItem TreeItem
        
{
            
get
            
{
                
return mTreeItem;
            }

        }


        
private TreeNode mTreeNode;
        
public TreeNode TreeNode
        
{
            
get
            
{
                
return mTreeNode;
            }

        }

    }

}


第五步: TreeControl,继承 ContentControl,实现 ITreeControl接口
TreeControl
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace BOCO.Controls
{
    
public class TreeControl : ContentControl,ITreeControl
    
{
        
private TreeView mTreeView = new TreeView();

        
public TreeControl()
        
{
            
this.Content = mTreeView;
        }



        
ITreeControl Members#region ITreeControl Members

        
public event EventHandler<TreeNodeEventArgs> NodeSelected;

        
public TreeItem SelectedItem
        
{
            
get throw new Exception("The method or operation is not implemented."); }
        }


        
public System.Windows.Forms.TreeNode SelectedNode
        
{
            
get
            
{
                
throw new Exception("The method or operation is not implemented.");
            }

            
set
            
{
                
throw new Exception("The method or operation is not implemented.");
            }

        }


        
#endregion

    }

}


第六步:树节点的层次应支持可配置

未完....明天继续....

转载于:https://www.cnblogs.com/yanchanggang/archive/2008/01/28/1056325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值