using System; using System.IO; using System.Windows.Forms; namespace drop_location { public partial class Form1 : Form { // Add OpenFileDialog to the form class private OpenFileDialog openFile = new OpenFileDialog(); public Form1() { InitializeComponent(); // Optional: Configure the OpenFileDialog openFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; openFile.Title = "Select a File"; } // The GetFileName method gets a filename from the user private bool GetFileName(out string selectedFile) { if (openFile.ShowDialog() == DialogResult.OK) { selectedFile = openFile.FileName; return true; } else { selectedFile = ""; return false; } } // The LoadFileContents method loads file contents into the ListBox private void LoadFileContents(string filename) { try { string lineContent; // Clear the ListBox first listBox1.Items.Clear(); // Read the file using (StreamReader inputFile = File.OpenText(filename)) { while (!inputFile.EndOfStream) { lineContent = inputFile.ReadLine(); // Only add non-empty lines (optional) if (!string.IsNullOrWhiteSpace(lineContent)) { listBox1.Items.Add(lineContent); } } } // Optional: Show success message or update status if (listBox1.Items.Count > 0) { MessageBox.Show($"Successfully loaded {listBox1.Items.Count} items.", "File Loaded", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("The file is empty.", "No Data", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { MessageBox.Show($"Error loading file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { // Optional: Add functionality when an item is selected if (listBox1.SelectedItem != null) { // For example, display the selected item in a label or textbox // labelSelectedItem.Text = $"Selected: {listBox1.SelectedItem}"; } } private void button1_Click(object sender, EventArgs e) { // Load file button click string filename; if (GetFileName(out filename)) { LoadFileContents(filename); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void Form1_Load(object sender, EventArgs e) { // Optional: Initialize form properties listBox1.SelectionMode = SelectionMode.One; // Set button text for clarity button1.Text = "Load File"; button2.Text = "Exit"; // Optional: Set form title this.Text = "Drop Location - File Viewer"; } // Optional: Add method to clear the ListBox private void ClearListBox() { listBox1.Items.Clear(); } // Optional: Add method to save ListBox contents to a file private void SaveListBoxContents() { SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; saveFile.Title = "Save ListBox Contents"; if (saveFile.ShowDialog() == DialogResult.OK) { try { using (StreamWriter outputFile = new StreamWriter(saveFile.FileName)) { foreach (var item in listBox1.Items) { outputFile.WriteLine(item.ToString()); } } MessageBox.Show("File saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Error saving file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } // Optional: Add a third button for saving private void button3_Click(object sender, EventArgs e) { SaveListBoxContents(); } } }