2

我还没有做过很多 C# 编程。不过,我非常擅长 C/C++。我无法弄清楚从项目中的其他类访问类成员的正确方法。例如,我有一个 addChannel() 类,它是一个弹出框,允许用户输入 Channel 类的信息。我有一个可以保存这些频道的 treeView。treeView 位于 ListView 类中,该类是其中包含树的主要形式。我在 addChannel 弹出窗口上有一个按钮,单击该按钮应添加一个新 Channel() 并将此通道作为新节点添加到树中。但是我根本无法访问树,也不知道如何访问。这是一些相关的代码。

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();
        }
    }
}

这是设计器的 ListView 部分类

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;
    }
}
4

3 回答 3

1

这不是 Windows 窗体的正常设置。

通常,您只需将 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 窗体会容易得多。如果你这样做,你会发现你想做的事情很容易。

于 2013-03-16T20:47:45.277 回答
0

当您创建 addChannel 类时,在运行它之前(使用 Show() 等),您需要将它与您想要访问的 listView 表单挂钩。2 个选项: 1. 将列表视图作为参数传递给 addChnel,然后再运行它。比你可以从 addCahnnel 调用方法到 listView。2.在你的addChannel类中创建一个事件并为这个事件注册listView(事件可以是:添加/删除频道等...)

选项 2 更好,但您需要学习如何使用事件和委托。见: http: //msdn.microsoft.com/en-us/library/aa645739 (v=vs.71).aspx

于 2013-03-16T20:51:44.897 回答
0

在构造函数中传递 TreeView

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
}

确保将 的DialogResult属性 设置button1为“确定”。您还可以将AcceptButton对话框表单的属性设置为button1; 这使用户能够使用 ENTER 键关闭表单。

于 2013-03-16T20:58:16.930 回答