How to Open new Form when click on TreeNode?
-
Dear Experts, I have a tree is filled dynamically, and I have a list of Forms, so I need when I click on Tree Node that assigned to specific form to open the Form. I hope To find a solution for this, Ahmad
Have some Google Foo[^]. From the treeview I presume you get the form name as a string, then use that to open a form.
Never underestimate the power of human stupidity RAH
-
Have some Google Foo[^]. From the treeview I presume you get the form name as a string, then use that to open a form.
Never underestimate the power of human stupidity RAH
Thank you, I have found the following Code, it is working properly:
private void button1_Click(object sender, EventArgs e)
{
Form Frm = GetFormByName(textBox1.Text );
Frm.Refresh();
Frm.Text = "Test";
Frm.StartPosition = FormStartPosition.CenterScreen;
Frm.MaximizeBox = false;
Frm.Show();} public Form GetFormByName(string FormName) { Type T = Type.GetType(FormName, false); if (T == null) { string Fullname = Application.ProductName + "." + FormName; T = Type.GetType(Fullname, true, true); } return (Form)Activator.CreateInstance(T); }
-
Dear Experts, I have a tree is filled dynamically, and I have a list of Forms, so I need when I click on Tree Node that assigned to specific form to open the Form. I hope To find a solution for this, Ahmad
Hi, please try
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
Form1 form=new Form1();
form.Show();
} -
Dear Experts, I have a tree is filled dynamically, and I have a list of Forms, so I need when I click on Tree Node that assigned to specific form to open the Form. I hope To find a solution for this, Ahmad
Consider using a Dictionary<string, Form> to 'Show existing instances of Forms; or, consider using a Dictionary<string,Type> to create and 'Show new instances based on Type:
// Assume five different Forms are defined in the Project, and 'Form1 is being used as the "main Form:"
private Dictionary dctStrForm;
private Dictionary dctStrType;
private void Form1_Load(object sender, System.EventArgs e)
{
dctStrForm = new Dictionary
{
{"Form2", new Form2()},
{"Form3", new Form3()},
{"Form4", new Form4()},
{"Form5", new Form5()}
};dctStrType = new Dictionary { {"Form2", typeof(Form2)}, {"Form3", typeof(Form3)}, {"Form4", typeof(Form4)}, {"Form5", typeof(Form5)} };
}
private void btnShowFormFromString_Click(object sender, System.EventArgs e)
{
string frmCandidate = textBox1.Text;if (dctStrForm.ContainsKey(frmCandidate)) { dctStrForm\[frmCandidate\].Show(); } else { MessageBox.Show("I don't have a Form named: " + frmCandidate); }
}
private void btnCreateAndShowFormFromString_Click(object sender, System.EventArgs e)
{
string frmCandidate = textBox1.Text;if (dctStrType.ContainsKey(frmCandidate)) { Type newFormType = dctStrType\[frmCandidate\]; Form newForm = Activator.CreateInstance(newFormType) as Form; newForm.Show(); } else { MessageBox.Show("I don't have a Form of Type: " + frmCandidate); }
}
Note that in the second example we are ignoring the probably obvious need anyone would have to keep track of new Form instances created, install EventHandlers for Events raised by the new Forms, etc. If you have assigned the 'Text or 'Type of your different Forms to the 'Tag Property of TreeNodes in your TreeView, then you can easily use the same techniques shown here: 0. in the After_Select EventHandler 1. check if the selected Node Tag is non-null: skip if null 2. if necessary cast the Tag to a Type 3. use the code shown above with either string or Type as needed.
“I speak in a poem of the ancient food of heroes: humiliation, unhappiness, discord. Those things are given to us to transform, so that we may make from the miserable circumstances of our lives things that are eternal, or aspire to be so.” Jorge Luis Borges<