need visual basic code this exercise, you’ll add a second form to an Invoice Tot
ID: 3739918 • Letter: N
Question
need visual basic code
this exercise, you’ll add a second form to an Invoice Total application that lets the
user change the sales tax percent.
Open the project and change the name of the existing form
1. Open the InvoiceTotal project in the Extra ExercisesChapter10InvoiceTotal
directory.
2. Change the name of the existing form to frmInvoiceTotal.
Create the Sales Tax form
3. Add another form named frmSalesTax to the project.
4. Add a label, text box, and two buttons to the new form and set the properties of
the form and its controls so they appear as shown above. When the user presses
the Enter key, the Click event of the OK button should fire. When the user
presses the Esc key, the Click event of the Cancel button should fire.
5. Add code to get the sales tax, store it in the Tag property of the form, and set the
DialogResult property of the form to OK when the user clicks the OK button.
Modify the code for the Invoice Total form
6. Change the SalesTax constant that’s declared in the Invoice Total form so its
value can be changed.
7. Add a Change Percent button to the Invoice Total form as shown above. Then,
add code that displays the Sales Tax form and gets the result when the user clicks
this button. If the user clicks the OK button on the Sales Tax form, this event
handler should store the new sales tax percent in the sales tax variable and
change the Tax label on the form so it displays the correct tax. Test the
application to be sure this works correctly.
8. Add data validation to the Sales Tax form to check that the user enters a decimal
value between 0 and 10 (non-inclusive). To make that easier, you can copy the
IsPresent, IsDecimal, and IsWithinRange methods from the Invoice Total form.
Test the application to be sure the validation works correctly.
Explanation / Answer
Please find my implementation:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InvoiceTotal
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmInvoiceTotal());
}
}
}
frmInvoiceTotal.Designer.cs
namespace InvoiceTotal
{
partial class frmInvoiceTotal
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnExit = new System.Windows.Forms.Button();
this.btnCalculate = new System.Windows.Forms.Button();
this.txtProductTotal = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.txtDiscountAmount = new System.Windows.Forms.TextBox();
this.txtTotal = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.txtSubtotal = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.txtTax = new System.Windows.Forms.TextBox();
this.btnChange = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnExit
//
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Location = new System.Drawing.Point(130, 161);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 16;
this.btnExit.Text = "E&xit";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// btnCalculate
//
this.btnCalculate.Location = new System.Drawing.Point(36, 161);
this.btnCalculate.Name = "btnCalculate";
this.btnCalculate.Size = new System.Drawing.Size(75, 23);
this.btnCalculate.TabIndex = 15;
this.btnCalculate.Text = "&Calculate";
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
//
// txtProductTotal
//
this.txtProductTotal.Location = new System.Drawing.Point(121, 16);
this.txtProductTotal.Name = "txtProductTotal";
this.txtProductTotal.Size = new System.Drawing.Size(84, 20);
this.txtProductTotal.TabIndex = 14;
//
// label3
//
this.label3.Location = new System.Drawing.Point(17, 121);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(94, 20);
this.label3.TabIndex = 5;
this.label3.Text = "Total:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.Location = new System.Drawing.Point(17, 42);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(94, 20);
this.label2.TabIndex = 8;
this.label2.Text = "Discount amount:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label1
//
this.label1.Location = new System.Drawing.Point(17, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(94, 20);
this.label1.TabIndex = 7;
this.label1.Text = "Product total:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtDiscountAmount
//
this.txtDiscountAmount.Location = new System.Drawing.Point(121, 42);
this.txtDiscountAmount.Name = "txtDiscountAmount";
this.txtDiscountAmount.ReadOnly = true;
this.txtDiscountAmount.Size = new System.Drawing.Size(84, 20);
this.txtDiscountAmount.TabIndex = 18;
this.txtDiscountAmount.TabStop = false;
//
// txtTotal
//
this.txtTotal.Location = new System.Drawing.Point(121, 122);
this.txtTotal.Name = "txtTotal";
this.txtTotal.ReadOnly = true;
this.txtTotal.Size = new System.Drawing.Size(84, 20);
this.txtTotal.TabIndex = 19;
this.txtTotal.TabStop = false;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(17, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(49, 13);
this.label4.TabIndex = 20;
this.label4.Text = "Subtotal:";
//
// txtSubtotal
//
this.txtSubtotal.Location = new System.Drawing.Point(121, 69);
this.txtSubtotal.Name = "txtSubtotal";
this.txtSubtotal.ReadOnly = true;
this.txtSubtotal.Size = new System.Drawing.Size(84, 20);
this.txtSubtotal.TabIndex = 21;
this.txtSubtotal.TabStop = false;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(17, 99);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(66, 13);
this.label6.TabIndex = 22;
this.label6.Text = "Tax (7.75%):";
//
// txtTax
//
this.txtTax.Location = new System.Drawing.Point(121, 96);
this.txtTax.Name = "txtTax";
this.txtTax.ReadOnly = true;
this.txtTax.Size = new System.Drawing.Size(84, 20);
this.txtTax.TabIndex = 23;
this.txtTax.TabStop = false;
//
// btnChange
//
this.btnChange.Location = new System.Drawing.Point(230, 94);
this.btnChange.Name = "btnChange";
this.btnChange.Size = new System.Drawing.Size(75, 23);
this.btnChange.TabIndex = 24;
this.btnChange.Text = "Change Percent";
this.btnChange.UseVisualStyleBackColor = true;
this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
//
// frmInvoiceTotal
//
this.AcceptButton = this.btnCalculate;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(343, 202);
this.Controls.Add(this.btnChange);
this.Controls.Add(this.txtTax);
this.Controls.Add(this.label6);
this.Controls.Add(this.txtSubtotal);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtTotal);
this.Controls.Add(this.txtDiscountAmount);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.txtProductTotal);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "frmInvoiceTotal";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Invoice Total";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Button btnCalculate;
private System.Windows.Forms.TextBox txtProductTotal;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtDiscountAmount;
private System.Windows.Forms.TextBox txtTotal;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtSubtotal;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox txtTax;
private System.Windows.Forms.Button btnChange;
}
}
frmInvoiceTotal.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InvoiceTotal
{
public partial class frmInvoiceTotal : Form
{
public frmInvoiceTotal()
{
InitializeComponent();
}
decimal SalesTaxPct = 7.75m;
private void btnCalculate_Click(object sender, EventArgs e)
{
if (IsValidData())
{
decimal productTotal = Convert.ToDecimal(txtProductTotal.Text);
decimal discountPercent = .0m;
if (productTotal < 100)
discountPercent = .0m;
else if (productTotal >= 100 && productTotal < 250)
discountPercent = .1m;
else if (productTotal >= 250)
discountPercent = .25m;
decimal discountAmount = productTotal * discountPercent;
decimal subtotal = productTotal - discountAmount;
decimal tax = subtotal * SalesTaxPct / 100;
decimal total = subtotal + tax;
txtDiscountAmount.Text = discountAmount.ToString("c");
txtSubtotal.Text = subtotal.ToString("c");
txtTax.Text = tax.ToString("c");
txtTotal.Text = total.ToString("c");
txtProductTotal.Focus();
}
}
public bool IsValidData()
{
return
IsPresent(txtProductTotal, "Subtotal") &&
IsDecimal(txtProductTotal, "Subtotal") &&
IsWithinRange(txtProductTotal, "Subtotal", 0, 1000000);
}
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
public bool IsDecimal(TextBox textBox, string name)
{
decimal number = 0m;
if (Decimal.TryParse(textBox.Text, out number))
{
return true;
}
else
{
MessageBox.Show(name + " must be a decimal number.", "Entry Error");
textBox.Focus();
return false;
}
}
public bool IsWithinRange(TextBox textBox, string name,
decimal min, decimal max)
{
decimal number = Convert.ToDecimal(textBox.Text);
if (number <= min || number >= max)
{
MessageBox.Show(name + " must be between " + min +
" and " + max + ".", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnChange_Click(object sender, EventArgs e)
{
frmSalesTax f = new frmSalesTax();
//f.Show();
if (f.ShowDialog() == DialogResult.OK)
{
SalesTaxPct = Convert.ToDecimal(f.Tag);
f.Close();
}
}
}
}
frmSalesTax.Designer.cs
namespace InvoiceTotal
{
partial class frmSalesTax
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtInput = new System.Windows.Forms.TextBox();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 89);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(60, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Sales Tax: ";
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(79, 86);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(100, 20);
this.txtInput.TabIndex = 1;
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(33, 127);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 2;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(123, 127);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 3;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
//
// frmSalesTax
//
this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.label1);
this.Name = "frmSalesTax";
this.Text = "frmSalesTax";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
}
}
frmSalesTax.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InvoiceTotal
{
public partial class frmSalesTax : Form
{
public frmSalesTax()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
decimal salesTax = Convert.ToDecimal(txtInput.Text);
Tag = salesTax;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.