2013-03-16 64 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键关闭窗体。