Read/Modify/Add Lines  
Author Message
StanleyE





PostPosted: Visual C# General, Read/Modify/Add Lines Top

Hello all,

I am creating a windows application program that reads a text file in which my console program creates “It takes a person name, gender, weight, and height.” What I want to do in the windows application is read that file, and ask the user to input a test score for that person and then add it to that person category in the text and save it.

For example

Console Version of Text File

Name: Steve

Gender: M

Height: 67

Weight: 176

Windows Version after User Input

Name: Steve

Gender: M

Height: 67

Weight: 176

Grade: 88

One of the many problems I’m facing is I have more that one applicant, so I need it to go under the weight of each person in that file. In addition, I need it to go asking for every grade of the name in the text. I’m thinking of putting each category (name, gender, weight, height, and grade) into there own array because at the end I will also need to do an average of how many males/females participated average weight, height and so forth.

If any one has any suggestions, methods or sample code for making this application easier it would be greatly appreciated. Thanks




Visual C#16  
 
 
James Curran





PostPosted: Visual C# General, Read/Modify/Add Lines Top

I’m thinking of putting each category (name, gender, weight, height, and grade) into there own array because at the end I will also need to do an average of how many males/females participated average weight, height and so forth.

No. You want to define a class Person, which has Name, Gender, Height, Weight & Grade properties. In the Form_Load method, create a List of Person objects, filling most of the properties. Create a second form, which takes a Person object in it's constructor, displays the information, and requests the grade (and updates the Person object).



 
 
StanleyE





PostPosted: Visual C# General, Read/Modify/Add Lines Top

I understand what you mean by creating a class named Person which has properties of Name, Gender, Height, Weight, and Grade. What I do not quite understand from what you said is the Form_Load method, and creating a list of person objects, filling most of the properties. In terms of displaying the applicant’s information I was able to do so using streamreader here is exactly what I have for a button that reads and displays the user information.

private void BTN_Read (object sender, System.EventArgs e)
{
//read text file
StreamReader R_File = new StreamReader(C:\text.txt);
//display the saved information from the txt file in a rich text box
this.RCH_TXT_BX.Text = Read_File.ReadToEnd();
Read_File.Close();
//read text file again
StreamReader R2_File= new StreamReader(C:\text.txt);
this.LBL_TXT_DISPLAY.Text = "Enter Exam Score For";
// displays first line on text "which is name"
this.LBL_Name.Text = ME.ReadLine();
R2_File.Close
}

now I created another button call Enter, in which after the user enters
a test score it would either be a grade accepted or rejected

private void BTN_ENTER_Click(object sender, System.EventArgs e)
{
if (Convert.ToInt32(TXT_BX_SCORE.Text) >= 79)
{
this.LBL_RESULT.Text = "GRADE ACCEPTED";
StreamWriter Write_File = new StreamWriter(C:\text.txt);
Write_File.Write_File.WriteLine("GRADE ACCEPTED");
Write_File.Close();
StreamReader R_File = new StreamReader(C:\text.txt);
this.RCH_TXT_BX.Text = Read_File.ReadToEnd();
}
else
this.LBL_RESULT.Text = "GRADE REJECTED";


now the problem with this is that it displays grade accepted in the bottom of the text file rather than under the weight of that person category and repeats over and over if the button is pressed, and the this.LBL_Name.Text does not go to the next name.

If you or anyone can provide help it would help "i know my codes is rather sloppy, still rather new to C#"



 
 
James Curran





PostPosted: Visual C# General, Read/Modify/Add Lines Top

OK, the bit I was missing was that I assumed that the text file had the names of several people in it. So, I was saying, create an array of Person objects, one for each person in the file (this I was say could be done in the Form_Load but could also be done on the BTN_Read). Then I proposed having a second dialog which took one of those Person object, which displayed the data neatly in separate label controls instead of a blob in a RichText box). It would have an editbox and an Enter button. The Enter_Click event would update the Person object from the editbox, and close the secordary form, returning control to the main form, which would then redisplay the second dialog, this time with the next Person object. When each Person object has been updated, the main form would then write out the file.