如何在treeview控件中显示c#中的必要表单 [英] how to show necessary forms in c# in treeview control
问题描述
你好,
在下面的代码中它放在树视图控件的afterselect事件中。
怎么可能不重复frm.Show()事件拜托?
谢谢
hello,
in below code it is placed in the afterselect event of a treeview control.
how is it possible to NOT repeat the frm.Show() events please?
thanks
{
string s = e.Node.Text.ToLower();
Form frm;
if (s == "form1")
{
frm = new Form1();
frm.Show();
}
else if (s == "form2")
{
frm = new Form2();
frm.Show();
}
else if (s == "form3")
{
frm = new Form3();
frm.Show();
}
}
推荐答案
这是你做出的最简单的改动将删除所有那些 show
语句并在结尾处有一个语句(在检查frm不为null之后)。即
Well the easiest changes for you to make are to remove all those show
statements and have a single one at the end (after checking that frm is not null). I.e.
if (s == "form1")
frm = new Form1();
if (s == "form2")
frm = new Form2();
if (s == "form3")
frm = new Form3();
if (frm != null)
frm.Show();
However, the switch[^] statement was designed for these scenarios E.g.
switch (s)
{
case "form1":
frm = new Form1();
break;
case "form2":
frm = new Form2();
break;
case "form3":
frm = new Form3();
break;
}
if (frm != null)
frm.Show();
如果是我,我会这样做
If it was me I'd do it this way
Form frm = null;
//Next line not required if nodes contain real form names
s = s.Substring(0, 1).ToUpper() + s.Substring(1, s.Length - 1);
var t = Type.GetType(frm.GetType().Namespace + "." + s);
if (t != null)
{
frm = (Form)Activator.CreateInstance(t);
frm.Show();
}
但是在尝试使用任何作业中的最后一位之前,请确保了解发生了什么。
But make sure you understand what is happening before trying to use that last bit in any assignments.
如果表单已经创建,则不要重新创建表单;如果表格已经可见,请不要重新显示表格:
表格范围:
Simply don't re-create the Form if it is already created; don't re-show the Form if it is already visible:
In Form scope:
// assume the TreeView, 'treeView1 is on a MainForm which has been shown
Form1 f1 = new Form1();
Form2 f2 = new Form2();
Form3 f3 = new Form3();
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
switch(e.Node.Text)
{
case "Node1":
if(! f1.Visible) f1.Show();
break;
case "Node2":
if(! f2.Visible) f2.Show();
break;
case "Node3":
if(! f3.Visible) f3.Show();
break;
}
}
请注意,此代码假定用户无法关闭辅助表单的实例。以下是您可以处理的方法:
Note this code assumes the user cannot close the instances of the secondary Forms. Here's how you might handle that:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
switch(e.Node.Text)
{
case "Node1":
if(f1.IsDisposed) f1 = new Form1();
if(! f1.Visible) f1.Show();
break;
case "Node2":
if(f2.IsDisposed) f2 = new Form2();
if(! f2.Visible) f2.Show();
break;
case "Node3":
if(f3.IsDisposed) f3 = new Form1();
if(! f3.Visible) f3.Show();
break;
}
}
可能有充分的理由不允许用户关闭(永久)二级表格:重新实例化表格成本高昂;您可能会丢失在表格上输入的数据(如果您还没有保留它)。
如果您希望用户有一种简单的方法来隐藏辅助表单没有真正关闭然后尝试这样:
There might be good reasons to not allow the user to close (permanently) a secondary Form: it's costly to re-instantiate Forms; you might lose data entered on the Forms (if you haven't preserved it).
If you want the user to have an easy way to "hide" a secondary Form without really "closing" it then try this:
private void Form1_Load(object sender, EventArgs e)
{
foreach(Form frm in new List<Form> {f1, f2, f3})
{
frm.FormClosing += frm_FormClosing;
frm.Show();
}
}
// do not set this EventHandler "as is" to be the FormClosing for the Main Form !
private void frm_FormClosing(object sender, FormClosingEventArgs e)
{
(sender as Form).Hide();
e.Cancel = true;
}
现在我想告诉你最好处理这个问题:使用两个对称类型字典< TreeNode,Form>和< Form,TreeNode>同步Forms和TreeView节点之间的选择。如果你想知道如何做到这一点:问这里。
And now I'd like to tell you there's a better to handle this in general: use two "symmetric" Dictionaries of Types <TreeNode, Form> and <Form, TreeNode> to synchronize selection between the Forms and the TreeView Nodes. If you want to know how to do that: ask here.
这篇关于如何在treeview控件中显示c#中的必要表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!