Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Visual C# Problem : Create a Visual C# application that displays the contents of

ID: 3601047 • Letter: V

Question

Visual C# Problem: 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 to 2012. The two files used are Teams.txt , which contains a list of the names of teams that have won the Championship at least once, and WorldSeriesWinners.txt - this file contains a chronological list of the World Series winning teams from 1903 - 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. Tip: Read the contents of the WorldSeriesWinners.txt file into a List or an array. When the user selects a team, an algorithm should step through the list or array counting the number of times the selected team appears. (for the purpose of this answer/code, the contents of the text files do not really matter, as long as the code works.

This should be in Visual C#, not Visual Basic. And the code should be making use of class, constructors, methods and (if possible) creating objects that is Random rand = new random

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();
        }
    }
}