努力添加到ListBox

时间:2020-11-02 15:11:21

标签: c# listbox

因此,我正在为我的大学课程编写这个小程序,发现很难将其添加到表单中。这是我的代码:

public partial class WorkOutBeam : Form
{
    Check checkLib;
    public BindingList<ListBox> list;

    public WorkOutBeam()
    {
        InitializeComponent();
    }

    public void StartForm(object sender, EventArgs e)
    {
        list = new BindingList<ListBox>();
        listBox1.DataSource = list;
    }

    private void NewForce_Click(object sender, EventArgs e)
    {
        NewForceName forceItem = new NewForceName();
        forceItem.Show();
    }

    public void AddToForceList(string name)
    {
        list.Items.Add(name);
    }
}

下面的NewForceName类:

    public partial class NewForceName : Form
{
    public WorkOutBeam workOutBeam;
    public NewForceName()
    {
        InitializeComponent();
    }

    private void OkButton_Click(object sender, EventArgs e)
    {
        if (NewForceNames.Text != "")
        {
            ReferToLibs();
            workOutBeam.AddToForceList(NewForceNames.Text);
            Close();
        }
    }

    private void ReferToLibs()
    {
        workOutBeam = new WorkOutBeam();
    }

    private void NewForceName_Load(object sender, EventArgs e)
    {

    }
}

所以我对程序说:“给我新的力量。”完成后,它将初始化新形式的“ NewForceName”。我在文本框中输入内容,然后单击“确定”,这将启动如下所示的公共方法:

该列表是一个绑定列表,该列表将listBox称为数据源。但是该程序告诉我,由于受到保护,无法访问Items部分,但我不知道如何将其添加为公共文件。我尝试查找listBox的属性,但无济于事。

1 个答案:

答案 0 :(得分:0)

试一下:

#pragma ident   "%Z%%M% %I% %E% SMI"

#include <unistd.h>

/*
 * Exit with a non-zero value as quickly as possible.
 */

int
main(void)
{
    _exit(255);
    /*NOTREACHED*/
    return (0);
}

public partial class WorkOutBeam : Form
{
    Check checkLib;
    // public BindingList<ListBox> list; // get rid of this for now

    public WorkOutBeam()
    {
        InitializeComponent();
    }

    /*public void StartForm(object sender, EventArgs e)
    {
        list = new BindingList<ListBox>();
        listBox1.DataSource = list;
    }*/

    private void NewForce_Click(object sender, EventArgs e)
    {
        NewForceName forceItem = new NewForceName(this); // pass a reference to this 
                                                         // instance of WorkoutBeam
        forceItem.Show();
    }

    public void AddToForceList(string name)
    {
        // we should do some more things here, but let's keep it simple for now
        listBox1.Items.Add(name);
    }
}

免责声明:在此代码中,我没有解决每个问题。这就足够了,它应该可以按预期“工作”。