2

私は C# プログラミングをあまりやったことがありません。私はC / C ++がとても得意です。プロジェクト内の他のクラスからクラス メンバーにアクセスする適切な方法がわかりません。たとえば、ユーザーが Channel クラスの情報を入力できるポップアップ ボックスである addChannel() クラスがあります。これらのチャネルを保持する 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

コンストラクターで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();
    }
}

まったく異なるアプローチは、選択したチャネルを公開するパブリックプロパティを追加することです。フォームからTreeViewにアクセスすることはまったくありませんaddChannelが、この作業はメインフォームのみで行います。

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「OK」に設定してください。AcceptButtonダイアログフォームのプロパティをbutton1;に設定することもできません。これにより、ユーザーはENTERキーを使用してフォームを閉じることができます。

于 2013-03-16T20:58:16.930 に答える
0

addChannel クラスを作成するときは、(Show() などを使用して) 実行する前に、目的の listView フォームに接続する必要があります。2 つのオプション: 1. addChnel を実行する前に、listview をパラメータとして 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 に答える