2014-09-30 73 views
0

好吧,所以我的應用程序是一個簡單的嬰兒禮物註冊表。在左邊的組框中,我有2個輸入文本框(item,store)和一個listBox(lstWish)用於輸出。這邊是願望清單。在正確的groupBox中,我有2個輸入文本框(firstName,lastName)和一個listBox(lstPurchased)用於輸出。在button_click上,從lstWish中選擇的項目被移動到lstPurchased。 lstWish目前輸出如「商店可用的商品」。將項目從一個列表框移動到另一個列表框時,如何添加到進入第二個列表框的字符串中?

當物品從lstWish變爲lstPurchased時,我想添加「from firstName lastName」。因此,lstPurchased將讀取「Store from firstName lastName」中提供的商品。如何在移動字符串的同時添加字符串的後半部分?

下面是在移動項目的方法:

private void MoveListBoxItems(ListBox lstWish, ListBox lstPurchased) 
    { 
     ListBox.SelectedObjectCollection sourceItems = lstWish.SelectedItems; 
     foreach (var item in sourceItems) 
     { 
      lstPurchased.Items.Add(item); 
     } 
     while (lstWish.SelectedItems.Count > 0) 
     { 
      lstWish.Items.Remove(lstWish.SelectedItems[0]); 
     } 
    } 

這裏是按鈕單擊代碼:

private void btnBuy_Click(object sender, EventArgs e) 
    { 
     if (txtFirst.Text == "" || txtLast.Text == "") 
     { 
      MessageBox.Show("Please enter first and last name.", "Warning"); 
      this.DialogResult = DialogResult.None; 
     } 
     else 
     { 
      DialogResult result = MessageBox.Show("Click Yes to confirm purchase. Click No to cancel." 
       , "Thank You", MessageBoxButtons.YesNo); 
      if (lstWish.SelectedIndex >=0) 
      { 
       MoveListBoxItems(lstWish, lstPurchased);     
      } 
      else 
      { 
       return; 
      } 
     } 
    } 

該列表是一個集合是一個列表。有兩個自定義類,Gift和Guest。這裏是具有用於填充ListBox中的toString類禮品:

namespace GiftRegistry 
{ 
class Gift 
{ 
    #region Fields 
    private string _item; 
    private string _store; 
    //private string _purchaser; 
    #endregion 

    #region Properties 
    public string Item { get; set; }//modify the set clauses to include regex and title case and   .trim() 

    public string Store { get; set; } 

    //public string Purchaser { get; set; } 
    #endregion 

    #region Constructors 
    public Gift() 
    { 
     this.Item = String.Empty; 
     this.Store = String.Empty; 
    } 

    public Gift(string Item, string Store) 
    { 
     this.Item = Item; 
     this.Store = Store; 
    } 
    #endregion 

    #region Method 
    public override string ToString() 
    { 
     return string.Format("{0} availble at {1}", this.Item, this.Store); 
    } 
    #endregion   
} 

}

遊客等級:

class Guest 
{ 
    #region Fields 
    private string _lastName; 
    private string _firstName; 
    #endregion 

    #region Properties 
    public string LastName 
    { 
     get { return _lastName; } 
     set 
     {     
      if (value == null) 
       throw new ArgumentNullException("Name", "Please enter a name"); 
      _lastName = value.Trim(); 
     } 
    } 
    public string FirstName 
    { 
     get { return _firstName; } 
     set 
     {//see above about regular expressions 
      if (value == null) 
       throw new ArgumentNullException("Name", "Please enter a name"); 
      _firstName = value.Trim(); 
     } 
    } 
    #endregion 

    #region Constructors 
    public Guest() 
    { 
     this.LastName = String.Empty; 
     this.FirstName = String.Empty; 
    } 

    public Guest(string lastName) 
    { 
     this.LastName = lastName; 
     this.FirstName = String.Empty; 
    } 

    public Guest(string lastName, string firstName) 
    { 
     this.LastName = lastName; 
     this.FirstName = firstName; 
    } 
    #endregion 

    #region Method 
    public override string ToString() 
    { 
     return string.Format("{0} {1}", this.FirstName, this.LastName); 
    } 
    #endregion 

} 
+1

字符串是一個字符串。你通常如何加入兩個字符串? 'str1 + str2'。就是這樣,就是這樣。第一個字符串是'item',所以只是連接第二個字符串,然後添加結果。你試圖採取一些簡單的事情,並努力工作。 – jmcilhinney 2014-09-30 01:26:09

+0

我用另一個應用程序中的string.join完成了它,但是我無法在這裏工作。我會在我的主窗體中編寫一個覆蓋方法,還是會從Guest類驅動? – ScottT 2014-09-30 01:38:00

+0

就像我說過的,當你嘗試做最簡單的事情時,你就會努力做到這一點。你不會使用'String.Join',因爲這是爲了連接一個值列表和它們之間的分隔符。這不是你想要做的。你只需要連接兩個字符串,只需使用'+'運算符即可。你在這裏添加項目:'lstPurchased.Items.Add(item);'。只需將其更改爲:'lstPurchased.Items.Add(item + str2);'。 'item'需要是一個字符串,你如何得到第二個字符串取決於你,但它很簡單。 – jmcilhinney 2014-09-30 01:49:33

回答

0

MoveListBoxItems(),您可以連接名字和姓氏有:

lstPurchased.Items.Add(string.Format("{0} from {1} {2}", item.ToString(), firstName, lastName)); 
+0

這不會把整個字符串放在我的第一個listBox(lstWish)中嗎? – ScottT 2014-09-30 02:00:48

+0

我在OP的評論中得到了建議,但爲了教育的興趣,我想更多地瞭解你的想法。如何讓我的表單代碼從我的Guest類中識別firstName,lastName?我得到它像這樣工作:lstPurchased.Items.Add(string.Format(「{0} from {1} {2}」,item.ToString(),txtFirst.Text,txtLast.Text)); – ScottT 2014-09-30 02:23:03

+0

@ScottT,如果你可以發佈你的Guest類的源代碼,我很樂意看看它。 – 2014-09-30 02:35:07

相關問題