How can I make this write and read into the read only textbox, textbox 6? Also h
ID: 3853410 • Letter: H
Question
How can I make this write and read into the read only textbox, textbox 6? Also how can I make it have a debugging log file?
Form1.cs
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
{
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()
{
//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 StreamReader 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
}
internal class txtData
{
public static string Text { get; internal set; }
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
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 Form1());
}
}
}
Form1 Designer.cs
namespace Week8
{
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.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.textBox6 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtLastName
//
this.txtLastName.Location = new System.Drawing.Point(77, 38);
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(77, 73);
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(77, 110);
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(77, 146);
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(77, 182);
this.txtGrade.Name = "txtGrade";
this.txtGrade.Size = new System.Drawing.Size(100, 20);
this.txtGrade.TabIndex = 4;
//
// button1
//
this.button1.Location = new System.Drawing.Point(90, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 5;
this.button1.Text = "Add";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 38);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(58, 13);
this.label1.TabIndex = 6;
this.label1.Text = "Last Name";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(13, 76);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13);
this.label2.TabIndex = 7;
this.label2.Text = "First Name";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 117);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(58, 13);
this.label3.TabIndex = 8;
this.label3.Text = "ID Number";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(13, 153);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(32, 13);
this.label4.TabIndex = 9;
this.label4.Text = "Class";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(12, 189);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(36, 13);
this.label5.TabIndex = 10;
this.label5.Text = "Grade";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(191, 38);
this.textBox6.Multiline = true;
this.textBox6.Name = "textBox6";
this.textBox6.ReadOnly = true;
this.textBox6.Size = new System.Drawing.Size(175, 164);
this.textBox6.TabIndex = 11;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(378, 261);
this.Controls.Add(this.textBox6);
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.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.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;
private System.Windows.Forms.TextBox textBox6;
}
}
Explanation / Answer
For doing such a task you will have to undergo certain changes in your code
Please follow the steps thoroughly to achieve the results.
To add the log file to your program you will have to follow these steps:
<?xmlversion="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="Mode" value ="Daily"/>
<add key ="IntervalMinutes" value ="1"/>
<add key ="ScheduledTime" value ="18:41"/>
</appSettings>
</configuration>
You are all set to go and these are all the requirements that are fulfilled.
Please rate the answer if it helped.....Thankyou
Hope it helps.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.