Help, I am using the newest version of Visual studio and this code has multiple
ID: 3853029 • Letter: H
Question
Help, I am using the newest version of Visual studio and this code has multiple errors, I tried it as on the 17 3 example solution and that even errors out what am I missing?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Grades
{
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());
}
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 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
{
char TextBox;
//Create an Object of the SteamWriter
// class to write the data to the file
SteamWriter dat_file =
new SteamWriter(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
SteamReaderread_file =
new SteamReader(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
}
}
Explanation / Answer
In line number 76 I see the below statement
txtData.Text += read_rec + " ";
txtData.Text is actually textbox control for which you are adding read_rec+" "
I assume your aim is to add value of the txtdata.Text for which you have to take txtdata.Text.Value.
workaround for this is to assign it to a string variable and add then you can assign it to textbox, which is as below:
strData+= read_rec + " ";
txtData.Text.Value=strData;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.