Would you please look over my code and let meknow what I\'m doing wrong and how
ID: 3593962 • Letter: W
Question
Would you please look over my code and let meknow what I'm doing wrong and how to fix it. Thank You, GOD Bless
using System;
using System.IO;
using System.Windows.Forms;
namespace Account
{
public partial class AccountInfo : Form
{
StreamWriter wrFile;
StreamReader rdFile;
FileStream fsdat_file;
string dat_file;
private object txtBalance;
private readonly object txtFName;
private readonly object txtLName;
private object txtAccount;
private readonly object chkProt;
private object btnSave;
private object btnEnter;
private object btnNext;
private void Sweep()
{
foreach (Control cntrl in this.Controls)
{
if (cntrl is TextBox)
cntrl.Text = "";
else if (cntrl is CheckBox)
{
CheckBox cntl = (CheckBox)cntrl;
cntl.Checked = false;
}
}
}
public AccountInfo()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
if (wrFile != null)
{
try
{
wrFile.Close();
}
catch (IOException)
{
MessageBox.Show("Error while closing file");
}
}
Application.Exit();
}
private void btnEnter_Click(object sender, EventArgs e)
{
try
{
string fName = txtFName.Text.Trim();
string lName = txtLName.Text.Trim();
int accNumber = int.Parse(txtAccount.Text.Trim());
bool overProt = false;
try
{
if (chkProt.Checked == true)
overProt = true;
}
catch (InvalidCastException)
{
MessageBox.Show("Put in y or n in the Overdraft Protection");
return;
}
decimal accBalance = decimal.Parse(txtBalance.Text.Trim());
if (accNumber > 0 && overProt == true) wrFile.WriteLine(accNumber + "," + fName + "," + lName + "," + accBalance + "," + "Yes");
else if (accNumber > 0 && overProt == false) wrFile.WriteLine(accNumber + "," + fName + "," + lName + "," + accBalance + "," + "No");
else
MessageBox.Show("Invalid Account Number", "Error");
}
catch (IOException)
{
MessageBox.Show("Error Writing to File", "Error");
}
catch (FormatException)
{
MessageBox.Show("Invalid Format", "Error");
}
Sweep();
}
private void btnSave_Click(object sender, EventArgs e)
{
DialogResult drAction;
using (SaveFileDialog sfdFile = new SaveFileDialog())
{
sfdFile.CheckFileExists = false;
drAction = sfdFile.ShowDialog();
dat_file = sfdFile.FileName;
}
if (drAction == DialogResult.OK)
{
if (dat_file == String.Empty)
MessageBox.Show("Incorrect File Name", "Error");
else
{
try
{
fsdat_file = new
FileStream(dat_file, FileMode.OpenOrCreate, FileAccess.Write);
wrFile = new
StreamWriter(fsdat_file);
btnSave.Enabled = false;
btnEnter.Enabled = true;
if (chkProt.Visible == false)
chkProt.Visible = true;
}
catch (IOException)
{
MessageBox.Show("Some error occurred. Please try again", "Error");
}
}
}
}
private void txtOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
try
{
if (ofd.ShowDialog() == DialogResult.OK)
{
Sweep();
dat_file = ofd.FileName;
if (dat_file == string.Empty)
MessageBox.Show("Incorrect file Name");
else
{
fsdat_file = new FileStream(dat_file, FileMode.Open, FileAccess.Read);
rdFile = new StreamReader(fsdat_file);
btnOpen.Enabled = false;
btnNext.Enabled = true;
}
}
}
catch (IOException)
{
MessageBox.Show("Erroroccurred while opening the file. Try again");
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (chkProt.Visible == true)
chkProt.Visible = false;
try
{
string nxtDat = rdFile.ReadLine();
string[] datSequence = nxtDat.Split(',');
if (nxtDat != null)
{
txtAccount.Text = datSequence[0];
txtFName.Text = datSequence[1];
txtLName.Text = datSequence[2];
txtBalance.Text = datSequence[3];
txtOverProt.Text = datSequence[4];
}
else
{
rdFile.Close();
btnOpen.Enabled = true;
btnNext.Enabled = false;
Sweep();
}
}
catch (IOException)
{
MessageBox.Show("Some error occurred. Please try again");
}
catch (NullReferenceException)
{
MessageBox.Show("No More Records");
}
}
}
}
Here is the photos. This is assignment 7.16 Visual C# Sixth Edition
(Reading and Wrrting Account information) Create a program that combines the ideas of Fig.1 and Fig 2 to allow a user to write records to and read records from a file. Add an extra field of type bool to the record to indicate whether the account has overdraft protection Fig 1 Creating and writing to a sequential-access fle I Fig. 17.9: CreateFi1eForm.cs 2 I Creating a sequential-access file using System; 4 using Systen.Wi ndows Forms 5 using System.IO; 6 using BankLibrary: 0 nanespace CreateFile 10 public partial class CreateFileForm:BankUIForn private Streankriter filewriter: //writes data to text file 13 // paraneterless constructor public CreatefileFor O InitializeComponent O I end const ructor I7 // event handler for Save Butto private void saveßut ton click object sender. EventArgs /I create and show dialog box enabling user to save file DialogResult result: // result of SaveFileDialog string fileName: // name of file containing data 25 using SaveFile0falog fi1eChooser new SaveFfleDialogO fileChooser.CheckFileExists -false; / let user create file result fileChooser.ShowDialogO; fileName fileChooser FieName; // name of file to save data Iend using f ensure that user clcked "OK if C result DialogResult.o) // ensure that user clicked OK' if C result DialogResult.o) /I show error if user specified invalid file if ( fileName "-string.Empty) MessageBox.ShowC Invalid File Name". "Error" MessageBoxButtons.ok, MessageBoxIcon.Error 39 // save file via FileStream if user specified valid file try // open file with write access FileStream output new FileStrean( fileName FileMode.OpenorCreate, Fi1eAccess.Write) /I sets file to where data is written filewriter new StreamwriterC output 50 // disable Save button and enable Enter button saveButton. Enabled false; enterButton.Enabled true; // end try // handle exception if there's a problem opening the file catch IOException) 58 // notify user if file does not exist MessageBox.Show( "Error opening file", "Error" MessageBoxButtons.ok, MessageBoxIcon.Error; // end catch I/ end else Iend if I/ end method saveBut ton Click // handler for enterBut ton Click private void enterButton Click object sender, EventArgs e) 70 71 /I store TextBox values string array stringt] valuesGetTextBoxvaluesO 73 74 75 76 // Record containing Text Box values to output Record record new RecordO // determine whether TextBox account field is empty if values int TextBoxIndices.ACCOUNT J I-string. Empty) 78 79Explanation / Answer
Remove / comment below lines
private object txtBalance;
private readonly object txtFName;
private readonly object txtLName;
private object txtAccount;
private readonly object chkProt;
private object btnSave;
private object btnEnter;
private object btnNext;
Create a UI for that these components. .ie designer class
go to form area and drag drop all the iteams/ components i.e Text Box, Button, Checkbox etc.
The autogenerated code will be somthing like this.
namespace WindowsFormsApplication5
{
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.txtFName = new System.Windows.Forms.TextBox();
this.txtLName = new System.Windows.Forms.TextBox();
this.txtAccount = new System.Windows.Forms.TextBox();
this.chkProt = new System.Windows.Forms.CheckBox();
this.txtBalance = new System.Windows.Forms.TextBox();
this.btnSave = new System.Windows.Forms.Button();
this.btnEnter = new System.Windows.Forms.Button();
this.btnOpen = new System.Windows.Forms.Button();
this.btnNext = new System.Windows.Forms.Button();
this.txtOverProt = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtFName
//
this.txtFName.Location = new System.Drawing.Point(294, 113);
this.txtFName.Name = "txtFName";
this.txtFName.Size = new System.Drawing.Size(100, 20);
this.txtFName.TabIndex = 0;
//
// txtLName
//
this.txtLName.Location = new System.Drawing.Point(294, 151);
this.txtLName.Name = "txtLName";
this.txtLName.Size = new System.Drawing.Size(100, 20);
this.txtLName.TabIndex = 1;
//
// txtAccount
//
this.txtAccount.Location = new System.Drawing.Point(294, 193);
this.txtAccount.Name = "txtAccount";
this.txtAccount.Size = new System.Drawing.Size(100, 20);
this.txtAccount.TabIndex = 2;
//
// chkProt
//
this.chkProt.AutoSize = true;
this.chkProt.Location = new System.Drawing.Point(309, 54);
this.chkProt.Name = "chkProt";
this.chkProt.Size = new System.Drawing.Size(63, 17);
this.chkProt.TabIndex = 3;
this.chkProt.Text = "chkProt";
this.chkProt.UseVisualStyleBackColor = true;
//
// txtBalance
//
this.txtBalance.Location = new System.Drawing.Point(294, 229);
this.txtBalance.Name = "txtBalance";
this.txtBalance.Size = new System.Drawing.Size(100, 20);
this.txtBalance.TabIndex = 4;
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(321, 293);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(75, 23);
this.btnSave.TabIndex = 5;
this.btnSave.Text = "button1";
this.btnSave.UseVisualStyleBackColor = true;
//
// btnEnter
//
this.btnEnter.Location = new System.Drawing.Point(419, 293);
this.btnEnter.Name = "btnEnter";
this.btnEnter.Size = new System.Drawing.Size(75, 23);
this.btnEnter.TabIndex = 6;
this.btnEnter.Text = "button1";
this.btnEnter.UseVisualStyleBackColor = true;
//
// btnOpen
//
this.btnOpen.Location = new System.Drawing.Point(229, 292);
this.btnOpen.Name = "btnOpen";
this.btnOpen.Size = new System.Drawing.Size(75, 23);
this.btnOpen.TabIndex = 7;
this.btnOpen.Text = "button1";
this.btnOpen.UseVisualStyleBackColor = true;
//
// btnNext
//
this.btnNext.Location = new System.Drawing.Point(139, 291);
this.btnNext.Name = "btnNext";
this.btnNext.Size = new System.Drawing.Size(75, 23);
this.btnNext.TabIndex = 8;
this.btnNext.Text = "button1";
this.btnNext.UseVisualStyleBackColor = true;
//
// txtOverProt
//
this.txtOverProt.Location = new System.Drawing.Point(71, 179);
this.txtOverProt.Name = "txtOverProt";
this.txtOverProt.Size = new System.Drawing.Size(100, 20);
this.txtOverProt.TabIndex = 9;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(547, 353);
this.Controls.Add(this.txtOverProt);
this.Controls.Add(this.btnNext);
this.Controls.Add(this.btnOpen);
this.Controls.Add(this.btnEnter);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.txtBalance);
this.Controls.Add(this.chkProt);
this.Controls.Add(this.txtAccount);
this.Controls.Add(this.txtLName);
this.Controls.Add(this.txtFName);
this.Name = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtFName;
private System.Windows.Forms.TextBox txtLName;
private System.Windows.Forms.TextBox txtAccount;
private System.Windows.Forms.CheckBox chkProt;
private System.Windows.Forms.TextBox txtBalance;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnEnter;
private System.Windows.Forms.Button btnOpen;
private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.TextBox txtOverProt;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.