Hi there all

I have to write a small interface where I can read text files from a location and edit them. My aim is to have a small GUI, in that GUI there will be a combo box where I select the text file. when selected the info in that text file displays into necessary text boxes, where they can be edited and lastly a save button.

Here is my text file structure..

content=1
Mnr. Spangenberg
42 Sunbird Drive
082 222 2222
[email]johan@africansurveyors.com[/email]
Other Quick notes about person...

Any help...??

I have done some homework for you, and this is the code I came up with. There is still some work to be done with selecting folder, now you have to write paths, but this is not the thread of this thread. Mainly the app works like you wanted to (I hope).

On the form put these controls:
- textBox (for inseting path to the folders)
- comboBox (to select files - text files)
- richTextBox (where text is displayed)
- button (to save the changed file)

optional:
- labels over controls to specify what they are/do

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.Collections.ObjectModel;

namespace Nov26EditFiles
{
    public partial class Form1 : Form
    {
        string folderPath;
        string fileName;
        int textLenght;
        bool bSaved;
        bool bJumpCode;

        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void PopulatingComboBox()
        {
            try
            {
                this.comboBox1.Items.Clear();
                this.richTextBox1.Text = null;
                string[] allFilesPath = System.IO.Directory.GetFiles(@"" + folderPath, "*.txt");//, System.IO.SearchOption.AllDirectories);
                foreach (string filePath in allFilesPath)
                {
                    string file = System.IO.Path.GetFileName(filePath);
                    this.comboBox1.Items.Add(file);
                }
                int filesCount = allFilesPath.Length;
                MessageBox.Show(filesCount + " files found in:\n" + folderPath, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("The written path is not valid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!bJumpCode)
            {
                try
                {
                    string fileName2 = this.comboBox1.SelectedItem.ToString();
                    int textLenghtNew = this.richTextBox1.Text.Length;

                    if (!bSaved && textLenghtNew != textLenght)
                    {
                        if (DialogResult.Yes == MessageBox.Show("File " + fileName + " has been changed and not saved!\n" +
                            "Do you want to save it before selecting other file?", "File not saved", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                        {
                            bJumpCode = true;
                            this.comboBox1.SelectedItem = fileName;
                            this.button1.Focus();
                            return;
                        }
                    }
                    else
                        fileName = this.comboBox1.SelectedItem.ToString();

                    using (System.IO.StreamReader sr = new System.IO.StreamReader(folderPath + "\\" + fileName2))
                    {
                        string text = sr.ReadToEnd();
                        this.richTextBox1.Text = text;
                        textLenght = this.richTextBox1.Text.Length;
                        bSaved = false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
                bJumpCode = false;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                folderPath = textBox1.Text.Trim();
                PopulatingComboBox();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {               
                fileName = this.comboBox1.SelectedItem.ToString();
                if (DialogResult.No == MessageBox.Show("Do you want to save file " + fileName + "?", 
                    "Saving file", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    return;
                using (System.IO.FileStream fs = new System.IO.FileStream(folderPath + "\\" + fileName, System.IO.FileMode.Create))
                {
                    using (System.IO.TextWriter tw = new System.IO.StreamWriter(fs))
                    {
                        foreach (string line in richTextBox1.Lines)
                        {
                            tw.WriteLine(line);
                        }
                    }
                    bSaved = true;
                    textLenght = this.richTextBox1.Text.Length;
                    MessageBox.Show("File " + fileName + " has been successfully changed and saved.", "Success saving", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Is there is anything else, let me know. I`ll be glad to help.

Thank you for the help :) I used your parts of your code and it worked!

I am glad it did :)
Just dont forget to mark the thread as salved.

bye, ybe
Mitja

Hi

This is sample code for to read text file .I think it will help you.

Read = oFile1.Open Text(-C:\sample.txt")

 While oRead1.Peek <> -1
       LineIn = oRead1.ReadLine()

 End While

 oRead1.Close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.