// Programmer: Janet Joy // Reads a file of info frequently needed to fill in forms. // The file if read in and displayed in a list box. // When is an item in the list box is selected it is copied to the clipboard.using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace clipboard { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void readfile() { // Let user see items unsorted initially. lstFile.Sorted = false; // Set the filter to show text files or all files. openFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"; openFileDialog1.ShowDialog(); this.Text = openFileDialog1.FileName; // Read the entire file into an array. string[] lines = System.IO.File.ReadAllLines(openFileDialog1.FileName); // Add each line to the list box. foreach (string line in lines) { lstFile.Items.Add(line); } } private void mnuOpen_Click(object sender, System.EventArgs e) { // Reads the file into list box in addition to what is already there. readfile(); } private void mnuNew_Click(object sender, System.EventArgs e) { // Clear the list box before reading the file. lstFile.Items.Clear(); readfile(); } private void newToolStripButton_Click(object sender, System.EventArgs e) { // Keep all the code in one place. Call the menu event. mnuNew_Click(sender, e); } private void openToolStripButton_Click(object sender, System.EventArgs e) { // Keep all the code in one place. Call the menu event. mnuOpen_Click(sender, e); } private void mnuExit_Click(object sender, System.EventArgs e) { // End the program by closing the form. Close(); } private void mnuSort_Click(object sender, System.EventArgs e) { // Sort the items in listbox. lstFile.Sorted = true; } private void Form1_Resize(object sender, System.EventArgs e) { // Make the listbox fill the form. lstFile.Width = this.Width; lstFile.Height = this.Height - lstFile.Location.Y; } private void lstFile_SelectedIndexChanged(object sender, System.EventArgs e) { // Copy the selected item to the clipboard. Clipboard.SetText(lstFile.Text); } private void toolStripSort_Click(object sender, System.EventArgs e) { // Keep all the code in one place. Call the menu event. mnuSort_Click(sender, e); } } }