[英]When moving an item from one listbox to another, how do I add to the string going into the second listbox?
Ok, so my application is a simple baby gift registry. 好的,所以我的应用程序是一个简单的婴儿礼物注册表。 In the left groupBox, I have 2 input text boxes(item, store) and a listBox(lstWish) for output. 在左边的groupBox中,我有2个输入文本框(item,store)和一个listBox(lstWish)用于输出。 This side is the Wish List. 这边就是愿望清单。 In the right groupBox, I have 2 input text boxes(firstName, lastName) and a listBox(lstPurchased) for output. 在右边的groupBox中,我有2个输入文本框(firstName,lastName)和一个listBox(lstPurchased)用于输出。 Upon a button_click, the selected item from lstWish is moved to lstPurchased. 单击button_click后,将从lstWish选中的项目移至lstPurchased。 lstWish currently outputs like "Item available at Store". lstWish当前输出类似“商店中可用的商品”。
When the item goes from lstWish to lstPurchased I want to add "from firstName lastName". 当项目从lstWish转到lstPurchased时,我想添加“ from firstName lastName”。 So the lstPurchased will read "Item available at Store from firstName lastName". 因此,lstPurchased将显示为“从firstName lastName在商店可用的商品”。 How do I add the second half of the string while moving it over? 如何在移动字符串时添加字符串的后半部分?
Here is the method that moves the item over: 这是将项目移到上方的方法:
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]);
}
}
And here is the code for the button click: 这是单击按钮的代码:
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;
}
}
}
The list is a collection is a List. 该列表是一个集合,是一个列表。 There are two custom classes, Gift and Guest. 有两个自定义类,Gift和Guest。 Here is the class Gift that has the toString that populates the listBox: 这是Gift类,它具有填充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
}
} }
Guest class: 来宾班:
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
}
在MoveListBoxItems()
,可以将firstName和lastName与以下内容连接:
lstPurchased.Items.Add(string.Format("{0} from {1} {2}", item.ToString(), firstName, lastName));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.