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. This side is the Wish List. In the right groupBox, I have 2 input text boxes(firstName, lastName) and a listBox(lstPurchased) for output. Upon a button_click, the selected item from lstWish is moved to lstPurchased. lstWish currently outputs like "Item available at Store".
When the item goes from lstWish to lstPurchased I want to add "from firstName lastName". So the lstPurchased will read "Item available at Store from 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. Here is the class Gift that has the toString that populates the listBox:
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.