在C#中如何实现点击左边的treeview的节点,右边显示相应的界面?右边的界面应该用什么东西做呢?很多人说用panel,可是如果节点很多的话,那不是会有很多panel?我本意是想,每个节点的界面都是单独的winform,然后加载到右边的panel里面,可是运行之后根本不显示加载的窗体啊。请高手帮忙。。

解决方案 »

  1.   

    可是本身这个放treeview的界面就是子窗体了啊
      

  2.   


    每一个节点对应一个url,效果不是传统的frame,是刷新全页面可以放在母版页里,不同的内容页使用即可
      

  3.   

    为啥要把treeview弄到子窗体里面呢?如果放在父窗体不就很容易了。
      

  4.   

    首先,是webform 还是 winform?
      

  5.   

    只用一个panel啊,点击别的节点时,内容重新加载。panel就是一个载体
      

  6.   


    可是为什么我加载了之后,并不显示呢。代码如下: private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                if (treeView1.SelectedNode.Text == "权限管理")
                {
                    Form4 f4 = new Form4();
                    f4.TopLevel = false;
                   
                    panel2.Controls.Clear();
                    panel2.Controls.Add(f4);
                }
            }
      

  7.   

    winfrom 没写过。 。  web的我会
      

  8.   

    在treeview里加个取结点事件
    判断取到的是第几级结点,然后 formX.Show...   X 是1\2\3\4...
      

  9.   

    不显示,是因为你没有调用窗体的Show方法:        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                if (e.Node.Name == "节点0")
                {
                    SettingForm1 stfrm1 = new SettingForm1();
                    stfrm1.TopLevel = false;
                    stfrm1.FormBorderStyle = FormBorderStyle.None;
                    stfrm1.WindowState = FormWindowState.Maximized;
                    panel1.Controls.Add(stfrm1);
                    stfrm1.Show();
                }
            }
    这样就可以了!
      

  10.   


    这个的前提还是必须是treeview所在的窗体是父窗体吧。
      

  11.   

    我做过,就用panel,所有的窗口都在panel中切换Form1 form1 = new Form1
    form1.TopLevel = false;
    panel1.Controls.Add(form1);
    form1.Show();点击treeview是先检索panel1上的Controlss是否为0,不是0,就全部关闭
    关闭不用我再说了吧
      

  12.   


    - -好像是这个原因,偶现在show了窗体之后就显示了。还有一个问题,可以把treeview放在splitContainer里面么。偶刚才试了一下,发现窗体又不显示了喔。望解答。
      

  13.   

    跟TreeView放在哪里没有关系的,关键是你不能用splitContainer1.Panel2.Controls.Add(stfrm1);
    必须在splitContainer1.Panel2上放一个独立的Panel才行!splitContainer1.Panel2的类型是:SplitterPanel不是Panel!
      

  14.   

    为什么每个界面都要用Form弹出呢,只用一个Form不行吗?
    Form的左边是TreeView,右边是Panel,而把每个界面定义一个UserControl,TreeView节点点击之后,动态设置UserControl到Panel不就完了。弹那么多Form用户体验很差的,所有的东西在一个Form当中搞定。
      

  15.   


    是在splitContainer.panel2上面放的一个panel,可是窗体还是不显示,之前的代码需要改什么地方么?
      

  16.   


    我就是这个意思啦,点击节点的时候,加载form到panel里面显示,是这样么?
      

  17.   

    我的意思是你不要在Panel里面加载form,而是加载一个userControl,将form当中的逻辑移动到userControl当中。将一个form加到一个penel当中,这不符合微软的设计规范。
      

  18.   

    不懂。。~~
     userControl是什么?学习!!
      

  19.   

    用webBrowser.Navigate(url);
    url是你treeview被选中节点的属性值TreeNode tn = this.treeView1.SelectedNode;
                string url = "";
                if (tn.Tag != null)
                {
                    url = tn.Tag.ToString();
                    webBrowser1.Navigate(url);
                }
    Tag存储url
      

  20.   

    做过这样的winform程序,像资源管理器一样的软件。
      

  21.   


    userControl应该是用户控件吧,但是具体做法不会,烦请指教。
      

  22.   


    偶现在明白了,之前的问题是因为没show。不过有朋友跟贴说这种方法并不好。期待他回贴呢。
      

  23.   

    不知道我做的是不是你想要的,比较粗糙,欢迎拍砖。
    using System;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }
        class MainForm:Form
        {
            private TreeView treeView1;
            private Panel panel1;        public MainForm()
            {
                this.InitializeComponent();            LoginInDialog login = new LoginInDialog();
                this.treeView1.Nodes[0].Tag = login;            WelcomeDialog welcome = new WelcomeDialog();
                this.treeView1.Nodes[1].Tag = welcome;
            }
        
            private void InitializeComponent()
            {
                System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Welcome");
                System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Login");
                this.treeView1 = new System.Windows.Forms.TreeView();
                this.panel1 = new System.Windows.Forms.Panel();            this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
                this.treeView1.Location = new System.Drawing.Point(0, 0);
                this.treeView1.Name = "treeView1";
                treeNode1.Name = "Node0";
                treeNode1.Text = "Welcome";
                treeNode2.Name = "Node1";
                treeNode2.Text = "Login";
                this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                treeNode1,
                treeNode2});
                this.treeView1.Size = new System.Drawing.Size(188, 380);
                this.treeView1.TabIndex = 0;
                this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
                this.Controls.Add(this.treeView1);            this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
                this.panel1.Location = new System.Drawing.Point(194, 0);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(358, 380);            this.ClientSize = new System.Drawing.Size(552, 380);
                this.Controls.Add(this.panel1);
                this.Controls.Add(this.treeView1);
                this.Name = "MainForm";
                this.ResumeLayout(false);
            }        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                this.panel1.Controls.Clear();
                this.panel1.Controls.Add(e.Node.Tag as Control);
            }
        }
        class WelcomeDialog:UserControl
        {
            public WelcomeDialog()
            {
                this.InitializeComponent();
            }        #region Component Designer generated code        private void InitializeComponent()
            {
                this.linkLabel1 = new System.Windows.Forms.LinkLabel();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();            this.linkLabel1.AutoSize = true;
                this.linkLabel1.Location = new System.Drawing.Point(117, 19);
                this.linkLabel1.Name = "linkLabel1";
                this.linkLabel1.Size = new System.Drawing.Size(111, 13);
                this.linkLabel1.TabIndex = 0;
                this.linkLabel1.TabStop = true;
                this.linkLabel1.Text = "http://www.csdn.net/";            this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(19, 19);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(67, 13);
                this.label1.TabIndex = 1;
                this.label1.Text = "Welcome to:";            this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(19, 56);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(277, 13);
                this.label2.TabIndex = 2;
                this.label2.Text = "Copyright © 1999-2010, CSDN.NET, All Rights Reserved";            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.Controls.Add(this.label2);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.linkLabel1);
                this.Name = "WelcomeDialog";
                this.Size = new System.Drawing.Size(299, 269);
            }        #endregion        private System.Windows.Forms.LinkLabel linkLabel1;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
        }
        class LoginInDialog:UserControl
        {
            public LoginInDialog()
            {
                this.InitializeComponent();
            }        private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();            this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(31, 28);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(60, 13);
                this.label1.TabIndex = 0;
                this.label1.Text = "UserName:";
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(34, 77);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(56, 13);
                this.label2.TabIndex = 1;
                this.label2.Text = "Password:";            this.textBox1.Location = new System.Drawing.Point(113, 28);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(130, 20);
                this.textBox1.TabIndex = 2;
                this.textBox2.Location = new System.Drawing.Point(113, 70);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(130, 20);
                this.textBox2.TabIndex = 2;            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.label1);
                this.Name = "LoginInDialog";
                this.Size = new System.Drawing.Size(297, 262);
            }
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
        }
    }
      

  24.   


    一样的方法没变!
    还是对panel1添加form:
                    panel1.Controls.Add(stfrm1);
                    stfrm1.Show()
      

  25.   

    就是使用UserControl来完成功能。
    每一个功能块都是一个UserControl,点击Tree的时候,就可以实例化一个UserControl了。
      

  26.   

    从一个Parent的Form当中构造一个孩子Form1,然后Show这个Form1,就会产生线程安全方面的风险,这个Form1上面的某个功能有可能无法使用。如果需求不是很变态,建议一个Form能搞定就使用一个Form,不要在父Form里面创建子Form。并且如果父与子直接有很多的交换,风险就更大了。
      

  27.   


    userControl不会用。55555.
      

  28.   


    呵呵,这明明是form的边界嘛: 
    在同一个Form1里,可以改变如下属性:
    Form1.ActiveForm.Width = x + y;
    x是原先的width值
    y是增量,也就是你的treeview的宽度,或panel的宽度
    这样的话,就在一个Form1里了(也就没有了子窗体一说了)
    具体实现:
    做个按钮,宽度做到最小,3(?)个像素单位,高度适中,按一下,width+=y,再按一下,width-=y.
    是这样的业务吗?
      

  29.   


    第一次发贴,谢谢大家的热情,不知道结贴之后还能不能回贴,不能的话可以加偶的QQ:806905.注明:csdn.
      

  30.   

    这里是不是事先把自己定义的usercontrol类型赋值给了相应node的tag属性?