Practice in C#1Practice 9. Exercise1: Using menusIn a nice little village of North Hungary, thereis a pub with a funny name. Now we write aprogram for this pub.Our application has more forms, and on themain form, there is a menu bar.The File menu is typical: you can open a data file containing the list of drinks and their price,and save the booking of pub. These files are something like this:drink_menu.txt: booking.txtThe Help menu helps you, the About gives a little information about you:Before opening the data file, the Drink menu and Save menu items are inactive, after readingthe file, they will be active.The effect of Drink menu is: You can see the menu of drinks – only one page in a time –, andyou can order any drink. As it is a little pub, de maximum number of a drink is 999. 2Clicking Order button, you can order. Give error message if there is no any selection or thereis any error with pieces belonging to a selected drink. In this case, the background of wrongtextboxes becomes red. In case of correctly completed data, the program accepts the ordering,and be the drink deselected and the textbox empty.Click the right mouse button on Order button a context menu appears:The effect of Bill menu item: (The bill isin a RichTextBox.)The effect of Pay menu item is: the bill will be empty andthe paid items are booked. Now the result is only in thememory, but applying Save menu item, it will be write intoa file. (For each drink the name of drink, the total amount ofsold items per drink and income per drink.)If you want to see some pictures about village, chose the Gallery menu item (as many times asyou want):Practice in C#3A possible solution is:1. Design of surface (GUI)As you see, this application consists of moreforms.Form1 (if you want, you can rename it)It is recommended to set fix size of the form(FormBorderStyle and MaximizeBoxproperties). The simplest way for using background image to put the picture in bin/debug folder,and from this folder you can chose the appropriate BackgroundImage object. The best settingof BackgroundImageLayout property is Stretch.Put the menu bar in the form, and assign to the form an OpenFileDialog and aSaveFileDialog control as well.DrinkMenuFormIt is recommended to set fix size of the form as well. In this form, there is a label with a title, apanel (pnlMenu) and a button (btnOrder). This panel will contain the drink menu, that isthese control elements will be created according to data of data-file. As you don’t know thenumber of drinks, so the AutoScroll property of panel has to be true.At first glance, we are ready with this form, but there is one more element, aContextMenuStrip control belonging to Order button. The main difference between menustrip and context menu strip is, that the later one invisible by default. It will be visible whenyou click the right mouse button on control belonging to this context menu strip. A contextmenu strip can be put in form in the same way as menu strip, the only difference is that youhave to assign it to a context, namely to the relevant control – in our case to Order button.(ContextMenuStrip property of btnOrder item.)BillFormOnly one RichTextBox control – its name is: rchTxtBill. Let it be read only and don’tallow the tabulator parking on it. 4GalleryFormIts controls are: pictureBox1, btnLeft, btnRight. The Text property of buttons is empty,but they have background image.The main question about forms is not their appearance, but how can we see them and whathappen on these surfaces. Of course, they are classes, so we have to instantiate them and ensuretheir visibility.2. CodingLet us begin with Drink class: class Drink { public int OrderedQuantity { get; private set; } public int TotalQuantity { get; private set; } public int Income { get; private set; } public string DrinkName { get; private set; } public int UnitPrice { get; set; } public Drink(string drinkName, int unitPrice) { this.DrinkName = drinkName; this.UnitPrice = unitPrice; } // During ordering the number of ordered drinks increases public void Order(int piece) { OrderedQuantity += piece; } public int Payable() { return OrderedQuantity * UnitPrice; } // After paying the ordered quantity is zero again. public void Pay() { TotalQuantity += OrderedQuantity; Income += Payable(); OrderedQuantity = 0; } public string ToPriceList() { return DrinkName + ( + UnitPrice + Ft); } public string ToBooking() { return DrinkName + ; + TotalQuantity + ; + Income; } public override string ToString() { return OrderedQuantity.ToString().PadLeft(4) + + Practice in C#5 DrinkName.PadRight(33) +Payable().ToString().PadLeft(10) + Ft; } }Hopefully you can understand this code. The only news are PadLeft(), PadRight()methods. They serve to fill with spaces the given length if the string is shorter. However thesemethods work only with Consolas and Courier fonts.Form1 class:This class works due to menu items. Namely you can open the data file (after choosing it), showthe drink-menu, the gallery, save the booking file, give help and give information about theauthor of the program.The questions are:1. How can we show the drink-menu, because it is in other form, and how pass the data to thisform?2. How can we show gallery, because it is in other form as well, and where (in which form) dowe load pictures?Showing a form is simple. We create an instance of the form, and call its Show() or ShowDialog()method. What is the difference between the two methods? The Show() method shows the form in anon-modal form. This means that you can click on the parent form, moreover you can show theform in multiple instances. ShowDialog() shows the form modally, meaning you cannot goto the parent form.As the Open menu item is in Form1 form, but the drink-menu is in DrinkMenuForm, so thelist of drinks have to be passed to the instance of DrinkMenuForm, namely we ask this formto write the drink-menu.The last question is the place of image loading. The simplest solution seems to load them inGalleryForm. However, we want to give the possibility to show the form in multipleinstances. In this case, we would have to load the pictures many times. Therefore the bestsolution is to load them in Form1 form and pass them to GalleryForm instances.The code of class is: public partial class Form1 : Form { private List drinks = new List(); private List pictures = new List();6 private int numberOfPictures = 10; public Form1() { InitializeComponent(); this.CenterToScreen(); openFileDialog1.InitialDirectory = Environment.CurrentDirectory; openFileDialog1.FileName = drink_menu.txt; saveFileDialog1.InitialDirectory = Environment.CurrentDirectory; saveFileDialog1.FileName = booking.txt; } // The pictures have to be loaded only once, so this is the task // of form loading. private void Form1_Load(object sender, EventArgs e) { try { PicturesLoading(); } catch (Exception ex) { MessageBox.Show(Error creating picture + ex, Error); } } private void PicturesLoading() { Image picture; for (int i = 1; i picture = Image.FromFile(picture + i + .jpg); pictures.Add(picture); } } private void openMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { StreamReader streamReader = null; try { string fileName = openFileDialog1.FileName;streamReader = new StreamReader(fileName); Input(streamReader); drinkMenuMenuItem.Enabled = true;saveMenuItem.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message, Error message for programmer); } finally { if (streamReader != null) { streamReader.Close(); } } } } private void Input(StreamReader streamReader) { string line; 代写C#留学生作业、代做RichTextBox作业、代写C#编程设计作业、代做C#实验作业 代写留学生 Statististring[] data; while(!streamReader.EndOfStream){ line = streamReader.ReadLine(); data = line.Split(;); // Shot of plum brandy;200 Practice in C#7 drinks.Add(new Drink(data[0],int.Parse(data[1]))); } } // We want one drink menu a time, so the form is shown with // ShowDialog() method. Of course we pass the list of drinks before. private void DrinkMenuMenuItem_Click(object sender, EventArgs e) { DrinkMenuForm drinkMenuForm = new DrinkMenuForm(); drinkMenuForm.WriteDrinkMenu(drinks); drinkMenuForm.ShowDialog(); } // We allow to show more gallery a time, so the form is shown with // Show() method after passing pictures. private void GalleryMenuItem_Click(object sender, EventArgs e) { GalleryForm galleryForm = new GalleryForm(); galleryForm.Pass(pictures); galleryForm.Show(); } private void SaveMenuItem_Click(object sender, EventArgs e) { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { StreamWriter streamWriter = null; try { string fajlNev = saveFileDialog1.FileName;streamWriter = new StreamWriter(fajlNev); WriteToFile(streamWriter); } catch (Exception) { MessageBox.Show(Error in file-writing, Error); } finally { if (streamWriter != null) { streamWriter.Close(); } } } } private void WriteToFile(StreamWriter streamWriter) { foreach (Drink item in drinks) { streamWriter.WriteLine(item.ToBooking()); } } private void HelpMenuItem_Click(object sender, EventArgs e) { new HelpForm().ShowDialog(); } private void AboutMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(Made on programming course, Information); } private void exitMenuItem_Click(object sender, EventArgs e) { this.Close(); } }8DrinkMenuForm class:Maybe this is the most complicated part of our program, however it is not your first dynamiccontrol application, so it will not be too difficult.The tasks of this form are: To write the drink-menu according to the list of drinks. This means: dynamically put thecontrols (checkbox, textbox, label) into the panel. To order drinks. Doing it, you have to pay attention to check the selection, the correctnumber-format of pieces, and of course to make orders. If in any textbox there is wrongnumber, so its color becomes red. You can correct this error in two ways. One of them is towrite a correct number, the other one is to deselect this item. In the latter case you have tochange the color of the textbox and make it empty. This action has to listen the checkboxchangeevent. According to Bill menu item, you have to show the bill – it is in other form, so you have toask this form to write the bill. According to Pay menu item, you “pay” the items.The code of the class is: public partial class DrinkMenuForm : Form { public DrinkMenuForm() { InitializeComponent(); } // The list of drinks will be used in other methods as well, // so we have to declare it on class level. private List drinks = new List(); // They are used for ordering. If you are smart programmer, you can solve this // program without these two lists, but using them makes the solution simpler. private List chkBoxes = new List(); private List txtBoxes = new List(); // The distances for placement dynamic controlls private int left = 10, top = 10, chkXSize = 250, chkYDistance = 40, txtXSize = 30, txtYSize = 17, txtBoxLblDistance = 5; private int maxQuantity = 999; internal void WriteDrinkMenu(List drinks) { pnlMenu.Controls.Clear(); chkBoxes.Clear(); txtBoxes.Clear();Practice in C#9 this.drinks = drinks; CheckBox chkBox; TextBox txtBox; Label lbl; // Placement of dynamic controls. for (int i = 0; i // chkBox chkBox = new CheckBox(); chkBox.Location = new Point(left, top + i * chkYDistance); chkBox.Size = new Size(chkXSize, txtYSize); chkBox.Text = drinks[i].ToPriceList(); chkBox.TextAlign = ContentAlignment.MiddleLeft; chkBox.CheckedChanged += new EventHandler(this.chkBox_CheckedChanged); // txtBox txtBox = new TextBox(); // Both locations are good. // If you look at Designer.cs file with trying controls, you can see // the explanation for -2 value. //txtBox.Location = new Point(left + chkXSize, top + i * chkYDistance-2); txtBox.Location = new Point(left + chkXSize, chkBox.Location.Y -2); txtBox.Size = new Size(txtXSize, txtYSize); // lbl lbl = new Label(); lbl.AutoSize = true; lbl.Location = new Point(txtBox.Location.X + txtXSize + txtBoxLblDistance, chkBox.Location.Y); lbl.Text = pcs; // put controls on the panel pnlMenu.Controls.Add(chkBox); pnlMenu.Controls.Add(txtBox); pnlMenu.Controls.Add(lbl); // add controls to the appropriate lists chkBoxes.Add(chkBox); txtBoxes.Add(txtBox); } } private void chkBox_CheckedChanged(object sender, EventArgs e) { CheckBox chkBox = (CheckBox)sender; if (!chkBox.Checked) { int i = chkBoxes.IndexOf(chkBox); txtBoxes[i].BackColor = Color.White; txtBoxes[i].Text = ; } } // ordering private void btnOrder_Click(object sender, EventArgs e) { bool isSelected = false, isWrongPcs = false; int pcs = 0;10 for (int i = 0; i if (chkBoxes[i].Checked) { isSelected = true;try { pcs = int.Parse(txtBoxes[i].Text); if (pcs maxQuantity) throw new Exception(); drinks[i].Order(pcs);txtBoxes[i].BackColor = Color.White;chkBoxes[i].Checked = false;txtBoxes[i].Clear(); }catch (Exception) { txtBoxes[i].BackColor = Color.Salmon;isWrongPcs = true; } } } if (!isSelected) { MessageBox.Show(There is no selected item, Warning); } else if (isWrongPcs) { MessageBox.Show(The pieces colored by red are incorrect, Warning); } } private void billMenuItem_Click(object sender, EventArgs e) { BillForm billForm = new BillForm(); billForm.Write(drinks); billForm.ShowDialog(); } private void payMenuItem_Click(object sender, EventArgs e) { foreach (Drink item in drinks) { item.Pay(); } } }BillForm class public partial class BillForm : Form { public BillForm() { InitializeComponent(); } internal void Write(List drinks) { rchTxtBill.Clear(); foreach (Drink item in drinks) { if (item.OrderedQuantity != 0) { rchTxtBill.Text += item.ToString() + \r\n; } } } }Practice in C#11GalleryForm classIt is simple but spectacular: public partial class GalleryForm : Form { public GalleryForm() { InitializeComponent(); this.CenterToParent(); } private List pictures = new List(); private int currentIndex; internal void Pass(List pictures) { this.pictures = pictures; pictureBox1.Image = pictures[currentIndex]; } private void btnRight_Click(object sender, EventArgs e) { if (currentIndex else currentIndex = 0; pictureBox1.Image = pictures[currentIndex]; } private void btnLeft_Click(object sender, EventArgs e) { if (currentIndex > 0) currentIndex--; else currentIndex = pictures.Count - 1; pictureBox1.Image = pictures[currentIndex]; } }HelpForm class: public partial class HelpForm : Form { public HelpForm() { InitializeComponent(); this.CenterToParent(); } private void HelpForm_Load(object sender, EventArgs e) { rchTxtHelp.Text = File menu item:\r\nYou can open the data file containing the drink list + \r\nand you can save the file containing the booking of pub + (name of drinks, total amount per drinks and income per drinks) + \r\n\nDrink menu menu item:\r\nYou can see the drink menu and order any drink + \r\n\nGallery menu item:\r\nYou can see pictures of the village + and the surrounding area. - + You can open it many times and multiple instances. ; } }转自:http://ass.3daixie.com/2018122213015261.html
网友评论