访问C#Windows窗体中的其他类

时间:2013-03-16 20:37:32

标签: c#

我还没有完成很多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;
    }
}

3 个答案:

答案 0 :(得分:1)

这不是Windows Forms的正常设置。

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

答案 1 :(得分:0)

创建addChannel类时,以及运行之前 (使用Show()等..),你需要将它与你想要的listView表单挂钩。 2个选项: 1.在运行之前将listview作为参数传递给addChnel。你可以调用addCahnnel到listView的方法。 2.在addChannel类中创建一个事件并为此事件注册listView(事件可以是:添加/删除通道等...)

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

答案 2 :(得分: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的{​​{1}}属性设置为&#39;确定&#39;。您还可以将对话框表单的button1属性设置为AcceptButton;这使用户可以使用ENTER键关闭表单。