简体   繁体   English

C#从不同的弹出窗体列表中计算选定的项目

[英]C# Calculate selected item from list of different popup forms

There are 3 related forms: 有3种相关形式:

  1. Form1: Main(contains a listbox named lst_main) and label as "Total Price" Form1:Main(包含一个名为lst_main的列表框),标签为“总价”
  2. Form2 = CategoryForm (5 buttons are there, which opens a new subCategoriesform Form2 = CategoryForm(有5个按钮,将打开一个新的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) Form3 = SubCategoriesForm(s):正如我提到的那样,此表单有5个,每个表单都有一个listBox,其中包含一些项,以及价格,例如“ Brown T Shirt”,价格为50等)

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". 用户根据所选类别从“ subCategoriesform”的列表中进行选择,最后,他从不同子类别中选择的所有项目都应显示在主列表框中(位于主表单中的列表框中),并且所有相关价格应为SUM并显示为“总价”。

I was thinking to do it with Hashtable, what do you think? 我当时想用Hashtable做到这一点,您怎么看? 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Windows窗体Listview所选项目 - C# Windows forms Listview selected item 从C#中的数据绑定列表中获取选定的项目详细信息 - Get selected Item details from a data binded List in C# 如何将列表框中的选定项目添加到列表 <string> 在C#中 - how to add the selected item from a listbox to list <string> in c# 如何在C#Windows窗体中设置ComboBox的选定项? - How to set Selected item of ComboBox in C# Windows Forms? 如何从C#中的其他表单中删除带有查询的列表项? - How to delete a list item with an query from a different form in c#? 如何使用C#Webforms将选定的列表值从弹出窗口传递到文本框中? - How to pass selected list value from popup into a textbox using C# webforms? c#-如何从模型中的项目列表中获取特定项目,并在单击按钮时显示所选项目的属性? - c# - How to get a specific item from a list of items in model and display the properties of the selected item on button click? 在C#中选择组合框中的项目时,字体大小不同 - Different font size when item from combo box is selected in C# DataGridview从不同形式的Excel到C# - DataGridview to excel from different forms c# 从 C# 中的字典创建一个随机列表,包括一个特定的选定项 - Create a random List from Dictionary in C# including one specific selected Item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM