2017-08-16 538 views
0

我有兩個單獨的窗體中的兩個列表框一旦填寫了一個按鈕並單擊事件,我希望來自該列表框中的數據在此實例中將一組分數傳輸到其他形式。到目前爲止,我的問題是將我的第二種形式的列表框轉換爲數組列表。如何從另一個列表框中更新列表框

下面的代碼:

Form1中

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(); 
} 
+0

遺憾的任何困惑我有個同學在主窗體看起來像這樣詹姆斯| 22 | 45 | 66當我們按下更新其移動到另一種形式與名稱文本框和列表框的學生的分數。一旦編輯完成,我按下確定按鈕,目前它只傳回名稱,我也需要分數。 – User3283827

回答

0

那麼你真的需要越來越列表框中的項目到一個數組/列表幫助嗎?這應該這樣做: 這當然是假設你的代碼的其餘部分工作。

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(); 
} 
+0

哪種形式會對不起?新的C#支持我。 – User3283827

+0

你的問題很難理解,但是這段代碼應該代替:'string [] stuffFromList = listboxscores.Text; //我不知道該怎麼做。 – Sach

+0

對不起,我對這個網站是新手我需要把事情做得更簡潔 – User3283827

相關問題