Visual Studio C# 1. Correct all syntax errors within the given program. 2. Corre
ID: 3695953 • Letter: V
Question
Visual Studio C#
1. Correct all syntax errors within the given program.
2. Correct all logic errors within the given program – there should be 4 events. Check the working application for validation.
3. In Form1.cs, there is a foreach loop that is repeated. Create a method for this loop so that the foreach loop is only written once, and there are two calls to your new method.
4. Add a new type of event (such as a play or a festival – requires the creation of a new subclass of Ticket). Add this event to the form including a picture of the event.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TicketPurchasing
{
public abstract class Ticket
{
//Private data
private double cost;
private string nameOfEvent;
private string location;
private string fileName;
//Constructor
public Ticket()
{
}
//Constructor
public Ticket(double c, string n, string l, string f)
{
cost = c;
nameOfEvent = n;
location = l;
fileName = f;
}
//Setters
public void setCost(double c)
{
cost = c;
}
public void setName(string n)
{
nameOfEvent = n;
}
public void setLocation(string l)
{
location = l;
}
public void setFileName(string f)
{
fileName = f;
}
//Getters
public double getCost()
{
return cost;
}
public string getName()
{
return nameOfEvent;
}
public string getLocation()
{
return location;
}
public string getFileName()
{
return fileName;
}
public virtual string getDetails()
{
return "Name of event: " + nameOfEvent +
" Location: " + location +
" Ticket cost: " + cost.ToString("c");
}
}
}
DESIGN FORM:
namespace TicketPurchasing
{
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
///true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.cbEvents = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.txtTickets = new System.Windows.Forms.TextBox();
this.btnDetails = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.txtCost = new System.Windows.Forms.TextBox();
this.btnClear = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.pbImage = new System.Windows.Forms.PictureBox();
this.lblDetails = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pbImage)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(94, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Upcoming Events:";
//
// cbEvents
//
this.cbEvents.FormattingEnabled = true;
this.cbEvents.Location = new System.Drawing.Point(112, 18);
this.cbEvents.Name = "cbEvents";
this.cbEvents.Size = new System.Drawing.Size(207, 21);
this.cbEvents.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(22, 166);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 13);
this.label2.TabIndex = 2;
this.label2.Text = "Number of Tickets: ";
//
// txtTickets
//
this.txtTickets.Location = new System.Drawing.Point(128, 163);
this.txtTickets.Name = "txtTickets";
this.txtTickets.Size = new System.Drawing.Size(100, 20);
this.txtTickets.TabIndex = 3;
this.txtTickets.Text = "0";
this.txtTickets.TextChanged += new System.EventHandler(this.txtTickets_TextChanged);
//
// btnDetails
//
this.btnDetails.Location = new System.Drawing.Point(31, 53);
this.btnDetails.Name = "btnDetails";
this.btnDetails.Size = new System.Drawing.Size(75, 23);
this.btnDetails.TabIndex = 4;
this.btnDetails.Text = "Details";
this.btnDetails.UseVisualStyleBackColor = true;
this.btnDetails.Click += new System.EventHandler(this.btnDetails_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(22, 211);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(61, 13);
this.label3.TabIndex = 6;
this.label3.Text = "Total Cost: ";
//
// txtCost
//
this.txtCost.Location = new System.Drawing.Point(89, 208);
this.txtCost.Name = "txtCost";
this.txtCost.ReadOnly = true;
this.txtCost.Size = new System.Drawing.Size(100, 20);
this.txtCost.TabIndex = 7;
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(506, 227);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 23);
this.btnClear.TabIndex = 8;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(607, 227);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 9;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// pbImage
//
this.pbImage.Location = new System.Drawing.Point(451, 12);
this.pbImage.Name = "pbImage";
this.pbImage.Size = new System.Drawing.Size(173, 180);
this.pbImage.TabIndex = 10;
this.pbImage.TabStop = false;
//
// lblDetails
//
this.lblDetails.AutoSize = true;
this.lblDetails.Location = new System.Drawing.Point(125, 58);
this.lblDetails.Name = "lblDetails";
this.lblDetails.Size = new System.Drawing.Size(90, 13);
this.lblDetails.TabIndex = 11;
this.lblDetails.Text = "Event Information";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(689, 262);
this.Controls.Add(this.lblDetails);
this.Controls.Add(this.pbImage);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.txtCost);
this.Controls.Add(this.label3);
this.Controls.Add(this.btnDetails);
this.Controls.Add(this.txtTickets);
this.Controls.Add(this.label2);
this.Controls.Add(this.cbEvents);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Ticket Orders";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pbImage)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
Explanation / Answer
namespace TicketPurchasing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ }
syntax errors corrected code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TicketPurchasing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ }
public abstract class Ticket
{
//Private data
private double cost;
private string nameOfEvent;
private string location;
private string fileName;
//Constructor
public Ticket()
{
}
//Constructor
public Ticket(double c, string n, string l, string f)
{
cost = c;
nameOfEvent = n;
location = l;
fileName = f;
}
//Setters
public void setCost(double c)
{
cost = c;
}
public void setName(string n)
{
nameOfEvent = n;
}
public void setLocation(string l)
{
location = l;
}
public void setFileName(string f)
{
fileName = f;
}
//Getters
public double getCost()
{
return cost;
}
public string getName()
{
return nameOfEvent;
}
public string getLocation()
{
return location;
}
public string getFileName()
{
return fileName;
}
public virtual string getDetails()
{
return "Name of event: " + nameOfEvent +
" Location: " + location +
" Ticket cost: " + cost.ToString("c");
}
}
}
//DESIGN FORM:
namespace TicketPurchasing
{
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
///true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.cbEvents = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.txtTickets = new System.Windows.Forms.TextBox();
this.btnDetails = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.txtCost = new System.Windows.Forms.TextBox();
this.btnClear = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.pbImage = new System.Windows.Forms.PictureBox();
this.lblDetails = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pbImage)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(94, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Upcoming Events:";
//
// cbEvents
//
this.cbEvents.FormattingEnabled = true;
this.cbEvents.Location = new System.Drawing.Point(112, 18);
this.cbEvents.Name = "cbEvents";
this.cbEvents.Size = new System.Drawing.Size(207, 21);
this.cbEvents.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(22, 166);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 13);
this.label2.TabIndex = 2;
this.label2.Text = "Number of Tickets: ";
//
// txtTickets
//
this.txtTickets.Location = new System.Drawing.Point(128, 163);
this.txtTickets.Name = "txtTickets";
this.txtTickets.Size = new System.Drawing.Size(100, 20);
this.txtTickets.TabIndex = 3;
this.txtTickets.Text = "0";
this.txtTickets.TextChanged += new System.EventHandler(this.txtTickets_TextChanged);
//
// btnDetails
//
this.btnDetails.Location = new System.Drawing.Point(31, 53);
this.btnDetails.Name = "btnDetails";
this.btnDetails.Size = new System.Drawing.Size(75, 23);
this.btnDetails.TabIndex = 4;
this.btnDetails.Text = "Details";
this.btnDetails.UseVisualStyleBackColor = true;
this.btnDetails.Click += new System.EventHandler(this.btnDetails_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(22, 211);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(61, 13);
this.label3.TabIndex = 6;
this.label3.Text = "Total Cost: ";
//
// txtCost
//
this.txtCost.Location = new System.Drawing.Point(89, 208);
this.txtCost.Name = "txtCost";
this.txtCost.ReadOnly = true;
this.txtCost.Size = new System.Drawing.Size(100, 20);
this.txtCost.TabIndex = 7;
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(506, 227);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 23);
this.btnClear.TabIndex = 8;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(607, 227);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 9;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// pbImage
//
this.pbImage.Location = new System.Drawing.Point(451, 12);
this.pbImage.Name = "pbImage";
this.pbImage.Size = new System.Drawing.Size(173, 180);
this.pbImage.TabIndex = 10;
this.pbImage.TabStop = false;
//
// lblDetails
//
this.lblDetails.AutoSize = true;
this.lblDetails.Location = new System.Drawing.Point(125, 58);
this.lblDetails.Name = "lblDetails";
this.lblDetails.Size = new System.Drawing.Size(90, 13);
this.lblDetails.TabIndex = 11;
this.lblDetails.Text = "Event Information";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(689, 262);
this.Controls.Add(this.lblDetails);
this.Controls.Add(this.pbImage);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.txtCost);
this.Controls.Add(this.label3);
this.Controls.Add(this.btnDetails);
this.Controls.Add(this.txtTickets);
this.Controls.Add(this.label2);
this.Controls.Add(this.cbEvents);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Ticket Orders";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pbImage)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cbEvents;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtTickets;
private System.Windows.Forms.Button btnDetails;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtCost;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.PictureBox pbImage;
private System.Windows.Forms.Label lblDetails;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.