0% found this document useful (0 votes)
87 views2 pages

Lab 2 Event Driven

This document contains code for a Windows Forms application that allows users to open, read, and display text files. It includes code to open a file dialog to select a text file, read the file line by line using a StreamReader, display each line in a list view control, and handle exceptions if the file is not found.

Uploaded by

cheska santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views2 pages

Lab 2 Event Driven

This document contains code for a Windows Forms application that allows users to open, read, and display text files. It includes code to open a file dialog to select a text file, read the file line by line using a StreamReader, display each line in a list view control, and handle exceptions if the file is not found.

Uploaded by

cheska santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 WindowsFormsApp5
{
public partial class FrmOpenTextFile : Form
{
public FrmOpenTextFile()
{
InitializeComponent();
}

private void btnOpen_Click(object sender, EventArgs e)


{

{
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Browse Text File";
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = "Text files (*.txt) |*.txt| All Files
(*.*)|*.*";
openFileDialog1.ShowDialog();

String path = openFileDialog1.FileName;

using (StreamReader streamReader = File.OpenText(path))


{
String _getText = "";
while ((_getText = streamReader.ReadLine()) != null)
{
Console.WriteLine(_getText);
lvShowText.Items.Add(_getText);
}
}
}
}
}
}

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 WindowsFormsApp4
{
public partial class FrmStudentRecord : Form
{
public FrmStudentRecord()
{
InitializeComponent();
}

private void btnRegister_Click(object sender, EventArgs e)


{
FrmRegistration register = new FrmRegistration();
register.Show();
Visible = false;
}

private void btnFind_Click(object sender, EventArgs e)


{
try
{
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Browse Text File";
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = "Text files (*.txt) |*.txt| All Files
(*.*)|*.*";
openFileDialog1.ShowDialog();

String docPath = openFileDialog1.FileName;


StreamReader reader = new StreamReader(docPath);
listView1.Text = reader.ReadToEnd();
reader.Close();
}
catch (System.IO.FileNotFoundException rm)
{
MessageBox.Show(rm.Message, "File does not found",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void btnUpload_Click(object sender, EventArgs e)


{
listView1.Text = "";
MessageBox.Show("Sucessfully Uploaded!!", "Message");
}
}
}

You might also like