【问题标题】:How to Update a Listbox from another Listbox如何从另一个列表框更新列表框
【发布时间】:2017-08-17 07:02:31
【问题描述】:

我在两个单独的表单中有两个列表框一旦填写了一个并使用了按钮单击事件,我希望在这种情况下来自该列表框的数据将一堆分数传输到另一个表单。到目前为止,我的问题是将第二种形式的列表框转换为数组列表。

代码如下:

表格1

Form3 newForm1 = new Form3(this); 
newForm1.ShowDialog();

foreach (string s in newForm1.updateStudentInfo)
{
    listForm1.Items.Add(s);
}

表格2:

public List<string> updateStudentInfo { get; set; }

    string student = txtName.Text;
            string[] stuffFromList = listboxscores // Unsure how to transfer into an arraylist


            for (int i = 0; i < stuffFromList.Length; i++)
            {
                student = student + "|" + form3.listForm1.Items.Add(stuffFromList[i]);
            } 

            updateStudentInfo.Add(student);
            form3.listForm1.Items.RemoveAt(form3.listForm1.SelectedIndex);
            this.Close();
}

【问题讨论】:

  • 抱歉,如有任何混淆,我的主表格中的学生看起来像这样 Jame|22|45|66 当我们按下更新时,它会移动到另一个带有名称文本框的表格,学生的分数在列表框。一旦他们被编辑,我按下 OK 按钮,目前它只将名称转移回来,我也需要得分。

标签: c# wpf listbox


【解决方案1】:

那么您真正需要帮助的是将列表框项放入数组/列表中?这应该这样做: 这当然是假设您的其余代码有效。

List<string> items = new List<string>();
if (lbScores.Items != null && lbScores.Items.Count > 0)
{
    foreach (var item in lbScores.Items)
    {
        items.Add(item.ToString());
    }
}

在这里我重写了代码,以便您更好地理解。

private void btnOK_Click(object sender, EventArgs e)
{
    string student = txtName.Text;
    if (lbScores.Items != null && lbScores.Items.Count > 0)
    {
        foreach (var item in lbScores.Items)
        {
            student = student + "|" + item.ToString();
        }
    }

    updateStudentInfo.Add(student);
    form3.listForm1.Items.RemoveAt(form3.listForm1.SelectedIndex);
    this.Close();
}

【讨论】:

  • 对不起,这会是哪种形式? C# 新手请耐心等待。
  • 你的问题很难理解,但是这段代码应该代替:string[] stuffFromList = listboxscores.Text; // I don't know what to do here
  • 对不起,我是这个网站的新手,我需要让事情更简洁
  • 请重写你原来的问题,清楚地定义你的问题和你想做什么,然后我可以更好地帮助你。事实上,这很难理解。
  • 在我的 string[] 数组中,我的 For 循环需要 stuffFromList,当我在上面使用 foreach 循环时,我用什么来代替它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多