无法从类方法中访问列表框 1

本文关键字:列表 访问 类方法 | 更新日期: 2023-09-27 18:31:56

如何从类方法访问listbox1?我正在尝试学习 C#,我不想让这个问题悬而未决。谢谢!

    //Classes
    public class Animal
    {
        public  string name;
        public int age;
        public int count;
        //Class Contructors
        public Animal() //Default Constructor
        {
            name = "Oz";
            age = 6;
            count ++;
        }
        public Animal(string _name, int _age)
        {
            name = _name;
            age = _age;
        }
        //Class Methods
        public void Print()
        {
            MessageBox.Show("Name: " + name);
            listbox1.......
        }
    }

更新:根据要求添加了设计器.cs代码。不知道要添加哪些更多详细信息,但是没有这个它不会让我发布。

    namespace CSharpExamples
{
partial class Form1
{
    private System.ComponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
        this.btnMethods = new System.Windows.Forms.Button();
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.btnClasses = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.btnMethods.Location = new System.Drawing.Point(12, 12);
        this.btnMethods.Name = "btnMethods";
        this.btnMethods.Size = new System.Drawing.Size(276, 23);
        this.btnMethods.TabIndex = 0;
        this.btnMethods.Text = "Methods";
        this.btnMethods.UseVisualStyleBackColor = true;
        this.btnMethods.Click += new System.EventHandler(this.btnMethods_Click);
        this.listBox1.FormattingEnabled = true;
        this.listBox1.Location = new System.Drawing.Point(404, 12);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(182, 238);
        this.listBox1.TabIndex = 1;
        this.btnClasses.Location = new System.Drawing.Point(12, 41);
        this.btnClasses.Name = "btnClasses";
        this.btnClasses.Size = new System.Drawing.Size(276, 23);
        this.btnClasses.TabIndex = 2;
        this.btnClasses.Text = "Classes";
        this.btnClasses.UseVisualStyleBackColor = true;
        this.btnClasses.Click += new System.EventHandler(this.btnClasses_Click_1);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(598, 262);
        this.Controls.Add(this.btnClasses);
        this.Controls.Add(this.listBox1);
        this.Controls.Add(this.btnMethods);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }
    #endregion
    private System.Windows.Forms.Button btnMethods;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button btnClasses;
}
}

无法从类方法中访问列表框 1

您正在尝试从Animal类访问listBox1,该类不知道它是什么,并且会给出编译器错误。如果您只想让列表框显示名称,您可以尝试如下操作:

class Animal
{
    public string name { get; set; } //make these properties. 
    public int age { get; set; }
    public int count { get; set; }
    //Class Contructors
    public Animal() //Default Constructor
    {
        name = "Oz";
        age = 6;
        count++;
    }
    public Animal(string _name, int _age)
    {
        name = _name;
        age = _age;
    }
    //Class Methods
    public void Print()
    {
        MessageBox.Show("Name: " + name);
    }
}

然后在主窗体上,您可以创建一个新的动物,然后使用以下名称将其添加到列表中:

public Form1()
{
    InitializeComponent();
    Animal an = new Animal();
    listBox1.Items.Add(an.name);
}

由于我不完全理解这样做的目的,所以我可能在这里偏离了基础。但是,您可以在类中包含using System.Windows.Forms;,以便可以将列表框参数添加到 Print 方法。

public void Print(ListBox lb)
{
    lb.Items.Add(name);
}

然后在列表框所在的表单中,您可以传递它

Animal pet = new Animal();
pet.Print(listBox1);

但是,我没有得到这个的原因是我觉得你最好覆盖.ToString() 方法显示您想要的内容,但将对象本身添加到列表框中。这样,在列表中选择项目后,您可以访问为动物定义的所有属性。

因此,您可以使用公共无效打印

代替公共无效打印

public override string ToString()
{
    return name;
}

然后在您的表单中,您只需将动物的实例添加到列表框中

Animal pet = new Animal("Fido", 3);
listBox1.Items.Add(pet)

现在您的列表框显示 Fido,但也保留值 3

然后,只需将选定项投射回动物,您就可以访问年龄属性和您决定添加的任何其他属性。

另外,是否有理由不使用属性而不是名称、年龄和计数字段?