gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:38 24 4
gpt4 key购买 nike

我没有做过很多 C# 编程。不过,我非常擅长 C/C++。我想不出从项目中的其他类访问类成员的正确方法。例如,我有一个 addChannel() 类,它是一个弹出框,允许用户输入 Channel 类的信息。我有一个 treeView 将容纳这些 channel 。 treeView 位于 ListView 类中,该类是其中包含树的主要形式。我在 addChannel 弹出窗口上有一个按钮,单击该按钮时,应该添加一个新的 Channel() 并将此 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 窗体会容易得多。如果你这样做,你会发现你想做的事情非常容易。

关于c# - 在 C# Windows 窗体中访问其他类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15454457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com