Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Open new Form when click on TreeNode?

How to Open new Form when click on TreeNode?

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aahamdan
    wrote on last edited by
    #1

    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

    M U B 3 Replies Last reply
    0
    • A aahamdan

      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

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • M Mycroft Holmes

        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

        A Offline
        A Offline
        aahamdan
        wrote on last edited by
        #3

        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);
            }
        
        1 Reply Last reply
        0
        • A aahamdan

          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

          U Offline
          U Offline
          UGUR KIZILKAYA
          wrote on last edited by
          #4

          Hi, please try

          private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
          {
          Form1 form=new Form1();
          form.Show();
          }

          1 Reply Last reply
          0
          • A aahamdan

            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

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            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<

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups