This is for my button named Edit, when you have an entry into the shopping basket and click on the entry and click Edit it opens up a new window which allows you to edit the entries, product name, quantity or price. This is what I have and it compiles and runs fine but is there an easier way to write it?

private void btn_Edit_Click(object sender, EventArgs e)
{
    if (lst_Results.SelectedIndex >= 0)
    {
        // Want to edit the value of the Item
        Edit editbutton = new Edit();

        editbutton.NameOfItem =
        basket.Items[lst_Results.SelectedIndex].ItemName;
        editbutton.Quantity = basket.Items[lst_Results.SelectedIndex].Quantity;
        editbutton.ReplacementValue =
        basket.Items[lst_Results.SelectedIndex].Price;


        if (editbutton.ShowDialog() == DialogResult.OK)
        {
            basket.UpdateReplacementValue(basket.Items[lst_Results.SelectedIndex].ItemName, editbutton.Quantity, editbutton.ReplacementValue);
            RenderLibrary();
        }
    }
}
有帮助吗?

解决方案

At least, you can write out the repeating array access.

// Want to edit the value of the Item
Edit editbutton = new Edit();

var item = basket.Items[lst_Results.SelectedIndex];

editbutton.NameOfItem = item.ItemName;
editbutton.Quantity = item.Quantity;
editbutton.ReplacementValue = item.Price;

if (editbutton.ShowDialog() == DialogResult.OK)
{
    basket.UpdateReplacementValue(item.ItemName, editbutton.Quantity, editbutton.ReplacementValue);
    RenderLibrary();
}

Also, you might want to just pass the object as parameter to the control, and edit it there using data binding.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top