使用树视图,在树视图节点上单击事件
我试图在单击树视图的子节点或父节点时打开表单:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TreeNode head = new TreeNode("HEAD");
TreeNode member = new TreeNode("MEMBER ");
TreeNode submember = new TreeNode("SUB-MEMBER");
private void Form1_Load(object sender, EventArgs e)
{
head.Nodes.Add(member);
member.Nodes.Add(submember);
treeView1.Nodes.Add(head);
treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode == member)
{
MemberForm mf = new MemberForm();
mf.ShowDialog();
}
if (treeView1.SelectedNode == head)
{
HeadForm hf = new HeadForm();
hf.ShowDialog();
}
if (treeView1.SelectedNode == submember)
{
SubMemberForm sf = new SubMemberForm(); //is this way of checking that which node is clicked efficient???
sf.ShowDialog();
}
}
}
I am trying to open a Form when the child node or parent node of a treeview is clicked :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TreeNode head = new TreeNode("HEAD");
TreeNode member = new TreeNode("MEMBER ");
TreeNode submember = new TreeNode("SUB-MEMBER");
private void Form1_Load(object sender, EventArgs e)
{
head.Nodes.Add(member);
member.Nodes.Add(submember);
treeView1.Nodes.Add(head);
treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode == member)
{
MemberForm mf = new MemberForm();
mf.ShowDialog();
}
if (treeView1.SelectedNode == head)
{
HeadForm hf = new HeadForm();
hf.ShowDialog();
}
if (treeView1.SelectedNode == submember)
{
SubMemberForm sf = new SubMemberForm(); //is this way of checking that which node is clicked efficient???
sf.ShowDialog();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要树视图中只有三个节点,这可能会很有效。但是,这需要您为添加的每个新节点编写额外的 if 语句。如果您尝试区分节点深度,最好使用
Level
属性。As long as you have only three nodes in your treeview, this might be efficient. However this would require you to write an extra if statement for each new node you add. If you're trying to differentiate on node depth you are better off using the
Level
property.这可能有点黑客和滥用标签功能,但你可以这样做:
This may be slightly hackish and abusive of the tag feature but you could do something like this: