[英]Accessing other classes in C# Windows Form
我還沒有完成很多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;
}
}
這不是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窗體會容易得多。 如果以這種方式進行操作,您會發現嘗試做的事情非常容易。
當創建addChannel類時,並在運行它之前(使用Show()等。),您需要將其與想要獲取的listView表單掛鈎。 2個選項:1.在運行列表視圖之前,將列表視圖作為參數傳遞給addChnel。 您可以將方法從addCahnnel調用到listView。 2.在您的addChannel類中創建一個事件,並為此事件注冊listView(事件可以是:添加/刪除了通道等)。
選項2更好,但是您將需要學習如何處理事件和委托。 請參閱: http : //msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
在構造函數中傳遞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
}
確保將button1
的DialogResult
屬性設置為“確定”。 您也可以將對話框表單的AcceptButton
屬性設置為button1
; 這使用戶可以使用ENTER鍵關閉表單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.