[英]C# Calculate selected item from list of different popup forms
There are 3 related forms: 有3种相关形式:
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.