Teams.txt This file contains a list of several Major League baseball teams in al
ID: 3599044 • Letter: T
Question
Teams.txt This file contains a list of several Major League baseball teams in al- phabetical order. Each team listed in the file has won the World Series at least once.
WorldSeriesWinners.txt—This file contains a chronological list of the World Series’ winning teams from 1903 through 2012. (The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2012. Note that the World Series was not played in 1904 or 1994.)
Create a Visual C# application that displays the contents of the Teams.txt file in a ListBox control. When the user selects a team in the ListBox, the application should display the number of times that team has won the World Series in the time period from 1903 through 2012.
Explanation / Answer
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WorldSeriesWinners
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new worldSeriesWinnersForm());
}
}
}
Form1.Designer.cs
namespace WorldSeriesWinners
{
partial class worldSeriesWinnersForm
{
/// <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.teamsListBox = new System.Windows.Forms.ListBox();
this.outputValueLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// teamsListBox
//
this.teamsListBox.FormattingEnabled = true;
this.teamsListBox.Location = new System.Drawing.Point(8, 8);
this.teamsListBox.Margin = new System.Windows.Forms.Padding(2);
this.teamsListBox.Name = "teamsListBox";
this.teamsListBox.Size = new System.Drawing.Size(332, 225);
this.teamsListBox.TabIndex = 0;
this.teamsListBox.SelectedIndexChanged += new System.EventHandler(this.teamsListBox_SelectedIndexChanged);
//
// outputValueLabel
//
this.outputValueLabel.AutoSize = true;
this.outputValueLabel.Location = new System.Drawing.Point(89, 272);
this.outputValueLabel.Name = "outputValueLabel";
this.outputValueLabel.Size = new System.Drawing.Size(35, 13);
this.outputValueLabel.TabIndex = 1;
this.outputValueLabel.Text = "label1";
//
// worldSeriesWinnersForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(351, 330);
this.Controls.Add(this.outputValueLabel);
this.Controls.Add(this.teamsListBox);
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "worldSeriesWinnersForm";
this.Text = "World Series Winners";
this.Load += new System.EventHandler(this.worldSeriesWinnersForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListBox teamsListBox;
private System.Windows.Forms.Label outputValueLabel;
}
}
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.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
// Some of this code was taken from the resources folder on Blackboard
namespace WorldSeriesWinners
{
public partial class worldSeriesWinnersForm : Form
{
public worldSeriesWinnersForm()
{
InitializeComponent();
}
private void worldSeriesWinnersForm_Load(object sender, EventArgs e)
{
// Import the list into the program
TeamLoadOnOpen();
}
// Import the teams into the present listbox
private void TeamLoadOnOpen()
{
// Imported items assigned to variable
string teams;
// A StreamReader variable.
StreamReader inputFile;
// Open the file and get a StreamReader object.
inputFile = File.OpenText("Teams.txt");
//Clear anything in list box
teamsListBox.Items.Clear();
//Read the contents using loop
while (!inputFile.EndOfStream)
{
//Get teams from file
teams = inputFile.ReadLine();
// Add teams to listbox
teamsListBox.Items.Add(teams);
}
}
// Method to control what happens on label select
private void teamsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = teamsListBox.GetItemText(teamsListBox.SelectedValue);
outputValueLabel.Text = selected.ToString();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.