Ok I will try this again, I seem to have an error with the text boxes and the co
ID: 3853135 • Letter: O
Question
Ok I will try this again, I seem to have an error with the text boxes and the codes I am including the codes and screen shots
// Name spaces for application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Week8
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new grades());
}
internal class grades : Form
{
}
public partial class Form1 : Form
{
// Get the location where the data file would be created
string fileLoc = Application.StartupPath +
"\Datafile.txt";
/* Constructor of the class to create the GUI of the form */
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
throw new NotImplementedException();
}
private void button1_Click(object sender, EventArgs e)
{
//form the desired string of the text boxes' text
string stu_rec = xtLastName.Text + "," +
txtFirstName.Text + ":" +
txtIDNumber.Text + ""
+ txtClass.Text + "" + txtGrade.Text;
try
{
//Create an Object of the SteamWriter
// class to write the data to the file
StreamWriter dat_file =
new StreamWriter(fileLoc, true);
//Write the data to the file
dat_file.WriteLine(stu_rec);
//clear the text of all the textbox controls
foreach (Control t in this.Controls)
{
if (t is TextBox)
t.Text = "";
}
//close the stream
dat_file.Close();
}
//throw an exception if the exception occurs
catch (IOException)
{
// show the error message
MessageBox.Show("Error Occurred. Please try again");
}
try
{
//create a new object of the SteamReader class to read the data from the same file
StreamReader read_file =
new StreamReader(fileLoc);
//variable to read the data
string read_rec = "";
//control where the data is to be shown
txtData.Text = "";
//read the data till the end of the file
do
{
//read the data and show it in the text box
read_rec = read_file.ReadLine();
txtData.Text += read_rec + " ";
}
while (read_rec != null);
//close the stream
read_file.Close();
}
//throw an exception if exception occurs
catch (IOException)
{
//show the message error
MessageBox.Show("Problem while reading the file. Please try again.");
}
}
}//End class
} // End Namespace
}
namespace StudentGrades
{
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.txtLastName = new System.Windows.Forms.TextBox();
this.txtFirstName = new System.Windows.Forms.TextBox();
this.txtIDNumber = new System.Windows.Forms.TextBox();
this.txtClass = new System.Windows.Forms.TextBox();
this.txtGrade = new System.Windows.Forms.TextBox();
this.Read = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
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.SuspendLayout();
//
// txtLastName
//
this.txtLastName.Location = new System.Drawing.Point(69, 12);
this.txtLastName.Name = "txtLastName";
this.txtLastName.Size = new System.Drawing.Size(100, 20);
this.txtLastName.TabIndex = 0;
//
// txtFirstName
//
this.txtFirstName.Location = new System.Drawing.Point(69, 47);
this.txtFirstName.Name = "txtFirstName";
this.txtFirstName.Size = new System.Drawing.Size(100, 20);
this.txtFirstName.TabIndex = 1;
//
// txtIDNumber
//
this.txtIDNumber.Location = new System.Drawing.Point(69, 86);
this.txtIDNumber.Name = "txtIDNumber";
this.txtIDNumber.Size = new System.Drawing.Size(100, 20);
this.txtIDNumber.TabIndex = 2;
//
// txtClass
//
this.txtClass.Location = new System.Drawing.Point(69, 126);
this.txtClass.Name = "txtClass";
this.txtClass.Size = new System.Drawing.Size(100, 20);
this.txtClass.TabIndex = 3;
//
// txtGrade
//
this.txtGrade.Location = new System.Drawing.Point(69, 167);
this.txtGrade.Name = "txtGrade";
this.txtGrade.Size = new System.Drawing.Size(100, 20);
this.txtGrade.TabIndex = 4;
//
// Read
//
this.Read.Location = new System.Drawing.Point(194, 12);
this.Read.Multiline = true;
this.Read.Name = "Read";
this.Read.ReadOnly = true;
this.Read.Size = new System.Drawing.Size(189, 175);
this.Read.TabIndex = 5;
//
// button1
//
this.button1.Location = new System.Drawing.Point(81, 193);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 6;
this.button1.Text = "Add";
this.button1.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(6, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(58, 13);
this.label1.TabIndex = 7;
this.label1.Text = "Last Name";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(6, 50);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13);
this.label2.TabIndex = 8;
this.label2.Text = "First Name";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(6, 89);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(58, 13);
this.label3.TabIndex = 9;
this.label3.Text = "ID Number";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(16, 129);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(32, 13);
this.label4.TabIndex = 10;
this.label4.Text = "Class";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(16, 170);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(36, 13);
this.label5.TabIndex = 11;
this.label5.Text = "Grade";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 264);
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.button1);
this.Controls.Add(this.Read);
this.Controls.Add(this.txtGrade);
this.Controls.Add(this.txtClass);
this.Controls.Add(this.txtIDNumber);
this.Controls.Add(this.txtFirstName);
this.Controls.Add(this.txtLastName);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtLastName;
private System.Windows.Forms.TextBox txtFirstName;
private System.Windows.Forms.TextBox txtIDNumber;
private System.Windows.Forms.TextBox txtClass;
private System.Windows.Forms.TextBox txtGrade;
private System.Windows.Forms.TextBox Read;
private System.Windows.Forms.Button button1;
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;
}
}
Explanation / Answer
DesignerCOde is working good .
but the cs code not arranged well"
Here is code:
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.Threading.Tasks;
using System.Windows.Forms;
namespace Week8
{
//internal class grades : Form
//{
//}
public partial class Form1: Form
{
// Get the location where the data file would be created
string fileLoc = Application.StartupPath +
"\Datafile.txt";
/* Constructor of the class to create the GUI of the form */
public Window()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new grades());
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//form the desired string of the text boxes' text
string stu_rec = txtLastName.Text + "," +
txtFirstName.Text + ":" +
txtIDNumber.Text + ""
+ txtClass.Text + "" + txtGrade.Text;
try
{
//Create an Object of the SteamWriter
// class to write the data to the file
StreamWriter dat_file =
new StreamWriter(fileLoc, true);
//Write the data to the file
dat_file.WriteLine(stu_rec);
//clear the text of all the textbox controls
foreach (Control t in this.Controls)
{
if (t is TextBox)
t.Text = "";
}
//close the stream
dat_file.Close();
}
//throw an exception if the exception occurs
catch (IOException)
{
// show the error message
MessageBox.Show("Error Occurred. Please try again");
}
try
{
//create a new object of the SteamReader class to read the data from the same file
StreamReader read_file =
new StreamReader(fileLoc);
//variable to read the data
string read_rec = "";
//control where the data is to be shown
// txtData.Text = "";
//read the data till the end of the file
do
{
//read the data and show it in the text box
read_rec = read_file.ReadLine();
// txtData.Text += read_rec + " ";
}
while (read_rec != null);
//close the stream
read_file.Close();
}
//throw an exception if exception occurs
catch (IOException)
{
//show the message error
MessageBox.Show("Problem while reading the file. Please try again.");
}
}//End class
} // End Namespace
}
I have removed the following :
From the code . and also txtData is initialized . Set the txtData value.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.