Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
{

            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();

            }

        }
Posted

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.
C#
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.
C#
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
C#
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.
 
Share this answer
 
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:
C#
// 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:
C#
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:
C#
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;
}
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900