C# Program They say persistence pays off. In this project we\'ll be dealing with
ID: 3739219 • Letter: C
Question
C# Program
They say persistence pays off. In this project we'll be dealing with persistence. In this project you're going to be storing a list of obiects in either a binary or XML document. You're going to create a one form application that will allow you to enter the date of a sporting event, along with names and scores for two teams, Visitor, and Home. The user will click add to save the score or clear to clear their fields. Beneath the fields will be a data grid with all of the dates, teams and scores. Sports Scores Sports Scores Date: 03/15/2018 Visitor Team Home Team Visitor Socre Home Score Date 2/13/2016 2/13/2016 4/2/2016 Visitor Team Bulls Red Winge Astros Score Home Team Pacers Bruins Meta Score 102 110Explanation / Answer
Hi There,
I am unable to upload the project file. So I will add content of All the files. Please follow below steps to build sccessfully.
Step1: Open Microsoft Visual Studio 2010.
Step 2: Create New Project, Sleect Language as C#, Windows Forms Project. default Form1 will be created. Close all open files. Expand Solution Explorer.
Step 3: Open Form1.cs in code view. Copy paste below code
/**********************Form1.cs******************************/
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.Xml;
namespace TeamScore
{
public partial class frmSprtScore : Form
{
private String vTeam;
private String vScore;
private String hTeam;
private String hScore;
private String date;
private const String xmlFileName = "teamscores.xml";
public frmSprtScore()
{
InitializeComponent();
}
public void SetInit()
{
// check if XML file exist
// load into DataGridview
}
bool ValidateTeamsAndScores()
{
bool result = false;
bool bVT, bVS, bHT, bHS;
bVT = bVS = bHT = bHS = false;
if (IsEmpty(tbxVisitorTeam.Text))
{
lblVTError.Text = "Team name required";
//lblVTError.ForeColor = Color.Red;
bVT = false;
}
else
{
lblVTError.Text = "";
bVT = true;
}
if (IsEmpty(tbxVisitorScore.Text))
{
lblVSError.Text = "Team score required";
bVS = false;
}
else
{
lblVSError.Text = "";
bVS = true;
}
if(IsEmpty(tbxHomeTeam.Text))
{
lblHTError.Text = "Team name required";
bHT = false;
}
else
{
lblHTError.Text = "";
bHT = true;
}
if(IsEmpty(tbxHomeScore.Text))
{
lblHSError.Text = "Team score required";
bHS = false;
}
else
{
lblHSError.Text = "";
bHS = true;
}
if (bVT && bVS && bHT && bHS)
result = true;
return result;
}
bool IsEmpty(String value)
{
bool result = false;
if (value.Length <= 0) // when string is empty then return true
result = true;
return result;
}
private void AddNode(XmlDocument doc, XmlElement team, String nodeName, String nodeValue)
{
XmlElement element = doc.CreateElement(string.Empty, nodeName, string.Empty);
XmlText text = doc.CreateTextNode(nodeValue);
element.AppendChild(text);
team.AppendChild(element);
}
private XmlElement AddTeamInXML(XmlDocument doc)
{
XmlElement team = doc.CreateElement(string.Empty, "Team", string.Empty);
AddNode(doc, team, "Date", date);
AddNode(doc, team,"VisitorTeam", vTeam);
AddNode(doc, team, "VisitorScore", vScore);
AddNode(doc, team, "HomeTeam", hTeam);
AddNode(doc, team, "HomeScore", hScore);
return team;
}
private XmlDocument OpenExistingXMLDocument(String xmlFileName)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFileName);
return doc;
}
private XmlDocument CreateNewXMLDocument()
{
XmlDocument doc = new XmlDocument();
//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlNode root = doc.CreateNode(XmlNodeType.Document, "root", string.Empty);
doc.InsertBefore(xmlDeclaration, root);
//XmlElement entry = doc.CreateElement(string.Empty, "entry", string.Empty);
XmlElement team = AddTeamInXML(doc);
root.AppendChild(team);
doc.AppendChild(root);
doc.Save(xmlFileName);
return doc;
}
private void LoadDataGrid()
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(@xmlFileName);
dataGridView1.DataSource = dataSet.Tables[0];
}
private void btnAdd_Click(object sender, EventArgs e)
{
// validate values in all fields
if (ValidateTeamsAndScores())
{
// get all values
vTeam = tbxVisitorTeam.Text;
vScore = tbxVisitorScore.Text;
hTeam = tbxHomeTeam.Text;
hScore = tbxHomeScore.Text;
DateTime dateTime = dateTimePicker1.Value;
date = dateTime.ToShortDateString();
// load XML
XmlDocument xmlDoc = OpenExistingXMLDocument(xmlFileName);
if (xmlDoc == null)
{
xmlDoc = CreateNewXMLDocument();
}
else
{
XmlElement team = AddTeamInXML(xmlDoc);
XmlElement root = xmlDoc.DocumentElement;
root.AppendChild(team);
xmlDoc.Save(xmlFileName);
}
LoadDataGrid();
}
}
private void frmSprtScore_Load(object sender, EventArgs e)
{
XmlDocument xmlDoc = OpenExistingXMLDocument(xmlFileName);
if (xmlDoc != null)
{
LoadDataGrid();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTime.Now;
tbxVisitorTeam.Text = "";
tbxVisitorScore.Text = "";
tbxHomeTeam.Text = "";
tbxHomeScore.Text = "";
}
}
}
/******************** Form1.cs ends*******************/
Step4: Open Form1.Designer.cs in Code view. Add below code
/******************* Form1.Designer.cs *****************************/
namespace TeamScore
{
partial class frmSprtScore
{
/// <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.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.header = new System.Windows.Forms.Label();
this.lblVisitorTeam = new System.Windows.Forms.Label();
this.lblHomeTeam = new System.Windows.Forms.Label();
this.tbxVisitorTeam = new System.Windows.Forms.TextBox();
this.tbxHomeTeam = new System.Windows.Forms.TextBox();
this.tbxHomeScore = new System.Windows.Forms.TextBox();
this.tbxVisitorScore = new System.Windows.Forms.TextBox();
this.lblHomeScore = new System.Windows.Forms.Label();
this.lblVisitorScore = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.lblVTError = new System.Windows.Forms.Label();
this.lblHTError = new System.Windows.Forms.Label();
this.lblVSError = new System.Windows.Forms.Label();
this.lblHSError = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dateTimePicker1
//
this.dateTimePicker1.CustomFormat = "";
this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Short;
this.dateTimePicker1.Location = new System.Drawing.Point(38, 120);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(91, 20);
this.dateTimePicker1.TabIndex = 0;
this.dateTimePicker1.Value = new System.DateTime(2018, 3, 28, 11, 18, 58, 0);
//
// header
//
this.header.AutoSize = true;
this.header.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.header.Location = new System.Drawing.Point(252, 46);
this.header.Name = "header";
this.header.Size = new System.Drawing.Size(140, 24);
this.header.TabIndex = 1;
this.header.Text = "Sports Scores";
//
// lblVisitorTeam
//
this.lblVisitorTeam.AutoSize = true;
this.lblVisitorTeam.Location = new System.Drawing.Point(192, 120);
this.lblVisitorTeam.Name = "lblVisitorTeam";
this.lblVisitorTeam.Size = new System.Drawing.Size(65, 13);
this.lblVisitorTeam.TabIndex = 2;
this.lblVisitorTeam.Text = "Visitor Team";
//
// lblHomeTeam
//
this.lblHomeTeam.AutoSize = true;
this.lblHomeTeam.Location = new System.Drawing.Point(454, 119);
this.lblHomeTeam.Name = "lblHomeTeam";
this.lblHomeTeam.Size = new System.Drawing.Size(65, 13);
this.lblHomeTeam.TabIndex = 3;
this.lblHomeTeam.Text = "Home Team";
//
// tbxVisitorTeam
//
this.tbxVisitorTeam.Location = new System.Drawing.Point(284, 119);
this.tbxVisitorTeam.Name = "tbxVisitorTeam";
this.tbxVisitorTeam.Size = new System.Drawing.Size(100, 20);
this.tbxVisitorTeam.TabIndex = 4;
//
// tbxHomeTeam
//
this.tbxHomeTeam.Location = new System.Drawing.Point(530, 118);
this.tbxHomeTeam.Name = "tbxHomeTeam";
this.tbxHomeTeam.Size = new System.Drawing.Size(100, 20);
this.tbxHomeTeam.TabIndex = 5;
//
// tbxHomeScore
//
this.tbxHomeScore.Location = new System.Drawing.Point(530, 180);
this.tbxHomeScore.Name = "tbxHomeScore";
this.tbxHomeScore.Size = new System.Drawing.Size(100, 20);
this.tbxHomeScore.TabIndex = 9;
//
// tbxVisitorScore
//
this.tbxVisitorScore.Location = new System.Drawing.Point(284, 181);
this.tbxVisitorScore.Name = "tbxVisitorScore";
this.tbxVisitorScore.Size = new System.Drawing.Size(100, 20);
this.tbxVisitorScore.TabIndex = 8;
//
// lblHomeScore
//
this.lblHomeScore.AutoSize = true;
this.lblHomeScore.Location = new System.Drawing.Point(454, 184);
this.lblHomeScore.Name = "lblHomeScore";
this.lblHomeScore.Size = new System.Drawing.Size(66, 13);
this.lblHomeScore.TabIndex = 7;
this.lblHomeScore.Text = "Home Score";
//
// lblVisitorScore
//
this.lblVisitorScore.AutoSize = true;
this.lblVisitorScore.Location = new System.Drawing.Point(192, 182);
this.lblVisitorScore.Name = "lblVisitorScore";
this.lblVisitorScore.Size = new System.Drawing.Size(66, 13);
this.lblVisitorScore.TabIndex = 6;
this.lblVisitorScore.Text = "Visitor Score";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(308, 251);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 10;
this.btnAdd.Text = "Add";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(457, 251);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 23);
this.btnClear.TabIndex = 11;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(38, 356);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(615, 248);
this.dataGridView1.TabIndex = 12;
//
// lblVTError
//
this.lblVTError.AutoSize = true;
this.lblVTError.ForeColor = System.Drawing.Color.Red;
this.lblVTError.Location = new System.Drawing.Point(290, 151);
this.lblVTError.Name = "lblVTError";
this.lblVTError.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.lblVTError.Size = new System.Drawing.Size(0, 13);
this.lblVTError.TabIndex = 13;
//
// lblHTError
//
this.lblHTError.AutoSize = true;
this.lblHTError.ForeColor = System.Drawing.Color.Red;
this.lblHTError.Location = new System.Drawing.Point(536, 151);
this.lblHTError.Name = "lblHTError";
this.lblHTError.Size = new System.Drawing.Size(0, 13);
this.lblHTError.TabIndex = 14;
//
// lblVSError
//
this.lblVSError.AutoSize = true;
this.lblVSError.ForeColor = System.Drawing.Color.Red;
this.lblVSError.Location = new System.Drawing.Point(290, 214);
this.lblVSError.Name = "lblVSError";
this.lblVSError.Size = new System.Drawing.Size(0, 13);
this.lblVSError.TabIndex = 15;
//
// lblHSError
//
this.lblHSError.AutoSize = true;
this.lblHSError.ForeColor = System.Drawing.Color.Red;
this.lblHSError.Location = new System.Drawing.Point(536, 214);
this.lblHSError.Name = "lblHSError";
this.lblHSError.Size = new System.Drawing.Size(0, 13);
this.lblHSError.TabIndex = 16;
//
// frmSprtScore
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(691, 647);
this.Controls.Add(this.lblHSError);
this.Controls.Add(this.lblVSError);
this.Controls.Add(this.lblHTError);
this.Controls.Add(this.lblVTError);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.tbxHomeScore);
this.Controls.Add(this.tbxVisitorScore);
this.Controls.Add(this.lblHomeScore);
this.Controls.Add(this.lblVisitorScore);
this.Controls.Add(this.tbxHomeTeam);
this.Controls.Add(this.tbxVisitorTeam);
this.Controls.Add(this.lblHomeTeam);
this.Controls.Add(this.lblVisitorTeam);
this.Controls.Add(this.header);
this.Controls.Add(this.dateTimePicker1);
this.Name = "frmSprtScore";
this.Text = "Sports Scores";
this.Load += new System.EventHandler(this.frmSprtScore_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.Label header;
private System.Windows.Forms.Label lblVisitorTeam;
private System.Windows.Forms.Label lblHomeTeam;
private System.Windows.Forms.TextBox tbxVisitorTeam;
private System.Windows.Forms.TextBox tbxHomeTeam;
private System.Windows.Forms.TextBox tbxHomeScore;
private System.Windows.Forms.TextBox tbxVisitorScore;
private System.Windows.Forms.Label lblHomeScore;
private System.Windows.Forms.Label lblVisitorScore;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Label lblVTError;
private System.Windows.Forms.Label lblHTError;
private System.Windows.Forms.Label lblVSError;
private System.Windows.Forms.Label lblHSError;
}
}
/******************* Form1.Designer.cs Ends*****************************/
Step 5: Save all files and build project. Now your for should appear.
Please give thumbs up if it solves your Question.
Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.