Hi, i have a windows form application and the main thing in there is a treeview, and i am adding nodes to the treeview. so i have another class file that will consist of the algorithm of how those things work for the treeview. the problem is i can't call the function from the treeview class unless i make the function static but the member of windows form are private and the static function cannot access non static member. can anybody help me or give me advice of solving this problem? the code is something similar to this. They are both in the same namespace.

public partial class TreeViewx1 : UserControl
{
       public void UpdateTreeView()
        {
        TreeView1.Nodes["test"].Nodes.Add(new TreeNode("test11"));
        }
}

class class22 
{
//other algorithm
UpdateTreeView();
}

the only thing i can call the UpdateTreeView() is to make it static. then it will be something like this TreeViewx1.UpdateTreeView();

but the TreeView1 in the form is private and non static. how can i solve this? thanks.

Recommended Answers

All 6 Replies

The public properties/methods of your user control will be accessible from the control's parent, to whom the instance of the control belongs. So if your control is on Form1, Form1 should be able to call UpdateTreeView().

Your seperate class will not have that type of access, and do you really think it should? That class shouldn't be particularly concerned with how the presentation layer is using and displaying data, its focus should be delivering the data to the control. The particular logic regarding how the data actually gets displayed should remain within the user control (and the question of when can belong to the control, the parent, or both).

Hi, thanks for the reply, so how can i ask the form to update the treeview? from your reply it means i cannot do that? my second class 2 actually manipulating the some data, for example something like an arraylist. then i wanted to ask the form to update the treeview with the data in the arraylist. the form is not a main program to run. it is just getting data from the arraylist and displaying it.

The public properties/methods of your user control will be accessible from the control's parent, to whom the instance of the control belongs. So if your control is on Form1, Form1 should be able to call UpdateTreeView().

Your seperate class will not have that type of access, and do you really think it should? That class shouldn't be particularly concerned with how the presentation layer is using and displaying data, its focus should be delivering the data to the control. The particular logic regarding how the data actually gets displayed should remain within the user control (and the question of when can belong to the control, the parent, or both).

When you are creating an instance of the form, you are doing something like this?

Form2 form2 = new Form2();
            form2.Show();

In this example, Form2 should be given a public method. The code that created the instance of the form would be able to call that method. Similarly, the user control's public method could be accessed by the form.

// inside whatever created instance of Form2
   form2.UpdateData(dataInput);

   .....

   // inside Form2
   public void UpdateData(ArrayList dataInput)
   {
         myControl.UpdateControl(dataInput);
   }

   .....

   // inside User Control
   public void UpdateData(ArrayList dataInput)
   {
        // do whatever
   }

Could you consider about inheriting classes like this:
after inheritation you should create:

TreeViewx1 t1=new class22();
   1.
      public partial class TreeViewx1 : UserControl
   2.
      {
   3.
      public void UpdateTreeView()
   4.
      {
   5.
      TreeView1.Nodes["test"].Nodes.Add(new TreeNode("test11"));
   6.
      }
   7.
      }
   8.
       
   9.
      class class22:TreeViewx1
  10.
      {
  11.
      //other algorithm
  12.
      UpdateTreeView();
  13.
      }

JW

Thanks, i am closer to what i am doing. can i make form2 global so that i always refer to the same Form?

When you are creating an instance of the form, you are doing something like this?

Form2 form2 = new Form2();
            form2.Show();

In this example, Form2 should be given a public method. The code that created the instance of the form would be able to call that method. Similarly, the user control's public method could be accessed by the form.

// inside whatever created instance of Form2
   form2.UpdateData(dataInput);

   .....

   // inside Form2
   public void UpdateData(ArrayList dataInput)
   {
         myControl.UpdateControl(dataInput);
   }

   .....

   // inside User Control
   public void UpdateData(ArrayList dataInput)
   {
        // do whatever
   }

thanks for the suggestion.

Could you consider about inheriting classes like this:
after inheritation you should create:

TreeViewx1 t1=new class22();
   1.
      public partial class TreeViewx1 : UserControl
   2.
      {
   3.
      public void UpdateTreeView()
   4.
      {
   5.
      TreeView1.Nodes["test"].Nodes.Add(new TreeNode("test11"));
   6.
      }
   7.
      }
   8.
       
   9.
      class class22:TreeViewx1
  10.
      {
  11.
      //other algorithm
  12.
      UpdateTreeView();
  13.
      }

JW

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.