2013-03-16 63 views
2

我還沒有做很多C#編程。不過,我非常擅長C/C++。我找不到從項目中的其他類訪問類成員的正確方法。例如,我有一個類addChannel(),它是一個彈出框,允許用戶輸入Channel類的信息。我有一個樹形視圖,可以容納這些頻道。 TreeView是一個ListView類,它是樹中的主要形式。我在addChannel彈出窗口中有一個按鈕,單擊它時應添加一個新的Channel()並將此通道作爲新節點添加到樹中。但是我根本無法訪問樹,也不知道如何。這裏有一些相關的代碼。訪問C#Windows中的其他類窗體

namespace RSSReader 
{ 
    public partial class addChannel : Form 
    { 
     public addChannel() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      // Save the info to an XML doc 

      // I want to access the channelTree treeView here 
      this.Close(); 
     } 
    } 
} 

這裏是設計師

namespace RSSReader 
{ 
    partial class ListView 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 


      // ALL THE INITIALIZATION IS HERE... I excluded it 

     public System.Windows.Forms.TreeView channelTree; 
     private System.Windows.Forms.WebBrowser webBrowser; 
     private System.Windows.Forms.Button addBtn; 
     private System.Windows.Forms.Button setBtn; 
     private System.Windows.Forms.Button remBtn; 
     private System.Windows.Forms.RadioButton titleFilter; 
     private System.Windows.Forms.RadioButton dateFilter; 
    } 
} 

回答

1

這是不正常的設置爲Windows窗體ListView控件部分類。

通常情況下,你只需拖動TreeView控件到窗體,拖動按鈕到窗體,並將得到的代碼會給你任何麻煩訪問任何事情:

namespace RssReader 
{ 
    public partial class addChannel : Form 
    { 
     public addChannel() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      treeView1.ItemHeight = 6; 
     } 
    } 
} 

這裏是後臺代碼:

namespace RssReader 
{ 
    partial class addChannel 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

                                            #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.treeView1 = new System.Windows.Forms.TreeView(); 
     this.button1 = new System.Windows.Forms.Button(); 
     this.SuspendLayout(); 
     // 
     // treeView1 
     // 
     this.treeView1.Location = new System.Drawing.Point(12, 12); 
     this.treeView1.Name = "treeView1"; 
     this.treeView1.Size = new System.Drawing.Size(121, 97); 
     this.treeView1.TabIndex = 0; 
     // 
     // button1 
     // 
     this.button1.Location = new System.Drawing.Point(13, 116); 
     this.button1.Name = "button1"; 
     this.button1.Size = new System.Drawing.Size(75, 23); 
     this.button1.TabIndex = 1; 
     this.button1.Text = "button1"; 
     this.button1.UseVisualStyleBackColor = true; 
     this.button1.Click += new System.EventHandler(this.button1_Click); 
     // 
     // addChannel 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(284, 262); 
     this.Controls.Add(this.button1); 
     this.Controls.Add(this.treeView1); 
     this.Name = "addChannel"; 
     this.Text = "Form1"; 
     this.ResumeLayout(false); 

    } 

    #endregion 

     private System.Windows.Forms.TreeView treeView1; 
     private System.Windows.Forms.Button button1; 
    } 
} 

如果您遵循Visual Studio設計器爲您實現的模式,Windows窗體會輕鬆得多。如果你這樣做,你會發現你想要做的很容易。

+0

但我的treeView並沒有分開addChannel類.. addChannel只是一個簡單的彈出窗體來獲得標題和描述。我不想在addChannel中創建一個新的TreeView,因爲我已經在我的ListView中有一個了。 – 2013-03-18 01:39:03

+0

我像你說的那樣將treeView和Add按鈕拖放到我的ListView上。但是addChannel是一個新的Class,當點擊Add按鈕時它彈出 – 2013-03-18 01:41:34

0

當您創建addChannel類並在運行它之前 (使用Show()等..),您需要將它與您想要到達的listView窗體掛鉤。 2選項: 1.在運行之前將listview作爲參數傳遞給addChnel。比你可以將方法從addCahnnel調用到listView。 2.在addChannel類中創建一個事件,併爲此事件註冊listView(事件可以是:channel added/remove etc ...)

選項2更好,但您需要學習如何使用活動和代表。見: http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

0

傳遞樹視圖在構造

public partial class addChannel : Form 
{      
    private TreeView _treeView; // TreeView on other Form. 

    public addChannel(TreeView treeView) 
    { 
     InitializeComponent(); 
     _treeView = treeView; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // Save the info to an XML doc 

     // Access _treeView here 
     Console.WriteLine(_treeView.Name); 

     this.Close(); 
    } 
} 

完全不同的方法是添加一個公共屬性暴露所述選擇的信道。你不會從addChannel形式訪問的TreeView可言,但做這項工作的主要形式單獨

public partial class addChannel : Form 
{      
    public addChannel() 
    { 
     InitializeComponent(); 
    } 

    public Channel SelectedChannel { get; private set; } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // Save the info to an XML doc 

     SelectedChannel = theChannel; 
     this.Close(); 
    } 
} 

在主窗體,你會做這樣的事情:

var fdlg = new addChannel(); 
if (fdlg.ShowDialog(this) == DialogResult.OK) { 
    this.treeView.Add(fdlg.SelectedChannel); // Or something similar 
} 

確保將button1DialogResult屬性設置爲「OK」。您也可以將對話框窗體的AcceptButton屬性設置爲button1;這使用戶可以用ENTER鍵關閉窗體。