// Precondition: Insert, Letter menu item activated // Postcondition: The Letter dialog box is displayed. If data entered // are OK, a Letter is created and added to the list // of parcels private void letterToolStripMenuItem_Click(object sender, EventArgs e) { LetterForm letterForm; // The letter dialog box form DialogResult result; // The result of showing form as dialog decimal fixedCost; // The letter's cost if (upv.AddressCount < LetterForm.MIN_ADDRESSES) // Make sure we have enough addresses { MessageBox.Show("Need " + LetterForm.MIN_ADDRESSES + " addresses to create letter!", "Addresses Error"); return; // Exit now since can't create valid letter } letterForm = new LetterForm(upv.AddressList); // Send list of addresses result = letterForm.ShowDialog(); if (result == DialogResult.OK) // Only add if OK { if (decimal.TryParse(letterForm.FixedCostText, out fixedCost)) { // For this to work, LetterForm's combo boxes need to be in same // order as upv's AddressList upv.AddLetter(upv.AddressAt(letterForm.OriginAddressIndex), upv.AddressAt(letterForm.DestinationAddressIndex), fixedCost); // Letter to be inserted } else // This should never happen if form validation works! { MessageBox.Show("Problem with Letter Validation!", "Validation Error"); } } letterForm.Dispose(); // Best practice for dialog boxes // Alternatively, use with using clause as in Ch. 17 }
// Precondition: Insert, Letter menu item activated // Postcondition: The Letter dialog box is displayed. If data entered // are OK, a Letter is created and added to the list // of parcels private void letterToolStripMenuItem_Click(object sender, EventArgs e) { LetterForm letterForm; // The letter dialog box form DialogResult result; // The result of showing form as dialog if (upv.AddressCount < LetterForm.MIN_ADDRESSES) // Make sure we have enough addresses { MessageBox.Show("Need " + LetterForm.MIN_ADDRESSES + " addresses to create letter!", "Addresses Error"); return; } letterForm = new LetterForm(upv.AddressList); // Send list of addresses result = letterForm.ShowDialog(); if (result == DialogResult.OK) // Only add if OK { try { // For this to work, LetterForm's combo boxes need to be in same // order as upv's AddressList upv.AddLetter(upv.AddressAt(letterForm.OriginAddressIndex), upv.AddressAt(letterForm.DestinationAddressIndex), decimal.Parse(letterForm.FixedCostText)); // Letter to be inserted } catch (FormatException) // This should never happen if form validation works! { MessageBox.Show("Problem with Letter Validation!", "Validation Error"); } } letterForm.Dispose(); // Best practice for dialog boxes }
//Precondition: Origin address, destination address, and fixed cost must have values //Postconditon: Shows form and creates a letter private void letterToolStripMenuItem_Click(object sender, EventArgs e) { LetterForm lfrm = new LetterForm(upv.AddressList); //New LetterForm object DialogResult result = lfrm.ShowDialog(); if (result == DialogResult.OK) { upv.AddLetter(upv.AddressAt(lfrm.OriginValue), upv.AddressAt(lfrm.DestValue), decimal.Parse(lfrm.FixedCost)); } lfrm.Dispose(); }
//Precondition: click, upv.address must be instantiated with more than 2 addresses //Postcondition: Show Letter Form, add letter to upv parcel list private void letterToolStripMenuItem_Click(object sender, EventArgs e) { LetterForm LF = new LetterForm(upv.addresses); //create LetterForm object, pass upv.AddressList into the constructor DialogResult result; //holds dialog result OK/Cancel result = LF.ShowDialog(); //shows form as a modal dialog box if (result == DialogResult.OK) //result == OK? add the selected indices and parsed fixed cost to the parcel list { //Precondition: origin address and destination address Indices cannot be Equal, fixed cost must be a decimal value //Postcondition: Add Letter to UserParcelView parcel list upv.AddLetter(LF.AddressList[LF.OriginIndex], LF.AddressList[LF.DestinationIndex], decimal.Parse(LF.FixedCost)); } else if (result == DialogResult.Cancel) //Dispose of resources if result == cancel { LF.Dispose(); } }