Visual Basic Question In the Chap9 folder of the student sam

Visual Basic Question

In the Chap9 folder of the student sample programs, you will find the following files:

GirlNames.txt - This file contains a list of the 200 most popular names given to girls born in the United States from 2000 to 2012

BoyNames.txt - This file contains a list of the 200 most popular names given to boys born in the United States from 2000 to 2012

Create an application that reads the contents of the two files into two seperate arrays or Lists. The user should be able to enter a boy\'s name, a girl\'s name, or both, and the application should display messages indicating whether the names were among the most popular.

Note: I can\'t get the txt documents to load on this question. Let me know if I can do anything to get you those.

Solution

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;

namespace Name_Search
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ReadNames(List<string> nameList, string fileName)
        {
            try
            {
                // Open the text file
                StreamReader inputFile = File.OpenText(fileName);

                // Read the names into the list
                while (!inputFile.EndOfStream)
                {
                    nameList.Add(inputFile.ReadLine());
                }

                // Close the file.
                inputFile.Close();
            }
            catch (Exception ex)
            {
                // Display error message.
                MessageBox.Show(ex.Message);
            }
        }

        private bool InputIsValid(out string boyName, out string girlName)
        {
            // Flag variable to indicate whether the input is good.
            bool inputGood = false;

            // Get names from the text boxes
            boyName = txtBoy.Text;
            girlName = txtGirl.Text;

            // Test if there is a name entered in either text box
            if (boyName != \"\" || girlName != \"\")
            {
                inputGood = true;
            }
            else
            {
                // Display error message if no name is entered.
                MessageBox.Show(\"Enter a boy name, a girl name, or both.\");

                // Reset focus.
                txtBoy.Focus();
            }

            return inputGood;
        }

        private string SearchNames(List<string> nameList, string name)
        {
            // Declare variables
            string message;
            int position;

            // Change name to have only the first letter uppercase in case
            // the user enters the name a different way.
            name = char.ToUpper(name[0]) + name.Substring(1).ToLower();

            // Search for name
            position = nameList.IndexOf(name);

            // Was name found in the the list?
            if (position != -1)
            {
                message = name + \" was among the most popular.\";
            }
            else
            {
                message = name + \" was not among the most popular.\";
            }

            return message;
        }

        private void ConvertNames(List<string> nameList)
        {
            for (int index = 0; index <nameList.Count; index++)
            {
                nameList[index] = char.ToUpper(nameList[index][0]) + nameList[index].Substring(1).ToLower();
            }
        }

        private void DisplayResults(string firstMessage, string secondMessage)
        {
            // Display either both messages or one of the messages
            if (firstMessage != \"\" && secondMessage != \"\")
            {
                lblMessage.Text = firstMessage + \"\ \" + secondMessage;
            }
            else if (firstMessage != \"\") {
                lblMessage.Text = firstMessage;
            }
            else
            {
                lblMessage.Text = secondMessage;
            }
          
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            // Declare variables
            string boyName;
            string girlName;
            string boyMessage = \"\";
            string girlMessage = \"\";
          
            // Create a list to hold boy names
            List<string> boyNamesList = new List<string>();

            // Create a list to hold girl names
            List<string> girlNamesList = new List<string>();

            // Read the names from the files into the lists.
            ReadNames(boyNamesList, \"BoyNames.txt\");
            ReadNames(girlNamesList, \"GirlNames.txt\");

            // Convert all names to proper Capitalization
            ConvertNames(boyNamesList);
            ConvertNames(girlNamesList);

            if (InputIsValid(out boyName,out girlName ))
            {
                // Check if a boy\'s name was entered and compare it to the list of names
                if (boyName != \"\" )
                {
                    boyMessage = SearchNames(boyNamesList, boyName);
                }

                // Check if a girl\'s name was entered and compare it to the list of names
                if (girlName != \"\")
                {
                    girlMessage = SearchNames(girlNamesList, girlName);
                }

                // Display if the names matched or not.
                DisplayResults(boyMessage, girlMessage);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            // Clear the text boxes and label.
            txtBoy.Text = \"\";
            txtGirl.Text = \"\";
            lblMessage.Text = \"\";

            // Reset focus.
            txtBoy.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // Close the form.
            this.Close();
        }
    }
}


Form1.Designer.cs

namespace Name_Search
{
    partial class Form1
    {
        /// <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.lblBoyCaption = new System.Windows.Forms.Label();
            this.lblGirlCaption = new System.Windows.Forms.Label();
            this.txtBoy = new System.Windows.Forms.TextBox();
            this.txtGirl = new System.Windows.Forms.TextBox();
            this.lblMessage = new System.Windows.Forms.Label();
            this.btnCheck = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.btnExit = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // lblBoyCaption
            //
            this.lblBoyCaption.AutoSize = true;
            this.lblBoyCaption.Location = new System.Drawing.Point(32, 15);
            this.lblBoyCaption.Name = \"lblBoyCaption\";
            this.lblBoyCaption.Size = new System.Drawing.Size(66, 13);
            this.lblBoyCaption.TabIndex = 0;
            this.lblBoyCaption.Text = \"Boy\\\'s Name:\";
            //
            // lblGirlCaption
            //
            this.lblGirlCaption.AutoSize = true;
            this.lblGirlCaption.Location = new System.Drawing.Point(35, 41);
            this.lblGirlCaption.Name = \"lblGirlCaption\";
            this.lblGirlCaption.Size = new System.Drawing.Size(63, 13);
            this.lblGirlCaption.TabIndex = 1;
            this.lblGirlCaption.Text = \"Girl\\\'s Name:\";
            //
            // txtBoy
            //
            this.txtBoy.Location = new System.Drawing.Point(104, 12);
            this.txtBoy.Name = \"txtBoy\";
            this.txtBoy.Size = new System.Drawing.Size(139, 20);
            this.txtBoy.TabIndex = 0;
            //
            // txtGirl
            //
            this.txtGirl.Location = new System.Drawing.Point(104, 38);
            this.txtGirl.Name = \"txtGirl\";
            this.txtGirl.Size = new System.Drawing.Size(139, 20);
            this.txtGirl.TabIndex = 1;
            //
            // lblMessage
            //
            this.lblMessage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.lblMessage.Location = new System.Drawing.Point(12, 71);
            this.lblMessage.Name = \"lblMessage\";
            this.lblMessage.Size = new System.Drawing.Size(260, 54);
            this.lblMessage.TabIndex = 4;
            this.lblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // btnCheck
            //
            this.btnCheck.Location = new System.Drawing.Point(23, 141);
            this.btnCheck.Name = \"btnCheck\";
            this.btnCheck.Size = new System.Drawing.Size(75, 23);
            this.btnCheck.TabIndex = 2;
            this.btnCheck.Text = \"Check\";
            this.btnCheck.UseVisualStyleBackColor = true;
            this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click);
            //
            // btnClear
            //
            this.btnClear.Location = new System.Drawing.Point(104, 141);
            this.btnClear.Name = \"btnClear\";
            this.btnClear.Size = new System.Drawing.Size(75, 23);
            this.btnClear.TabIndex = 3;
            this.btnClear.Text = \"Clear\";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            //
            // btnExit
            //
            this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnExit.Location = new System.Drawing.Point(185, 141);
            this.btnExit.Name = \"btnExit\";
            this.btnExit.Size = new System.Drawing.Size(75, 23);
            this.btnExit.TabIndex = 4;
            this.btnExit.Text = \"Exit\";
            this.btnExit.UseVisualStyleBackColor = true;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            //
            // Form1
            //
            this.AcceptButton = this.btnCheck;
            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(284, 180);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnCheck);
            this.Controls.Add(this.lblMessage);
            this.Controls.Add(this.txtGirl);
            this.Controls.Add(this.txtBoy);
            this.Controls.Add(this.lblGirlCaption);
            this.Controls.Add(this.lblBoyCaption);
            this.Name = \"Form1\";
            this.Text = \"Name Search\";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblBoyCaption;
        private System.Windows.Forms.Label lblGirlCaption;
        private System.Windows.Forms.TextBox txtBoy;
        private System.Windows.Forms.TextBox txtGirl;
        private System.Windows.Forms.Label lblMessage;
        private System.Windows.Forms.Button btnCheck;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btnExit;
    }
}


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Name_Search
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Visual Basic Question In the Chap9 folder of the student sample programs, you will find the following files: GirlNames.txt - This file contains a list of the 20
Visual Basic Question In the Chap9 folder of the student sample programs, you will find the following files: GirlNames.txt - This file contains a list of the 20
Visual Basic Question In the Chap9 folder of the student sample programs, you will find the following files: GirlNames.txt - This file contains a list of the 20
Visual Basic Question In the Chap9 folder of the student sample programs, you will find the following files: GirlNames.txt - This file contains a list of the 20
Visual Basic Question In the Chap9 folder of the student sample programs, you will find the following files: GirlNames.txt - This file contains a list of the 20
Visual Basic Question In the Chap9 folder of the student sample programs, you will find the following files: GirlNames.txt - This file contains a list of the 20

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site