Answer to Question #4956 in C# for vassan

Question #4956
Write a GUI application which allows the user to enter a name, password and phone number. When they click the Save button, the three fields are appended to a text file in a single line, separated by commas. The application should handle 5 records. Use it to add the 5 records to the text file.
1
Expert's answer
2011-11-03T09:13:55-0400
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace q4956
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void btnSave_Click(object sender, EventArgs e)
{
string name = "";
string password = "";
string phone = "";

if (txtName.Text.Length != 0)
{
name = txtName.Text;
}
else
{
lblNameError.Text = "*";
return;
}
if (txtPassword.Text.Length != 0)
{
password = txtPassword.Text;
}
else
{
lblPasswordError.Text = "*";
return;
} if (txtPhone.Text.Length != 0)
{
phone = txtPhone.Text;
}
else
{
lblPhoneError.Text = "*";
return;
}
lblNameError.Text = "";
lblPasswordError.Text = "";
lblPhoneError.Text = "";
StreamWriter sr = new StreamWriter("db.txt",true);
sr.WriteLine(name + ","+password+","+phone);
sr.Close();
sr.Dispose();
txtName.Text = "";
txtPassword.Text = "";
txtPhone.Text = "";
}
}
}


form 1 designer

namespace q4956
{
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.label1 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.txtPassword = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPhone = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.lblNameError = new System.Windows.Forms.Label();
this.lblPasswordError = new System.Windows.Forms.Label();
this.lblPhoneError = new System.Windows.Forms.Label();
this.btnSave = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(35, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(38, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Name:";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(74, 12);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(100, 20);
this.txtName.TabIndex = 1;
//
// txtPassword
//
this.txtPassword.Location = new System.Drawing.Point(74, 39);
this.txtPassword.Name = "txtPassword";
this.txtPassword.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(100, 20);
this.txtPassword.TabIndex = 2;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(17, 42);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Password:";
//
// txtPhone
//
this.txtPhone.Location = new System.Drawing.Point(74, 66);
this.txtPhone.Name = "txtPhone";
this.txtPhone.Size = new System.Drawing.Size(100, 20);
this.txtPhone.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(33, 69);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 13);
this.label3.TabIndex = 5;
this.label3.Text = "Phone:";
//
// lblNameError
//
this.lblNameError.AutoSize = true;
this.lblNameError.ForeColor = System.Drawing.Color.Red;
this.lblNameError.Location = new System.Drawing.Point(181, 15);
this.lblNameError.Name = "lblNameError";
this.lblNameError.Size = new System.Drawing.Size(0, 13);
this.lblNameError.TabIndex = 6;
//
// lblPasswordError
//
this.lblPasswordError.AutoSize = true;
this.lblPasswordError.ForeColor = System.Drawing.Color.Red;
this.lblPasswordError.Location = new System.Drawing.Point(181, 42);
this.lblPasswordError.Name = "lblPasswordError";
this.lblPasswordError.Size = new System.Drawing.Size(0, 13);
this.lblPasswordError.TabIndex = 7;
//
// lblPhoneError
//
this.lblPhoneError.AutoSize = true;
this.lblPhoneError.ForeColor = System.Drawing.Color.Red;
this.lblPhoneError.Location = new System.Drawing.Point(181, 69);
this.lblPhoneError.Name = "lblPhoneError";
this.lblPhoneError.Size = new System.Drawing.Size(0, 13);
this.lblPhoneError.TabIndex = 8;
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(74, 103);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(75, 23);
this.btnSave.TabIndex = 9;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(214, 148);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.lblPhoneError);
this.Controls.Add(this.lblPasswordError);
this.Controls.Add(this.lblNameError);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPhone);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtPhone;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label lblNameError;
private System.Windows.Forms.Label lblPasswordError;
private System.Windows.Forms.Label lblPhoneError;
private System.Windows.Forms.Button btnSave;
}
}

Program

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

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS