?
Solved

Controlling form controls from other classes

Posted on 2003-03-14
9
Medium Priority
?
242 Views
Last Modified: 2010-04-15
Maybe this is a stupid question, maybe not...

I am working with a program that has multiple tree controls.

I want to create a seperate class, in a seperate namespace, that would allow me to control these tree controls.

I know I can do this with static methods inside a seperate class but...
What I would like to do is pass a reference to the tree control into a new instance of the class object and manipulate the control from within the new instance.

I would really like to know how to do this just for the knowledge.
I have been playing around with it all day and can't quite get it.
0
Comment
Question by:EBurritt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 

Expert Comment

by:mystar
ID: 8142356
Hello.
for example namespace of your project is "test1".
you want to create a class with another namespace:
do following instruction.
Create a class with a name :for example myclass
you can change namespace of this class:

using system;
using system.windows.forms;
namespace mytree
{
 public class myclass
  {
   public myclass()
   {}
   //add any methods
   //don't need you create a static method.you can create
  //methods and write any code that do your works
  }
}
in another class, you must using mytree namespace:
for example :

using system;
using system.windows.forms;
using mytree

namespace  test
{
public class mytest
{
  private class1 c = new class1();
  public mytest()
  {}

}

}
0
 

Expert Comment

by:mystar
ID: 8142360
Hello.
for example namespace of your project is "test1".
you want to create a class with another namespace:
do following instruction.
Create a class with a name :for example myclass
you can change namespace of this class:

using system;
using system.windows.forms;
namespace mytree
{
 public class myclass
  {
   public myclass()
   {}
   //add any methods
   //don't need you create a static method.you can create
  //methods and write any code that do your works
  }
}
in another class, you must using mytree namespace:
for example :

using system;
using system.windows.forms;
using mytree;

namespace  test1
{
public class mytest
{
  private class1 c = new class1();
  public mytest()
  {}

}

}
0
 

Author Comment

by:EBurritt
ID: 8147239
Thanks for the try but...

Maybe I was not clear enough in my question.

I have a form - form1.
On this form I have a treeview control - tree1.
This form and control are created in a class - formClass1.
This class is in a namespace - formNamespace1.

Now I have a seperate namespace - treeNamespace1.
In this namespace I have a class - treeClass1.
Within this class I want to add a node to the treeview control in the other class - formClass1 of the first namespace.

My question is - How do I refer to the treeview control on the form that was created in formClass1 from treeClass1?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
mystar earned 100 total points
ID: 8149491
hello.

you must define treeView  as follow:
public static TreeView treeview = new TreeView();

and another class write follow code:
formClass1.treeView1.Nodes.Add("1");

0
 

Author Comment

by:EBurritt
ID: 8152147
Thats very good, now for the final piece:

Is there a way to pass a reference to the TreeView control into the second class.

For example:
Lets assume "tree1" is the name of the TreeView control on a form called "form1".

class1
...
    private class2 tV = new class2();
    tV.addNewNode(tree1, "1");
    ...

class2
...
    public void addNewNode(TreeView tV2, nodeValue string)
        tV2.Nodes.Add("1");
        ...

Doing it this way would let me control multiple TreeView controls from the same instance of the class2 class.

See how I am trying to pass a reference to the "tree1" control into the second class. This is what I cannot get to work.
0
 

Expert Comment

by:mystar
ID: 8156908
you can define treeview in class1 as follow:
public class1
{
public static System.Windows.Forms.TreeView tv = new
   System.Windows.Forms.TreeView();

//add any code
}

public class2
{
 public void addNewNode(nodeValue string)
 {
       Form1.tv.Nodes.Add(nodeValue);
 }
}
0
 

Author Comment

by:EBurritt
ID: 8159946
I understand that, but is there a way to pass a reference to "tv" in your example:

public class2
{
public void addNewNode(nodeValue string)
{
      Form1.tv.Nodes.Add(nodeValue);
}
}

Because; lets say that I have four TreeView controls on form1 - tv1, tv2, tv3, tv4; I could use the same class to control all four TreeView controls.

Likewise, if I develop another program that has a TreeView control called "tree1" - I could use this same class to control tree1 also just by passing a reference to the specific control.

Something like this:

public void addNewNode(???? tv,string nodeValue)

I need to pass form1.tv as a parameter through to the class.

I know how to pass a reference to the entire form through to the class, but that doesn't help me because I still have to explicitly refer to each TreeView control by name.

If I am being difficult, I certainly do apologize.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question