Add the following forms to the
Program 1 Project in your Assignment 5:1)
DeleteEmployee Form•
You will need a text box for the user to enter an Employee ID•
You will also need a Delete Employee button•
Clicking on the Delete Employee button should remove the employee datafrom the
employee.txt file. If the employee does not exist in theemployee.txt
file, display an appropriate message on the screen.2)
DisplayEmployee Form•
You will need a text box for the user to enter an Employee ID•
You will also need a Display Employee button•
Clicking on Display Employee button will search for the given employeein the
employee.txt file and display the employee data on the screen (usedifferent label for different data items). If the employee does not exist in
the
Sam HobbsPosted Jul 3, 2010, 11:24 PM
String str = sr.ReadLine();
while (str != null)
{
label1.Text = strFromFile;
}
You read the data into str but that is not the data you put into label1.Text. That's the main problem with that. The only other problem is that you replace label1.Text each time the while loop executes, but you do not read any more data. Roy's code solves all those problems. Also, instead of Roy's code:
while ((line = file.ReadLine()) != null)
{
label1.Text += line;
}
You could do all that using:
label1.Text = file.ReadToEnd();
nigel drakesPosted Jul 3, 2010, 7:55 PM
Thanks
Roy
Roy SPosted Jul 3, 2010, 7:44 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string line;
StreamReader file = new StreamReader("c:\\first.txt");
while ((line = file.ReadLine()) != null)
{
label1.Text += line;
}
file.Close();
}
}
}
nigel drakesPosted Jul 3, 2010, 7:35 PM
this is my code I tried what you said but there is some part I am missing it makes sense to me but I guess its wrong because it does not do nathing bu give me an error.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("C:\\first.txt");
String str = sr.ReadLine();
while (str != null)
{
label1.Text = strFromFile;
}
}
}
}
Roy SPosted Jul 3, 2010, 7:32 PM
txtBox.Text = strFromFile;
Or am I again thinking too simple again while your real problem is way more complicated?
nigel drakesPosted Jul 3, 2010, 7:23 PM
thanks
Roy SPosted Jul 3, 2010, 7:19 PM
http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx