asp.net c#Treeview 增加節點

來源:互聯網
上載者:User

asp教程.net c#treeview 增加節點
void  creat_node(string
name,microsoft.web.ui.webcontrols.treenode  t1,string  number)
        {
            //定義一個新的結點

            microsoft.web.ui.webcontrols.treenode  t2=new
microsoft.web.ui.webcontrols.treenode();
            t2.id=number;
            t2.text=name;
            //判斷是否為根結點
            treeview1.expandedimageurl="images/depart03.bmp";
            if(t1.id.trim()=="###")
            {
                treeview1.nodes.add(t2);
                t2.imageurl="images/depart01.bmp";
            }
            else
            {
                t1.nodes.add(t2);
                   t2.imageurl="images/depart01.bmp";
                t1.imageurl="images/depart02.bmp";
            }

           //取得這個結點的所有子結點,利用遞迴自動產生
            string myselectquery = "select parentid, id,name from table1
where  parentid='" + number +"'";
            string myconnstring="provider=microsoft.jet.oledb.4.0;data
source="+server.mappath(".")+"/dt/mydata.mdb";
            oledbconnection myconnection = new
oledbconnection(myconnstring);
            oledbcommand mycommand = new
oledbcommand(myselectquery,myconnection);
            myconnection.open();
            oledbdatareader myreader;
            myreader = mycommand.executereader();
            while (myreader.read())
            {
                creat_node(myreader.getstring(2),t2,myreader.getstring(1));

            }
            myreader.close();
            myconnection.close();

        }

    private void createprovidertreebtn_click(object sender,
system.eventargs e)
        {
            microsoft.web.ui.webcontrols.treenode  t1=new
microsoft.web.ui.webcontrols.treenode();
            creat_node("",t1,"###");
   
2、增加根節點  
private   void menuitemaddroot_click(object   sender,   system.eventargs   e)  
{  
try  
{  
treenode   treenode=new   treenode("新節點");  
treenode.tag=maxrecordnumber(dataset.tables["tree"],"nodeid")+1;  
this.treeview.nodes.add(treenode);  
adddeletenode(treenode,rootnodetag,"add");  
treeview.selectednode   =   treenode   ;  
loadtreeview.loadtreenodeimage(treeview.selectednode,this.imagelist1,nodeimagemode);  
//執行指定節點的選定操作  
this.treeviewafterselect(treeview.selectednode   );  
menuitemrename_click(new   object(),new   system.eventargs());  
}  
catch(exception   te)  
{  
messagebox.show(this,te.message,"errorroot",messageboxbuttons.ok,messageboxicon.warning);  
}  
}

 
3、增加子節點  
private   void   menuitemaddchild_click(object   sender,   system.eventargs   e)  
{  
try  
{  
//1.當一個節點有對應連結資料時,不可以增加子節點  
//2.當一個節點有對應連結資料時,可以增加圖片  
if   (this.ishavelinedata(dataset.tables["data"],this.richtextboxid))  
{  
messagebox.show(this,"要增加子節點,必須在這個節點沒有可以連結的資料後才可以增加!","操作錯誤",messageboxbuttons.ok,messageboxicon.information   );  
return;  
}  
treenode   treenode=new   treenode("新節點");  
treenode.tag=maxrecordnumber(dataset.tables["tree"],"nodeid")+1;  
treeview.selectednode.nodes.add(treenode);  
adddeletenode(treenode,treeview.selectednode.tag.tostring(),"add");  
treeview.selectednode   =   treenode   ;  
loadtreeview.loadtreenodeimage(treeview.selectednode,this.imagelist1,nodeimagemode);  
//執行指定節點的選定操作  
this.treeviewafterselect(treeview.selectednode   );  
menuitemrename_click(new   object(),new   system.eventargs());  
}  
catch(exception   te)  
{  
messagebox.show(this,te.message,"errorchild",messageboxbuttons.ok,messageboxicon.warning);  
}  
}

 
4、增加兄弟節點  
private   void   menuitemaddborther_click(object   sender,   system.eventargs   e)  
{  
try  
{  
//判斷是否選定組件中的根節點  
if   (treeview.selectednode.parent==null)  
{  
messagebox.show   ("節點<"+treeview.selectednode.text.trim()+   ">為根節點,要加入兄弟節點,請使用"加入根節點"選項!"   ,   "information"   ,   messageboxbuttons.ok   ,   messageboxicon.information   )   ;  
return;  
}  
treenode   treenode=new   treenode("新節點");  
treenode.tag=maxrecordnumber(dataset.tables["tree"],"nodeid")+1;  
treeview.selectednode.parent.nodes.add(treenode);  
adddeletenode(treenode,treeview.selectednode.parent.tag.tostring().trim(),"add");  
treeview.selectednode   =   treenode   ;  
loadtreeview.loadtreenodeimage(treeview.selectednode,this.imagelist1,nodeimagemode);  
//執行指定節點的選定操作  
this.treeviewafterselect(treeview.selectednode   );  
menuitemrename_click(new   object(),new   system.eventargs());  
}  
catch(exception   te)  
{  
messagebox.show(this,te.message,"errorparent",messageboxbuttons.ok,messageboxicon.warning);  
}  


contextmenustrip1與treeview1關聯,在節點上單擊右鍵就給該節點添加子節點,在空白地方單擊右鍵,就添加根節點
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;

namespace windowsapplication8
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
            this.treeview1.labeledit = true;
        }

        private void treeview1_nodemouseclick(object sender, treenodemouseclickeventargs e)
        {
            if (e.button == mousebuttons.right)
            {
                this.treeview1.selectednode = e.node;
                flag = true;
                contextmenustrip1.show(mouseposition);
            }
        }
        bool flag = false;
        private void 添加節點toolstripmenuitem_click(object sender, eventargs e)
        {
            treenode tn = new treenode();
            if (flag)
            {
                this.treeview1.selectednode.nodes.add(tn);
                this.treeview1.selectednode = tn;
                tn.beginedit();
                flag = false;
            }
            else
            {
                this.treeview1.nodes.add(tn);
                this.treeview1.selectednode = tn;
                tn.beginedit();
                flag = false;
            }
        }

        private void treeview1_afterlabeledit(object sender, nodelabelediteventargs e)
        {
            e.node.text = e.label;
        }

        private void contextmenustrip1_opening(object sender, canceleventargs e)
        {
            if (!flag)
            {
                this.treeview1.selectednode = null;
            }
        }
        bool flag1 = false;
        private void contextmenustrip1_itemclicked(object sender, toolstripitemclickedeventargs e)
        {
            flag1 = true;
        }

        private void contextmenustrip1_closed(object sender, toolstripdropdownclosedeventargs e)
        {
            if (!flag1)
            {
                flag = false;
            }
            else
            {
                flag1 = false;
            }
        }

    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.