Dictionary set dictionary <K, V> and constructed applications ==> health check plan project, Dictionary plan
Effect
First, let's prepare the classes we need.
1. Check the project class
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Chapter 5 _ health check package management system _ {// project class public class HealthCheckItem {// project Description public string Description {get; set ;} // project Name public string Name {get; set;} // project Price public int Price {get; set;} // public HealthCheckItem () constructed without Parameters () {}// construct public HealthCheckItem (string name, string description, int price) {this. name = name; this. price = price; this. description = description ;}}}
2. meal packs
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Chapter 5 _ health check package management system _ {// set public class HealthCheckSet {// set of HealthCheckItem public List <HealthCheckItem> Item {get; set ;} // package Price public int Price {get; set ;}// package Name public string Name {get; set ;}// public HealthCheckSet () is constructed without Parameters () {}// construct public HealthCheckSet (string name, List <HealthCheckItem> item) {this. name = name; this. item = item;} // calculate the total price public void CalcPrice () {int totalPrice = 0; foreach (HealthCheckItem item in this. item) {totalPrice + = item. price;} this. price = totalPrice ;}}}
Then we can implement some function keys on the home page.
1. Initial work on the main interface
// Create a set of all check items // List <HealthCheckItem> Alllist = new List <HealthCheckItem> (); Dictionary <string, HealthCheckItem> Alllist = new Dictionary <string, healthCheckItem> (); // create a List of check items in the package <HealthCheckItem> list = new List <HealthCheckItem> (); // use a Dictionary to save the set Dictionary <string, healthCheckSet> dictionary = new Dictionary <string, HealthCheckSet> (); // initialization check item HealthCheckItem item, item2, item3, item4, item5, item6, item7; // define a default HealthCheckSet moren package; // The method for initializing the check item public void main () {item = new HealthCheckItem ("height", "used to check height", 10 ); item2 = new HealthCheckItem ("weight", "used to check weight", 5); item3 = new HealthCheckItem ("vision", "used to check vision", 15 ); item4 = new HealthCheckItem ("hearing", "used to check hearing", 20); item5 = new HealthCheckItem ("liver function", "used to check liver function", 100 ); item6 = new HealthCheckItem ("", "", 10); item7 = new HealthCheckItem ("ECG", "", 100); Alllist. add (item. name, item); Alllist. add (item2.Name, item2); Alllist. add (item3.Name, item3); Alllist. add (item4.Name, item4); Alllist. add (item5.Name, item5); Alllist. add (item6.Name, item6); Alllist. add (item7.Name, item7); // dictionary. add (item. name, item) ;}// generate default package data public void yuan () {list. add (item); list. add (item2); list. add (item3); moren = new HealthCheckSet ("admission exam", list); // calculate the price of moren. calcPrice (); this. dictionary. add ("", moren);} // The Loading Method of the package list drop-down box is public void combox () {cbm_sum.Items.Clear (); cbm_sum.Items.Add ("-- select --"); foreach (string item in dictionary. keys) {cbm_sum.Items.Add (item) ;}// the first item by default is selected cbm_sum.SelectedIndex = 0 ;}// check the project drop-down box's loading method public void combox2 () {cmb_xiang.Items.Clear (); cmb_xiang.Items.Add ("-- select --"); foreach (string item in Alllist. keys) {cmb_xiang.Items.Add (item) ;}// by default, the first item is cmb_xiang.SelectedIndex = 0;} private void FrmMain_Load (object sender, EventArgs e) {main (); // initialize the check project yuan (); // generate the default package data combox (); // load combox2 () from the package list drop-down box; // check the loading of the Project drop-down box}
// Method of filling in the DataGridView of the package public void UpdateSet (HealthCheckSet set) {if (set. item = null) {// assign a null value to the data source of the DataGridView. dataSource = new BindingList <HealthCheckItem> (); return;} else {dgv. dataSource = new BindingList <HealthCheckItem> (set. item );}}
2. Add package button
// Add package private void but_add_Click (object sender, EventArgs e) {if (txt_name.Text! = "") {// Determines whether the dictionary contains the package if (dictionary. keys. contains (txt_name.Text) {MessageBox. show ("this package already exists", "prompt", MessageBoxButtons. OKCancel, MessageBoxIcon. error); return;} else {// instantiate the health List <HealthCheckItem> hao = new List <HealthCheckItem> (); HealthCheckSet health = new HealthCheckSet (); health. item = hao; health. name = ""; health. price = 0; this. dictionary. add (txt_name.Text, health); combox (); cb M_sum.Text = txt_name.Text; txt_name.Text = "" ;}} else {MessageBox. Show ("added cannot be blank! "," Prompt ", MessageBoxButtons. OKCancel, MessageBoxIcon. Error );}}
3. Select (Change) Plan
// Select the package private void cbm_sum_SelectedIndexChanged (object sender, EventArgs e) {string setName = cbm_sum.Text; if (cbm_sum.Text = "-- select --") {// assign a null value to the data source of the DataGridView. dataSource = new BindingList <HealthCheckItem> (); items = ""; cmb_xiang.Text = ""; lab_xianshiprice.Text = ""; but_new.Enabled = false; return;} else {lab_xianshiname.Text = setName; if (dictionary [setName]! = Null) {// bind the data UpdateSet (dictionary [setName]) to the dview Based on the package name;} else {// assign a null value to the data source of the DataGridView. dataSource = new BindingList <HealthCheckItem> ();} // calculate the total price lab_xianshiprice.Text = dictionary [setName] based on the check items given to the package name. price. toString ();}}
4. Add check item button
// Add the project private void but_new_Click (object sender, EventArgs e) {string name = cmb_xiang.Text; // find the corresponding project set based on the package you select, at the same time, determine whether the aggregate contains the item if (! Dictionary [cbm_sum.Text]. item. contains (Alllist [name]) // No, add {dictionary [cbm_sum.Text]. item. add (Alllist [name]); MessageBox. show ("added successfully! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. information); dgv. dataSource = new BindingList <HealthCheckItem> (dictionary [cbm_sum.Text]. item); dictionary [cbm_sum.Text]. calcPrice (); // calculate the total price lab_xianshiprice.Text = dictionary [cbm_sum.Text] based on the check items given to the package name. price. toString ();} else // If yes, {MessageBox is displayed. show ("this project already exists", "prompt", MessageBoxButtons. OKCancel, MessageBoxIcon. error );}}
5. delete button
// Delete private void but_shan_Click (object sender, EventArgs e) {string key = dgv. selectedRows [0]. cells [1]. value. toString (); this. dictionary [cbm_sum.Text]. item. remove (Alllist [key]); dgv. dataSource = new BindingList <HealthCheckItem> (dictionary [cbm_sum.Text]. item); but_shan.Enabled = false; // disable the delete button}
Public string name; // select private void dgv_CellClick (object sender, DataGridViewCellEventArgs e) {if (dgv. SelectedRows. Count! = 1 | dgv. SelectedRows [0]. Cells [1]. Value = null) {MessageBox. Show ("Please select a row correctly! "," Prompt ", MessageBoxButtons. OKCancel, MessageBoxIcon. error); return;} else {name = dgv. selectedRows [0]. cells [1]. value. toString (); but_shan.Enabled = true; // available for the delete button }}
6. The Add button is available or disabled.
// Whether the Add button is available (check the SelectedIndexChanged event in the project drop-down box) private void cmb_xiang_SelectedIndexChanged (object sender, EventArgs e) {if (cmb_xiang.Text = "-- select --" | cbm_sum.Text = "-- select --") {but_new.Enabled = false; // disable} else {but_new.Enabled = true; // available }}
In this way, our project has been completed, and some of them can be optimized. Don't forget that nothing can be a classic forever!