How to add nodes in TreeView control from two different Forms in C# -


my program:

form (form1) contains:

  • button1 control
  • treeview1 control

form (form2) contains:

  • burron1 control
  • label1 control
  • label2 control

classarray

  • public static ar2 array

my aim make each element ar2 array node in treeview1 control using both forms (form1 , form2). tricky thing need elements meet specific conditions added form2. don't know how that.

i added if condition pretending next element special , must added form2.

                if (element == "3")             {                 form2 frm2 = new form2(counter);                 frm2.show();                 break;              } 

so when element equal 3 call form2 , add next element form2.

on form2 need click button1 , add this special element only (in case element 4) , rest of elements have automatically added foreach loop in form1.

form1 code:

public partial class form1 : form {     int counter = 0;     public form1()     {         initializecomponent();     }      private void form1_load(object sender, eventargs e)     {      }      private void button1_click(object sender, eventargs e)     {         foreach (string element in classarray.ar2)         {             treeview1.nodes.add(element);             counter++;              if (element == "3")             {                 form2 frm2 = new form2(counter);                 frm2.show();                 break;              }         }     } } 

classarray code:

    class classarray {     public static string[] ar2 = new string[8] { "1", "2", "3", "4", "5", "6", "7", "8" };  } 

form2 code:

public partial class form2 : form {      public form2(int counter)     {         initializecomponent();         label1.text = "elements added form1 = "+counter.tostring();     }      private void button1_click(object sender, eventargs e)     {         //add special element note     } } 

enter image description here

click add special element node in treeview1.

then foreach loop should continue until meet next condition or if no condition continue end adding elements ar2 nodes.

any appreciated.

in form1 create public method, treeview node added outside.

    public void addelement(int index)     {         string element = classarray.ar2[index];         treeview1.nodes.add(element);         counter++;     } 

rework button1_click code use addelement method. replace foreach loop for. way possible skip 1 element, add form2.

    private void button1_click(object sender, eventargs e)     {         (int = 0; < classarray.ar2.length; i++)         {             addelement(i);              if (classarray.ar2[i] == "3")             {                 form2 frm2 = new form2(counter);                 // showdialog stop code execution until dialog closed                 frm2.showdialog(this); // "this" - dialog owner; come in handy in form2                 i++; // skip "4"             }         }     } 

now in form2 take form1 instance , call created addelement add node treeview.

    private void button1_click(object sender, eventargs e)     {         //add rest of elemnts notes         form1 form = owner form1;         form.addelement(currentelementindex);     } 

currentelementindex class level variable, holds counter value.

    int currentelementindex = 0;      public form2(int counter)     {         ...         currentelementindex = counter;     } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -