Create a Windows Form Application project using C# language. Create a Form with
ID: 3797675 • Letter: C
Question
Create a Windows Form Application project using C# language.
Create a Form with a text property of Friends
Add a Class file to your project and name it Friend
In the Friend class declare the following:
private string lastName;
private string phoneNumber;
private int month;
private int day;
private string firstName;
Write accessors for each of the declared class variables
Override the class ToString method to return a string in the following format: FirstName + ',' + LastName + ',' + PhoneNumber + ',' + Month + "," + Day
Add a TabControl to the form and size it so that it fills the form
Name the first tabPage Entry.
On the Entry tabPage add four textboxes to take entry for a first name, last name, birth month, and birthday and add a MaskedTextbox to take entry for a phone number formatted as 999-9999.
Add an Enter Friend button which will process the entries made in the textboxes; writing each entry to a file and an Exit button which will exit the application
Name the second tabPage Read
On the Read tabPage, place a Read button which will read the file entries written to the file on the Entry tabPage, an Exit button that will call the Entry page exit button click event and a listbox that will display each entry read from the file when the Read button is clicked
Name the third tabPage Reminder
On the Reminder tabPage, place a textbox that will take an input for the birth month entries that will be displayed, a Reminder button which will read the file entries and display the file entries which have a month value that matches the value entered in the textbox, an Exit button that will call the Entry page exit button click event, and a listbox that will display each matching entry read from the file when the Reminder button is clicked
At the Form level instantiate a Friend object and declare/instantiate a FileStream/StreamReader and a FileStream/StreamWriter object that will open a Friends.txt file with the FileMode set to OpenCreate, FileAccess set to Read, FileShare set to ReadWrite and FileMode set to Append, FileAccess set to Write and FileShare set to ReadWrite respectively
The data entry button will extract the inputs from the textboxes on the Entry tab and will:
Write the data as a comma delimited record to a .txt file using the Friend object ToString method you overrode in the Friend class; also display a messagebox that displays what was written to the file
Include a try-catch block that will display a messagebox with error message for any errors encountered in reading from the textboxes and writing to the file
Clear the write stream buffer
Clear entries in the textboxes
The Read button will accomplish the following:
Read the file and display in the listbox each record splitting out the fields, eliminating the comma delimiters and placing spaces between the fields
Reset the file position pointer to the beginning of the file for the next file read
The Reminder button will accomplish the following:
Take a text entry for the birth month to be searched for
Read the file and display in the listbox each record splitting out the fields, eliminating the comma delimiters and placing spaces between the fields
The birth month and day will be separated by a "/" when the record(s) is displayed
Reset the file position pointer to the beginning of the file for the next file read
Clear the entry in the textbox
When clicking out of tabPage 2 and 3 which contain the listboxes clear the display contents of the listboxes
When the application is terminated close all open FileStream objects
Internal documentation.
Friends Entry Read Reminder Enter First Name: Enter Last Name Enter Phone Number: Enter Birth Month (eg: 100 Enter Birth Day (eg: 24) Enter Friend ExitExplanation / Answer
Solution: See the code below:
1. Friend class: Friend.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;
namespace Friends
{
public partial class Form1 : Form
{
private Friend friend; //Friend object
public Form1()
{
InitializeComponent();
friend = new Friend();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button4_Click(object sender, EventArgs e)
{
button2_Click(sender, e);
}
private void label6_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
button2_Click(sender, e);
}
}
}
--------------------------------------
2. Form1 class: 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 Friends
{
public partial class Form1 : Form
{
private Friend friend; //Friend object
private string friendsDataFileName; //friends data file name
private FileStream fileReadingStream; //file stream to read friends data file name
private FileStream fileWritingStream; //file stream to write friends data file name
public Form1()
{
InitializeComponent();
friend = new Friend();
friendsDataFileName = @".Friends.txt";
fileReadingStream = new FileStream(friendsDataFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
fileWritingStream = new FileStream(friendsDataFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
friend.FirstName = firstNameBox.Text;
friend.LastName = lastNameBox.Text;
friend.PhoneNumber = phoneNumberBox.Text;
friend.Month = int.Parse(birthMonthBox.Text);
friend.Day = int.Parse(birthDayBox.Text);
StreamWriter sw = new StreamWriter(fileWritingStream);
sw.WriteLine(friend.ToString());
sw.Close();
}
catch(FileNotFoundException)
{
MessageBox.Show("Error");
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button4_Click(object sender, EventArgs e)
{
button2_Click(sender, e);
}
private void label6_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
if(fileReadingStream.)
listBox2.Items.Clear();
string month = textBox5.Text;
StreamReader sr = new StreamReader(fileReadingStream);
string line = "";
while ((line = sr.ReadLine()) != null)
{
if(line.Contains(month))
{
listBox2.Items.Add(line);
}
else
{
MessageBox.Show("No match found!");
}
}
sr.Close();
}
private void button6_Click(object sender, EventArgs e)
{
button2_Click(sender, e);
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
StreamReader sr = new StreamReader(fileReadingStream);
string line="";
while((line=sr.ReadLine())!=null)
{
listBox1.Items.Add(line);
}
sr.Close();
}
}
}
-------------------------------
3. Form1.Designer.cs:
-----------------------------
namespace Friends
{
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.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.phoneNumberBox = new System.Windows.Forms.MaskedTextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.birthMonthBox = new System.Windows.Forms.TextBox();
this.lastNameBox = new System.Windows.Forms.TextBox();
this.birthDayBox = new System.Windows.Forms.TextBox();
this.firstNameBox = new System.Windows.Forms.TextBox();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.label6 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.tabPage3 = new System.Windows.Forms.TabPage();
this.listBox2 = new System.Windows.Forms.ListBox();
this.button6 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.label7 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
this.tabPage3.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Location = new System.Drawing.Point(1, -1);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(509, 359);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.button2);
this.tabPage1.Controls.Add(this.button1);
this.tabPage1.Controls.Add(this.label5);
this.tabPage1.Controls.Add(this.label4);
this.tabPage1.Controls.Add(this.phoneNumberBox);
this.tabPage1.Controls.Add(this.label3);
this.tabPage1.Controls.Add(this.label2);
this.tabPage1.Controls.Add(this.label1);
this.tabPage1.Controls.Add(this.birthMonthBox);
this.tabPage1.Controls.Add(this.lastNameBox);
this.tabPage1.Controls.Add(this.birthDayBox);
this.tabPage1.Controls.Add(this.firstNameBox);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(501, 333);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "Entry";
this.tabPage1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(330, 286);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 11;
this.button2.Text = "Exit";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(330, 235);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 10;
this.button1.Text = "Enter Friend";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(44, 289);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(120, 13);
this.label5.TabIndex = 9;
this.label5.Text = "Enter Birth Day (eg. 24):";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(44, 235);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(131, 13);
this.label4.TabIndex = 8;
this.label4.Text = "Enter Birth Month (eg. 10):";
//
// phoneNumberBox
//
this.phoneNumberBox.Location = new System.Drawing.Point(190, 170);
this.phoneNumberBox.Mask = "000-0000";
this.phoneNumberBox.Name = "phoneNumberBox";
this.phoneNumberBox.Size = new System.Drawing.Size(60, 20);
this.phoneNumberBox.TabIndex = 7;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(44, 170);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(109, 13);
this.label3.TabIndex = 6;
this.label3.Text = "Enter Phone Number:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(44, 108);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(89, 13);
this.label2.TabIndex = 5;
this.label2.Text = "Enter Last Name:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(44, 50);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 13);
this.label1.TabIndex = 4;
this.label1.Text = "Enter First Name:";
//
// birthMonthBox
//
this.birthMonthBox.Location = new System.Drawing.Point(190, 235);
this.birthMonthBox.Name = "birthMonthBox";
this.birthMonthBox.Size = new System.Drawing.Size(60, 20);
this.birthMonthBox.TabIndex = 3;
//
// lastNameBox
//
this.lastNameBox.Location = new System.Drawing.Point(190, 108);
this.lastNameBox.Name = "lastNameBox";
this.lastNameBox.Size = new System.Drawing.Size(159, 20);
this.lastNameBox.TabIndex = 2;
//
// birthDayBox
//
this.birthDayBox.Location = new System.Drawing.Point(190, 286);
this.birthDayBox.Name = "birthDayBox";
this.birthDayBox.Size = new System.Drawing.Size(60, 20);
this.birthDayBox.TabIndex = 1;
//
// firstNameBox
//
this.firstNameBox.Location = new System.Drawing.Point(190, 50);
this.firstNameBox.Name = "firstNameBox";
this.firstNameBox.Size = new System.Drawing.Size(159, 20);
this.firstNameBox.TabIndex = 0;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.label6);
this.tabPage2.Controls.Add(this.listBox1);
this.tabPage2.Controls.Add(this.button4);
this.tabPage2.Controls.Add(this.button3);
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(501, 333);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "Read";
this.tabPage2.UseVisualStyleBackColor = true;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(21, 73);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(81, 13);
this.label6.TabIndex = 3;
this.label6.Text = "Friends' Details:";
this.label6.Click += new System.EventHandler(this.label6_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(24, 100);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(362, 199);
this.listBox1.TabIndex = 2;
//
// button4
//
this.button4.Location = new System.Drawing.Point(131, 37);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 1;
this.button4.Text = "Exit";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(24, 37);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 0;
this.button3.Text = "Read";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// tabPage3
//
this.tabPage3.Controls.Add(this.listBox2);
this.tabPage3.Controls.Add(this.button6);
this.tabPage3.Controls.Add(this.button5);
this.tabPage3.Controls.Add(this.label7);
this.tabPage3.Controls.Add(this.textBox5);
this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Name = "tabPage3";
this.tabPage3.Padding = new System.Windows.Forms.Padding(3);
this.tabPage3.Size = new System.Drawing.Size(501, 333);
this.tabPage3.TabIndex = 2;
this.tabPage3.Text = "Reminder";
this.tabPage3.UseVisualStyleBackColor = true;
//
// listBox2
//
this.listBox2.FormattingEnabled = true;
this.listBox2.Location = new System.Drawing.Point(24, 88);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(391, 225);
this.listBox2.TabIndex = 4;
//
// button6
//
this.button6.Location = new System.Drawing.Point(402, 44);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 3;
this.button6.Text = "Exit";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(312, 44);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 2;
this.button5.Text = "Reminder";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(21, 44);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(149, 13);
this.label7.TabIndex = 1;
this.label7.Text = "Enter Birth Moth for Reminder:";
this.label7.Click += new System.EventHandler(this.label7_Click);
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(186, 44);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(97, 20);
this.textBox5.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(508, 356);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Friends";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage1.PerformLayout();
this.tabPage2.ResumeLayout(false);
this.tabPage2.PerformLayout();
this.tabPage3.ResumeLayout(false);
this.tabPage3.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox birthMonthBox;
private System.Windows.Forms.TextBox lastNameBox;
private System.Windows.Forms.TextBox birthDayBox;
private System.Windows.Forms.TextBox firstNameBox;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TabPage tabPage3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.MaskedTextBox phoneNumberBox;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.ListBox listBox2;
}
}
----------------------------------
4. Program.cs:
----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Friends
{
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());
}
}
}
-------------------------------------------