2012-03-16 55 views
1

我工作在Windows訪問樹視圖控件形式的C#應用​​程序使用Visual Studio 2010C# - 從另一個類

  • 有一個表單mainForm
  • mainForm包含樹視圖控件xmlTreeView
  • 有一個自己寫的類myClass.cs

現在,myClass需要訪問xmlTreeView。但是我不知道a)如何訪問表單和b)哪種方式最適合這樣做。

我試圖實現一個接口後面oleksii's answer但我不明白。該應用的主要形式是這樣定義的:

public interface IMainForm { 
    TreeView treeView { get; } 
} 

public partial class mainForm : Form, IMainForm { 

    public TreeView treeViewControl { 
    get { return myTreeViewControl; } 
    } 

    // Some code here 
    [...] 

    RuleTree rt = new RuleTree(); //How do I call this with the IMainForm interface??? 
} 

另一類RuleTree的定義是這樣的:

class RuleTree { 
    private readonly IMainForm mainForm; 
    public RuleTree(IMainForm mainForm) { 
    this.mainForm = mainForm; 
    } 

}

如何調用RuleTree構造與IMainForm接口???

回答

2

我會做以下事情。不要把它看作代碼,只是爲了讓你能理解,你可以相應地修改它。

public class MyClass 
{ 
    public void MyMethod(YourTreeViewControl treeview) 
    { 
      // Do what you need to do here 
    } 
} 

然後在你的代碼的形式僅僅落後MyClass的實例,並通過您的樹狀的實例吧,這樣的事情:

MyClass myClass = new MyClass(); 
myClass.MyMethod(tvYourTreeViewControl); 

希望這是有道理:)

+0

是的,這是有道理的,因爲我也考慮到了這一點。我會給這個嘗試和oleksii的答案。將找出哪些更適合我的需求:-) – 2012-03-16 08:49:38

-2

這是不可能的訪問asp.net服務器端控件到其他其他類,那麼他們的CS類如 test.aspx是一個頁面 只能在test.aspx.cs 其他那麼這個類訪問測試頁面控件這是不可能的。

+1

它不是ASP應用程序,而是WindowsForms。 – 2012-03-16 08:14:17

+0

這不是一個ASP.NET問題,所以你的答案是無關的。 – 2012-03-16 08:36:18

+0

@BrendanVogt你還好嗎? Windows窗體也是一個asp.net應用程序。 ASP。淨只是一種技術沒有別的。 – 2012-03-16 11:35:25

1

其中一種可能的方法是在這裏使用依賴注入。 MyClass將有一個構造函數,其參數爲Form。因此,當您創建MyClass時,它將以的形式注入。例如:

Foo 
{ 
    Foo(){} 
} 

Bar 
{ 
    private Foo currentFoo; 

    Bar(Foo foo) //dependency injection 
    { 
     currentFoo = foo; 
    } 

    public void OtherMethod() 
    { 
     //do something with currentFoo 
    } 
} 

這將是更好地使用接口(或抽象類),所以不是Foo你可以注入IFoo,這在很大程度上解耦你的類,這是一個很好的設計決策。

+0

我想嘗試一下實現一個接口,但不知何故,我卡住了。這是我選擇布倫丹的答案:-) – 2012-03-17 16:11:19

0

我有評論我的代碼請閱讀評論,我也可以提供解決方案。

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Windows.Forms; 

    namespace WindowsFormsApplication1 
    { 
     static class Program 
     { 


    /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     /// 

     //Declare a static form that will accesible trhought the appication 
     //create form called frmMain form or any other name 
     // 
     public static frmMain MainForm { get; private set; } 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      //comment out default application run 
      //Application.Run(new MainForm()); 

      //create a new instance of your frmMain form 
      //inside your main form add a tree view 
      //Loacte this file "frmMain.Designer.cs" 
      //Change treeView1 from private to public 
      // public System.Windows.Forms.TreeView treeView1; 
      MainForm = new frmMain(); 

      //before I show my form I'll change docking of my tree view from myClass 
      MyClass mine = new MyClass(); //done 

      MainForm.ShowDialog(); 

     } 


    } 

    public class MyClass 
    {   
     public MyClass() 
     { 
      Program.MainForm.treeView1.Dock = DockStyle.Fill; 
     } 

    } 
}