简体   繁体   English

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

[英]Accessing other classes in C# Windows Form

I haven't done a whole lot of C# programming. 我还没有完成很多C#编程。 I am very good at C/C++ though. 我虽然擅长C / C ++。 I cannot figure out the proper way to access Class members from other classes within the project. 我无法弄清楚从项目中其他类访问类成员的正确方法。 For example, I have a class addChannel() which is a pop up box that allows the user to enter information for a Channel class. 例如,我有一个类addChannel(),它是一个弹出框,允许用户输入Channel类的信息。 I have a treeView that will hold these channels. 我有一个treeView可以容纳这些频道。 The treeView is in a ListView class which is the main form with the tree in it. treeView在ListView类中,该类是其中包含树的主要形式。 I have a button on the addChannel pop-up window that, when clicked, should add a new Channel() and add this channel as a new Node to the tree. 我在addChannel弹出窗口上有一个按钮,单击该按钮时,应添加一个新Channel()并将此通道作为新节点添加到树中。 However I cannot access the tree at all and do not know how. 但是我根本无法访问树,也不知道如何。 Here is some relevant code. 这是一些相关的代码。

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

And here is the ListView partial class for the designer 这是设计器的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;
    }
}

This is not the normal set-up for Windows Forms. 这不是Windows窗体的常规设置。

Normally, you would just drag the TreeView onto the form, drag the buttons onto the form, and the resulting code would give you no trouble accessing anything: 通常,您只需将TreeView拖到窗体上,将按钮拖到窗体上,然后生成的代码将使您访问任何内容都没有问题:

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

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

Here is the code-behind: 下面是代码:

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

Windows Forms is a lot easier if you follow the patterns which the Visual Studio designer implements for you. 如果遵循Visual Studio设计器为您实现的模式,则Windows窗体会容易得多。 If you do it this way, you will find what you're trying to do very easy. 如果以这种方式进行操作,您会发现尝试做的事情非常容易。

When you create the addChannel class , and before running it (using Show() etc..) , you need to hook it up with the listView form you want to get to. 当创建addChannel类时,并在运行它之前(使用Show()等。),您需要将其与想要获取的listView表单挂钩。 2 options: 1. Pass the listview as a parameter to addChnel before running it. 2个选项:1.在运行列表视图之前,将列表视图作为参数传递给addChnel。 than you can call methods from addCahnnel to listView. 您可以将方法从addCahnnel调用到listView。 2. Create an event in you addChannel class and register the listView for this event (event can be: channel added/removed etc...) 2.在您的addChannel类中创建一个事件,并为此事件注册listView(事件可以是:添加/删除了通道等)。

Option 2 is better , but you will need to learn how to work with events and delegates. 选项2更好,但是您将需要学习如何处理事件和委托。 see: http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx 请参阅: http : //msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

Pass the TreeView in the constructor 在构造函数中传递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();
    }
}

A completely different approach is to add a public property exposing the selected channel. 完全不同的方法是添加一个公开属性,以暴露所选频道。 You would not access the TreeView from the addChannel form at all, but do this work on the main form alone 您根本不会从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();
    }
}

In the main form you would do something like this: 在主要形式中,您将执行以下操作:

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

Make sure to set the DialogResult property of button1 to 'OK'. 确保将button1DialogResult属性设置为“确定”。 You cann also set the AcceptButton property of the dialog form to button1 ; 您也可以将对话框表单的AcceptButton属性设置为button1 this enables the user to close the form with the ENTER-key. 这使用户可以使用ENTER键关闭表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM