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

恋上微笑的天使 2010-04-15 03:35:15
在C#中如何实现点击左边的treeview的节点,右边显示相应的界面?

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

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

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

请高手帮忙。。
...全文
1532 44 打赏 收藏 转发到动态 举报
写回复
用AI写文章
44 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaowen550 2012-09-10
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 的回复:]
不知道我做的是不是你想要的,比较粗糙,欢迎拍砖。


C# code


using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static voi……
[/Quote]


这里是不是事先把自己定义的usercontrol类型赋值给了相应node的tag属性?
l504992005 2011-11-10
  • 打赏
  • 举报
回复
winfrom中有什么具体的控件啊
cocohunter 2010-08-25
  • 打赏
  • 举报
回复
很好很强大,学习了
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 resigner 的回复:]
引用 15 楼 loveme_muzi 的回复:
引用 13 楼 resigner 的回复:
在treeview里加个取结点事件
判断取到的是第几级结点,然后 formX.Show... X 是1\2\3\4...


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

可以是,也可以不是
[/Quote]

偶现在明白了,之前的问题是因为没show。

不过有朋友跟贴说这种方法并不好。

期待他回贴呢。
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 wedy_wang 的回复:]
我的意思是你不要在Panel里面加载form,而是加载一个userControl,将form当中的逻辑移动到userControl当中。将一个form加到一个penel当中,这不符合微软的设计规范。
[/Quote]

userControl应该是用户控件吧,但是具体做法不会,烦请指教。
  • 打赏
  • 举报
回复

第一次发贴,谢谢大家的热情,不知道结贴之后还能不能回贴,不能的话可以加偶的QQ:806905.注明:csdn.
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 resigner 的回复:]
引用 29 楼 loveme_muzi 的回复:
引用 26 楼 resigner 的回复:
引用 15 楼 loveme_muzi 的回复:
引用 13 楼 resigner 的回复:
在treeview里加个取结点事件
判断取到的是第几级结点,然后 formX.Show... X 是1\2\3\4...


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

可以……
[/Quote]

不是这样的呢,发不上图,可以加偶的QQ:806905
没花鹿 2010-04-16
  • 打赏
  • 举报
回复
我的级别不够,发不上图来.
没花鹿 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 loveme_muzi 的回复:]
引用 26 楼 resigner 的回复:
引用 15 楼 loveme_muzi 的回复:
引用 13 楼 resigner 的回复:
在treeview里加个取结点事件
判断取到的是第几级结点,然后 formX.Show... X 是1\2\3\4...


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

可以是,也可以不是


偶现在明白了,之前的问题是……
[/Quote]

呵呵,这明明是form的边界嘛:
在同一个Form1里,可以改变如下属性:
Form1.ActiveForm.Width = x + y;
x是原先的width值
y是增量,也就是你的treeview的宽度,或panel的宽度
这样的话,就在一个Form1里了(也就没有了子窗体一说了)
具体实现:
做个按钮,宽度做到最小,3(?)个像素单位,高度适中,按一下,width+=y,再按一下,width-=y.
是这样的业务吗?
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 mjp1234airen4385 的回复:]
就是使用UserControl来完成功能。
每一个功能块都是一个UserControl,点击Tree的时候,就可以实例化一个UserControl了。
[/Quote]

userControl不会用。55555.
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 computerfox 的回复:]
引用 20 楼 loveme_muzi 的回复:
引用 18 楼 computerfox 的回复:
跟TreeView放在哪里没有关系的,关键是你不能用splitContainer1.Panel2.Controls.Add(stfrm1);
必须在splitContainer1.Panel2上放一个独立的Panel才行!

splitContainer1.Panel2的类型是:Split……
[/Quote]

好的,偶再看一下,谢谢。
wedy_wang 2010-04-16
  • 打赏
  • 举报
回复
从一个Parent的Form当中构造一个孩子Form1,然后Show这个Form1,就会产生线程安全方面的风险,这个Form1上面的某个功能有可能无法使用。如果需求不是很变态,建议一个Form能搞定就使用一个Form,不要在父Form里面创建子Form。并且如果父与子直接有很多的交换,风险就更大了。
mjp1234airen4385 2010-04-16
  • 打赏
  • 举报
回复
就是使用UserControl来完成功能。
每一个功能块都是一个UserControl,点击Tree的时候,就可以实例化一个UserControl了。
捷哥1999 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 loveme_muzi 的回复:]
引用 18 楼 computerfox 的回复:
跟TreeView放在哪里没有关系的,关键是你不能用splitContainer1.Panel2.Controls.Add(stfrm1);
必须在splitContainer1.Panel2上放一个独立的Panel才行!

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


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

一样的方法没变!
还是对panel1添加form:


panel1.Controls.Add(stfrm1);
stfrm1.Show()

Justin-Liu 2010-04-16
  • 打赏
  • 举报
回复
越看越复杂了。。。
wedy_wang 2010-04-16
  • 打赏
  • 举报
回复
不知道我做的是不是你想要的,比较粗糙,欢迎拍砖。


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;
}
}
bestboy1 2010-04-15
  • 打赏
  • 举报
回复
做过这样的winform程序,像资源管理器一样的软件。
没花鹿 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 loveme_muzi 的回复:]
引用 13 楼 resigner 的回复:
在treeview里加个取结点事件
判断取到的是第几级结点,然后 formX.Show... X 是1\2\3\4...


这个的前提还是必须是treeview所在的窗体是父窗体吧。
[/Quote]
可以是,也可以不是
zs82891879 2010-04-15
  • 打赏
  • 举报
回复
用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
zhangCbrother 2010-04-15
  • 打赏
  • 举报
回复
不懂。。~~
userControl是什么?学习!!
加载更多回复(23)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧