【问题标题】:Struggling to add to ListBox努力添加到 ListBox
【发布时间】:2020-11-02 15:11:21
【问题描述】:

所以我正在为我在大学的作业制作这个小程序,但我发现很难在我的表单中添加到我的列表中。这是我的代码:

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”。我在一个文本框中输入并单击“确定”,这将启动一个公共方法,如下所示:

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

【问题讨论】:

  • list 是您绑定到listBox 还是listBox 本身的列表?如果list 只是一个列表,那么试试list.Add(name)
  • @Fildor 'listBox' 绑定到 'list'。
  • 你是怎么做到的?它们到底是什么类型?
  • @Fildor list = new BindingList&lt;ListBox&gt;(); 然后listBox1.DataSource = list;
  • @Fildor 我已经编辑了一点,我认为它是可重现的。

标签: c# listbox


【解决方案1】:

试一试:

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

public partial class NewForceName : Form
{
    public WorkOutBeam workOutBeam;
    public NewForceName( WorkoutBeam beam ) // we take a WorkoutBeam instance as CTOR param!
    {
        InitializeComponent();
        workoutBeam = beam;
    }

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

    // DO NOT create new WorkoutBeams every time. Use the original.
    /*private void ReferToLibs()
    {
        workOutBeam = new WorkOutBeam();
    }*/
}

免责声明:我没有解决此代码中的每一个问题。这足以让它按预期“工作”。

【讨论】:

  • 最好的有 100k+ :) 但我很乐意提供帮助。
猜你喜欢
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多