好的,所以我的申请是一个简单的婴儿礼品登记处。在左侧groupBox中,我有2个输入文本框(item,store)和一个用于输出的listBox(lstWish)。这边是愿望清单。在右侧groupBox中,我有2个输入文本框(firstName,lastName)和一个listBox(lstPurchased)用于输出。按下button_click后,lstWish中的所选项目将移至lstPurchased。 lstWish目前输出类似于"项目可在商店"。
当项目从lstWish转到lstPurchased时,我想从firstName lastName"添加"。因此,lstPurchased将从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;
}
}
}
列表是一个列表集合。有两个自定义类,礼物和来宾。这是具有填充listBox的toString的类Gift:
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
}
答案 0 :(得分:0)
在MoveListBoxItems()
中,您可以将firstName和lastName连接到:
lstPurchased.Items.Add(string.Format("{0} from {1} {2}", item.ToString(), firstName, lastName));