Question

Using C#, create a form-based program that will accept the following data and then store the...

  1. Using C#, create a form-based program that will accept the following data and then store the data (in a pipe delimited file).
    1. full name
    2. street address
    3. apartment number
    4. City
    5. state
    6. zip

Add ten records and store the data one line per record in one text file. Create a button that will read the file, parse the data and print each record on a separate line in a datagridview box. Since the pipes indicate the begin and end of each value, do not print them. Include appropriate exception-handling techniques in your solution.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main code

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 Write_into_file_and_read_from_file
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{
if (txtAddress.Text == "" && txtAptNumber.Text == "" || txtCity.Text == "" || txtName.Text == "" || txtState.Text == "" || txtZip.Text == "")
{
MessageBox.Show("Please enter all the recodrds.");
}
else
{
using (StreamWriter w = File.AppendText("myFile.txt"))
{
w.WriteLine(txtName.Text+"|"+txtAddress.Text+"|"+txtAptNumber.Text+"|"+txtCity.Text+"|"+txtState.Text+"|"+txtZip.Text);
}
MessageBox.Show("Record added to file.");
}
}

private void btnRead_Click(object sender, EventArgs e)
{
try
{
string line;
System.IO.StreamReader file = new System.IO.StreamReader(Application.StartupPath + "\\myFile.txt");
  
  
while ((line = file.ReadLine()) != null)
{
string[] strArray = line.Split('|');
dataGridNameList.Rows.Add(strArray[0], strArray[1], strArray[2], strArray[3], strArray[4], strArray[5]);
}
}
catch (Exception )
{
MessageBox.Show("File not found");
}
}

private void Form1_Load(object sender, EventArgs e)
{
  
}
}
}

Form1.Designer.cs

namespace Write_into_file_and_read_from_file
{
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.txtName = new System.Windows.Forms.TextBox();
this.txtAddress = new System.Windows.Forms.TextBox();
this.txtAptNumber = new System.Windows.Forms.TextBox();
this.txtCity = new System.Windows.Forms.TextBox();
this.txtState = new System.Windows.Forms.TextBox();
this.txtZip = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnRead = new System.Windows.Forms.Button();
this.dataGridNameList = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridNameList)).BeginInit();
this.SuspendLayout();
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(262, 47);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(143, 20);
this.txtName.TabIndex = 0;
//
// txtAddress
//
this.txtAddress.Location = new System.Drawing.Point(262, 87);
this.txtAddress.Name = "txtAddress";
this.txtAddress.Size = new System.Drawing.Size(143, 20);
this.txtAddress.TabIndex = 1;
//
// txtAptNumber
//
this.txtAptNumber.Location = new System.Drawing.Point(262, 126);
this.txtAptNumber.Name = "txtAptNumber";
this.txtAptNumber.Size = new System.Drawing.Size(143, 20);
this.txtAptNumber.TabIndex = 2;
//
// txtCity
//
this.txtCity.Location = new System.Drawing.Point(262, 163);
this.txtCity.Name = "txtCity";
this.txtCity.Size = new System.Drawing.Size(143, 20);
this.txtCity.TabIndex = 3;
//
// txtState
//
this.txtState.Location = new System.Drawing.Point(262, 201);
this.txtState.Name = "txtState";
this.txtState.Size = new System.Drawing.Size(143, 20);
this.txtState.TabIndex = 4;
//
// txtZip
//
this.txtZip.Location = new System.Drawing.Point(262, 242);
this.txtZip.Name = "txtZip";
this.txtZip.Size = new System.Drawing.Size(143, 20);
this.txtZip.TabIndex = 5;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(181, 50);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(54, 13);
this.label1.TabIndex = 6;
this.label1.Text = "Full Name";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(159, 94);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(76, 13);
this.label2.TabIndex = 7;
this.label2.Text = "Street Address";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(142, 129);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(93, 13);
this.label3.TabIndex = 8;
this.label3.Text = "Apartment number";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(211, 170);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(24, 13);
this.label4.TabIndex = 9;
this.label4.Text = "City";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(203, 204);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(32, 13);
this.label5.TabIndex = 10;
this.label5.Text = "State";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(211, 249);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(22, 13);
this.label6.TabIndex = 11;
this.label6.Text = "Zip";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(93, 304);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 12;
this.btnAdd.Text = "Add";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRead
//
this.btnRead.Location = new System.Drawing.Point(411, 304);
this.btnRead.Name = "btnRead";
this.btnRead.Size = new System.Drawing.Size(75, 23);
this.btnRead.TabIndex = 13;
this.btnRead.Text = "Read";
this.btnRead.UseVisualStyleBackColor = true;
this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
//
// dataGridNameList
//
this.dataGridNameList.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridNameList.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2,
this.Column3,
this.Column4,
this.Column5,
this.Column6});
this.dataGridNameList.Location = new System.Drawing.Point(47, 352);
this.dataGridNameList.Name = "dataGridNameList";
this.dataGridNameList.Size = new System.Drawing.Size(536, 150);
this.dataGridNameList.TabIndex = 14;
//
// Column1
//
this.Column1.HeaderText = "Name";
this.Column1.Name = "Column1";
//
// Column2
//
this.Column2.HeaderText = "Sreet Address";
this.Column2.Name = "Column2";
//
// Column3
//
this.Column3.HeaderText = "Apartment Number";
this.Column3.Name = "Column3";
//
// Column4
//
this.Column4.HeaderText = "City";
this.Column4.Name = "Column4";
//
// Column5
//
this.Column5.HeaderText = "State";
this.Column5.Name = "Column5";
//
// Column6
//
this.Column6.HeaderText = "Zip";
this.Column6.Name = "Column6";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(617, 514);
this.Controls.Add(this.dataGridNameList);
this.Controls.Add(this.btnRead);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtZip);
this.Controls.Add(this.txtState);
this.Controls.Add(this.txtCity);
this.Controls.Add(this.txtAptNumber);
this.Controls.Add(this.txtAddress);
this.Controls.Add(this.txtName);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridNameList)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtAddress;
private System.Windows.Forms.TextBox txtAptNumber;
private System.Windows.Forms.TextBox txtCity;
private System.Windows.Forms.TextBox txtState;
private System.Windows.Forms.TextBox txtZip;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnRead;
private System.Windows.Forms.DataGridView dataGridNameList;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
private System.Windows.Forms.DataGridViewTextBoxColumn Column5;
private System.Windows.Forms.DataGridViewTextBoxColumn Column6;
}
}

Design

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Using C#, create a form-based program that will accept the following data and then store the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • signature 1. Create a new NetBeans Java project. The name of the project has to be...

    signature 1. Create a new NetBeans Java project. The name of the project has to be the first part of the name you write on the test sheet. The name of the package has to be testo You can chose a name for the Main class. (2p) 2. Create a new class named Address in the test Two package. This class has the following attributes: city: String-the name of the city zip: int - the ZIP code of the city...

  • In this assignment, you will write one (1) medium size C program. The program needs to...

    In this assignment, you will write one (1) medium size C program. The program needs to be structured using multiple functions, i.e., you are required to organize your code into distinct logical units. The following set of instructions provide the specific requirements for the program. Make sure to test thoroughly before submitting. Write   a   program,   named   program1.c,   that   reads   and   processes   employee   records   (data   about   an   employee).   Each   employee   record   must   be   stored   using   a   struct   that   contains   the   following  ...

  • Using C# Create a “Main” method that will take user input one line at a time...

    Using C# Create a “Main” method that will take user input one line at a time until they enter the phrase “done”. Store each line entered into an ArrayList, so that one element of the ArrayList is one line of user input. You do NOT need to write your own ArrayList class, please use the standard library implementation of an ArrayList. After the user finishes typing in input, ask the user for a location and file name. Save the contents...

  • Inventory Program (C++) Write a program that uses a structure to store the following inventory data...

    Inventory Program (C++) Write a program that uses a structure to store the following inventory data in a file: - Item Description -Quantity on Hand -Wholesale cost -Retail cost -Date Added to Inventory The program should have a menu that allows the user to perform the following tasks: -Add new records to file -Display any record in the file -Change any record in the file Input Validation: The program should not accept quantities, or wholesale or retail costs, less than...

  • Write a program in C++ with comments! Create an abstract class Property, containing the data items...

    Write a program in C++ with comments! Create an abstract class Property, containing the data items owner, address and price. Add an abstract method showOwner(). Also provide getter/setters for all the data items. Make validations if necessary in the setter so that no invalid values are entered. Create a class House, to inherit the Property class. This class should contain additional data item – yardSize – an integer – the size of the yard of the house, getter and setter...

  • Write a program in C++: Using classes, design an online address book to keep track of the names f...

    Write a program in C++: Using classes, design an online address book to keep track of the names first and last, addresses, phone numbers, and dates of birth. The menu driven program should perform the following operations: Load the data into the address book from a file Write the data in the address book to a file Search for a person by last name or phone number (one function to do both) Add a new entry to the address book...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • Program in C Student Data using Structs In this assignment you will create a program (using...

    Program in C Student Data using Structs In this assignment you will create a program (using multiple .c files) to solve the following problem: You have been hired by the university to create a program that will read in input from a .txt file. This input will contain a student name, student id #, their age and their current gpa. You should use the following struct for your student data: struct Student{                 char name[50];                 int id;                 float...

  • write a C program to make the dictionary USING LINKED LISTS and STORE DATA in File...

    write a C program to make the dictionary USING LINKED LISTS and STORE DATA in File HANDLING..... you have to create functions 1. insert 2. delete 3. search(if we search with any letter then the words with same first letter have been shown) 4. display 6. exit Appearance should be like proper dictionary.

  • CISC 1115 Assignment 6 Write a complete program, including javadoc comments, to process voter statistics Input...

    CISC 1115 Assignment 6 Write a complete program, including javadoc comments, to process voter statistics Input to Program: A file containing lines of data, such that each line has 2 integers on it The first integer represents a zip code (in Brooklyn or the Bronx), and the second represents the number of voters in that zip code. Output: All output may be displayed to the screen. In main: 1. Your program will read in all of the data in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT