读书人

在C#中怎么实现点击左边的treeview的节

发布时间: 2012-01-09 21:05:42 作者: rapoo

在C#中如何实现点击左边的treeview的节点,右边显示相应的界面?
在C#中如何实现点击左边的treeview的节点,右边显示相应的界面?

右边的界面应该用什么东西做呢?

很多人说用panel,可是如果节点很多的话,那不是会有很多panel?

我本意是想,每个节点的界面都是单独的winform,然后加载到右边的panel里面,可是运行之后根本不显示加载的窗体啊。

请高手帮忙。。

[解决办法]
用父窗口来做。
[解决办法]

探讨
在C#中如何实现点击左边的treeview的节点,右边显示相应的界面?

右边的界面应该用什么东西做呢?

很多人说用panel,可是如果节点很多的话,那不是会有很多panel?


[解决办法]
不显示,是因为你没有调用窗体的Show方法:
C# code
        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();            }        }
[解决办法]
我做过,就用panel,所有的窗口都在panel中切换
C# code
Form1 form1 = new Form1form1.TopLevel = false;panel1.Controls.Add(form1);form1.Show();
[解决办法]
跟TreeView放在哪里没有关系的,关键是你不能用splitContainer1.Panel2.Controls.Add(stfrm1);
必须在splitContainer1.Panel2上放一个独立的Panel才行!

splitContainer1.Panel2的类型是:SplitterPanel不是Panel!

[解决办法]
为什么每个界面都要用Form弹出呢,只用一个Form不行吗?
Form的左边是TreeView,右边是Panel,而把每个界面定义一个UserControl,TreeView节点点击之后,动态设置UserControl到Panel不就完了。弹那么多Form用户体验很差的,所有的东西在一个Form当中搞定。
[解决办法]
我的意思是你不要在Panel里面加载form,而是加载一个userControl,将form当中的逻辑移动到userControl当中。将一个form加到一个penel当中,这不符合微软的设计规范。
[解决办法]
用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
[解决办法]
不知道我做的是不是你想要的,比较粗糙,欢迎拍砖。

C# code
 
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;
}
}


------解决方案--------------------


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

splitContainer1.Panel2的类型是:SplitterPanel不是Panel!


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

[解决办法]
就是使用UserControl来完成功能。
每一个功能块都是一个UserControl,点击Tree的时候,就可以实例化一个UserControl了。
[解决办法]
从一个Parent的Form当中构造一个孩子Form1,然后Show这个Form1,就会产生线程安全方面的风险,这个Form1上面的某个功能有可能无法使用。如果需求不是很变态,建议一个Form能搞定就使用一个Form,不要在父Form里面创建子Form。并且如果父与子直接有很多的交换,风险就更大了。
[解决办法]
探讨
引用:
引用:
引用:
在treeview里加个取结点事件
判断取到的是第几级结点,然后 formX.Show... X 是1\2\3\4...


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

可以是,也可以不是


偶现在明白了,之前的问题是……

读书人网 >C#

热点推荐