I'm new to C#. I'm trying to write a winforms application that reads a text file and prints the specific lines according to the conditions. For example, I have a text file look like this:
07/10 08:15:00 EVENT: IN
07/10 08:15:00 BUILDING: A
07/10 08:15:00 ROOM: 101
07/10 08:15:00 MEMBER: JOHN, MARIA, PETER, SOPHIA
07/10 08:15:00 --------------------------------------
07/11 17:15:00 EVENT: OUT
07/11 17:15:00 BUILDING: B
07/11 17:15:00 ROOM: 102
07/11 17:15:00 MEMBER: ANDY, HAN, CINDY, LESTER
07/11 17:15:00 --------------------------------------
07/12 09:30:00 EVENT: IN
07/12 09:30:00 BUILDING: A
07/12 09:30:00 ROOM: 101
07/12 09:30:00 MEMBER: JIMMY, NICKY, EMILY, DANIEL
07/12 09:30:00 --------------------------------------
07/13 08:46:00 EVENT: IN
07/13 08:46:00 BUILDING: B
07/13 08:46:00 ROOM: 202
07/13 08:46:00 MEMBER: ROMEO, JULIET, WILLIAM
07/13 08:46:00 --------------------------------------
07/13 15:29:00 EVENT: IN
07/13 15:29:00 BUILDING: A
07/13 15:29:00 ROOM: 101
07/13 15:29:00 MEMBER: THOMAS, VIVIAN, KIM, SAMUEL
07/13 15:29:00 --------------------------------------
07/14 20:33:00 EVENT: OUT
07/14 20:33:00 BUILDING: A
07/14 20:33:00 ROOM: 301
07/14 20:33:00 MEMBER: JIMMY, NICKY, DANIEL
07/14 20:33:00 --------------------------------------
I want to print the event name and all other lines belong to this event when it match all 3 conditions as below:
- Condition1: Event: IN
- Condition2: Building: A
- Condition3: Room: 101
Result should be like this:
07/10 08:15:00 EVENT: IN
07/10 08:15:00 BUILDING: A
07/10 08:15:00 ROOM: 101
07/10 08:15:00 MEMBER: JOHN, MARIA, PETER, SOPHIA
07/10 08:15:00 --------------------------------------
07/12 09:30:00 EVENT: IN
07/12 09:30:00 BUILDING: A
07/12 09:30:00 ROOM: 101
07/12 09:30:00 MEMBER: JIMMY, NICKY, EMILY, DANIEL
07/12 09:30:00 --------------------------------------
07/15 10:11:00 EVENT: IN
07/15 10:11:00 BUILDING: A
07/15 10:11:00 ROOM: 101
07/15 10:11:00 MEMBER: THOMAS, VIVIAN, KIM, SAMUEL
07/15 10:11:00 --------------------------------------
Here is my code:
private void btnSearch_Click(object sender, EventArgs e)
{
txtResult.Clear();
int counter = 0;
string line;
if (txtPath.Text != "")
{
StreamReader reader = new StreamReader(txtPath.Text);
while ((line = reader.ReadLine()) != null)
{
if (line.Contains("EVENT: IN"))
{
//problem at this line
txtResult.Text += line + "\r\n";
if ((line = reader.ReadLine()) != null)
{
if (line.Contains("BUILDING: A"))
{
txtResult.Text += line + "\r\n";
if ((line = reader.ReadLine()) != null)
{
if (line.Contains("ROOM: 101"))
{
txtResult.Text += line + "\r\n";
if ((line = reader.ReadLine()) != null)
txtResult.Text += line + "\r\n";
if ((line = reader.ReadLine()) != null)
txtResult.Text += line + "\r\n";
}
}
}
}
}
counter++;
}
reader.Close();
}
else
MessageBox.Show("Find not found!", "Message", MessageBoxButtons.OK);
}
The problem is when it reach the first condition, it prints all the lines that contain the word 'EVENT: IN', like this:
07/10 08:15:00 EVENT: IN
07/10 08:15:00 BUILDING: A
07/10 08:15:00 ROOM: 101
07/10 08:15:00 MEMBER: JOHN, MARIA, PETER, SOPHIA
07/10 08:15:00 --------------------------------------
07/12 09:30:00 EVENT: IN
07/12 09:30:00 BUILDING: A
07/12 09:30:00 ROOM: 101
07/12 09:30:00 MEMBER: JIMMY, NICKY, EMILY, DANIEL
07/12 09:30:00 --------------------------------------
07/13 08:46:00 EVENT: IN
07/13 15:29:00 EVENT: IN
07/15 10:11:00 EVENT: IN
07/15 10:11:00 BUILDING: A
07/15 10:11:00 ROOM: 101
07/15 10:11:00 MEMBER: DONALD, GARETH, BOBBY
07/15 10:11:00 --------------------------------------
My question is how to print only lines that match all 3 conditions?
Thank for help!