首页 > 开发 > 综合 > 正文

TreeView 派生类: TreeViewEx 实现 NodeShowToolTip、NodeDo

2024-07-21 02:22:00
字体:
来源:转载
供稿:网友
// [email protected] invent
//一个 treeview 的派生类: treeviewex 实现 nodeshowtooltip、nodedoubleclick 事件
//1.实现了 nodeshowtooltip 事件,结合键盘 ctrl 键显示及设置 tooltiptext
//2.实现了 nodedoubleclick 事件,可在调用中只响应"叶子"节点
//3.点击 treeview 空白处不选中任何节点
//class1.cs
using system;
public class treeviewex : system.windows.forms.treeview
{
    public event treeviewexeventhandler nodedoubleclick;
    public event treeviewexeventhandler nodeshowtooltip;
    private void onnodedoubleclick(system.windows.forms.treenode xx)
    {
        if(this.nodedoubleclick != null)
        {
            this.nodedoubleclick(this,new treeviewexeventargs(xx));
        }
    }
    private void onnodeshowtooltip(system.windows.forms.treenode xx, system.windows.forms.tooltip yy)
    {
        if(this.nodeshowtooltip != null)
        {
            if ((xx != null) && (this.tooltip !=null))
                this.nodeshowtooltip(this,new treeviewexeventargs(xx,this.tooltip));
        }
    }
    protected override void ondoubleclick(system.eventargs e)
    {
        if (this.selectednode != null)
            this.onnodedoubleclick(this.selectednode);
        base.ondoubleclick(e);
    }
    private system.windows.forms.tooltip tooltip;
    public treeviewex()
    {
        tooltip = new system.windows.forms.tooltip();
        this.tooltip.initialdelay = 300;
        this.tooltip.reshowdelay = 0;
    }
    protected override void onclick (system.eventargs e)
    {
        if (this.getnodeat(system.windows.forms.treeview.mouseposition.x,system.windows.forms.treeview.mouseposition.y) == null)
            this.selectednode = null;
        base.onclick(e);
    }
    protected override void onmousedown (system.windows.forms.mouseeventargs e)
    {
        if (this.getnodeat(e.x,e.y) == null)
            this.selectednode = null;
        base.onmousedown(e);

    }
    private system.windows.forms.treenode lasttreenode;
    protected override void onmousemove (system.windows.forms.mouseeventargs e)
    {
        this.cursor = system.windows.forms.cursors.default ;
        system.windows.forms.treenode treenode;
        treenode = this.getnodeat(e.x,e.y) ;
        if(treenode != null)
        {
            if( (system.windows.forms.control.modifierkeys & system.windows.forms.keys.control) != 0)
            {
                this.cursor = system.windows.forms.cursors.hand ;
                if(treenode != lasttreenode | lasttreenode ==null )
                {
                    lasttreenode = treenode;
                    this.tooltip.active = false;
                    this.onnodeshowtooltip(treenode,this.tooltip);
                    this.tooltip.active = true;
                }
            }
            else
            {
                this.cursor = system.windows.forms.cursors.default ;
                this.tooltip.active = false;
            }
        }
        else
        {
            this.cursor = system.windows.forms.cursors.default ;
            this.tooltip.active = false;
        }
        base.onmousemove(e);
    }
}
public delegate void treeviewexeventhandler(object sender,treeviewexeventargs e);
public class treeviewexeventargs : system.eventargs
{
    public treeviewexeventargs(system.windows.forms.treenode selectednode)
    {
        this.m_selectednode = selectednode;
    }
    public treeviewexeventargs(system.windows.forms.treenode node,system.windows.forms.tooltip nodetooltip)
    {
        this.m_nodetooltip = nodetooltip;
        this.m_node = node;
    }
    public treeviewexeventargs(system.windows.forms.treenode node,string tooltiptext)
    {
        this.m_tooltiptext = tooltiptext;
    }
    private string m_tooltiptext;
    private system.windows.forms.tooltip m_nodetooltip;
    private system.windows.forms.treenode m_selectednode;
    private system.windows.forms.treenode m_node;
    public system.windows.forms.treenode selectednode
    {
        get
        {
            return this.m_selectednode;
        }
    }
    public string tooltiptext
    {
        get
        {
            return this.tooltiptext;
        }
        set{m_tooltiptext = value;}
    }
    public system.windows.forms.treenode node
    {
        get
        {
            return this.m_node;
        }
    }
    public system.windows.forms.tooltip nodetooltip
    {
        get
        {
            return this.m_nodetooltip;
        }
    }
}
public class treenodeex : system.windows.forms.treenode
{
    public int getlevel()
    {
        int i = 0;
        system.windows.forms.treenode xx =this.parent;
        while ((xx = xx.parent) != null) i++;
        return i;
    }
}

//=================================================================
//调用示例
    private treeviewex treeviewex1;
    private void form1_load(object sender, system.eventargs e)
    {
        treeviewex1 = new treeviewex();
        this.controls.add(treeviewex1);
        treeviewex1.hideselection=false;
        treeviewex1.nodedoubleclick +=new treeviewexeventhandler(treeviewex1_nodedoubleclick);
        treeviewex1.nodeshowtooltip += new treeviewexeventhandler(this.treeviewex1_nodeshowtooltip);
        treeviewex1.nodes.addrange(new system.windows.forms.treenode[]{
                                                                          new system.windows.forms.treenode("root",
                                                                          new system.windows.forms.treenode[]
{new system.windows.forms.treenode("a"),new system.windows.forms.treenode("b")}
                                                                          ),
                                                                          new system.windows.forms.treenode("root1",
                                                                          new system.windows.forms.treenode[]
{new system.windows.forms.treenode("a1"),new system.windows.forms.treenode("b1")}
                                                                          )
                                                                      }
            );
    }

    public void treeviewex1_nodeshowtooltip (object sender,treeviewexeventargs e)
    {
        e.nodetooltip.settooltip (this.treeviewex1,"[" + e.node.text + "]{" );
    }

    private void treeviewex1_nodedoubleclick(object sender, treeviewexeventargs e)
    {
        if (e.selectednode.getnodecount(true)==0 )
            system.windows.forms.messagebox.show(e.selectednode.text);
    }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表