无法从类方法中访问listbox1

时间:2016-01-26 19:33:41

标签: c#

如何从类方法中访问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.......
        }
    }

更新:根据要求添加了Designer.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;
}
}

2 个答案:

答案 0 :(得分:1)

您正在尝试从listBox1类访问Animal,它不知道它是什么,并会给编译器错误。如果您只是希望列表框显示名称,您可以尝试这样的事情:

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

答案 1 :(得分:0)

由于我没有完全理解这个目的,我可能会离开这里。但您可以在类中添加using System.Windows.Forms;,以便可以向Print方法添加listbox参数。

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

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

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

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

因此,您可以使用

代替公共无效打印
public override string ToString()
{
    return name;
}

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

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

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

然后通过简单地将SelectedItem强制转换为Animal,您可以访问age属性以及您决定添加的任何其他属性。

另外,为什么你没有使用属性而不是字段来表示姓名,年龄和数量?