简体   繁体   中英

C# Calculate selected item from list of different popup forms

There are 3 related forms:

  1. Form1: Main(contains a listbox named lst_main) and label as "Total Price"
  2. Form2 = CategoryForm (5 buttons are there, which opens a new subCategoriesform
  3. Form3 = SubCategoriesForm(s): as I mentioned there are 5 of this form, each form has a listBox with some items, and prices for example "Brown T Shirt" and it costs 50 and etc)

User Selects from the lists from "subCategoriesform" based on the selected category, and at the end all of his selected items from different sub categories should be shown in the main listbox (located listbox at the mainform) and also all related prices should be SUM and show as "Total Price".

I was thinking to do it with Hashtable, what do you think? is there any clean solution for this?

The first thing that comes to mind is make some public list in each form and get them in main form

//some where in Main Form
Form2 _frm=new Form2();
listofall.Concat(_frm.listofform2); //listofall is list of Main Form
//Make same to all other forms

Give your forms business properties that the previous form can use to get the selection. In example below, it just allows a single item selection. But you can adapt it to allow user to select a list of items before closing the form.

public class BaseSelectionForm : Form
{
    public string Selection { get; protected set; }
}

public class MainForm : Form
{
    public List<string> Selections { get; set; }

    private void ButtonClick(object sender, EventArgs e)
    {
        using (var dialog = new CategoryForm())
        {
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Selections.Add(dialog.Item);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
        }
    }
}

public class CategoryForm : BaseSelectionForm 
{
    private void ButtonClick(object sender, EventArgs e)
    {
        using (var dialog = new SubCategoryForm())
        {
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Selection = "This Category Name > " + dialog.Item;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
        }
    }
}

public class SubCategoryForm : BaseSelectionForm 
{
    private void ButtonClick(object sender, EventArgs e)
    {
        Selection = "Brown Shirt / $34.00";
        this.DialogResult = System.Windows.Forms.DialogResult.OK;
    }
}

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.

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