繁体   English   中英

将项目从一个列表框移动到另一个列表框时,如何添加到进入第二个列表框的字符串中?

[英]When moving an item from one listbox to another, how do I add to the string going into the second listbox?

好的,所以我的应用程序是一个简单的婴儿礼物注册表。 在左边的groupBox中,我有2个输入文本框(item,store)和一个listBox(lstWish)用于输出。 这边就是愿望清单。 在右边的groupBox中,我有2个输入文本框(firstName,lastName)和一个listBox(lstPurchased)用于输出。 单击button_click后,将从lstWish选中的项目移至lstPurchased。 lstWish当前输出类似“商店中可用的商品”。

当项目从lstWish转到lstPurchased时,我想添加“ from 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;
            }
        }
    }

该列表是一个集合,是一个列表。 有两个自定义类,Gift和Guest。 这是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        
}

}

来宾班:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM