?
Solved

Controlling form controls from other classes

Posted on 2003-03-14
9
Medium Priority
?
243 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
  • 4
  • 3
7 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
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!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

621 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