简体   繁体   中英

Getting certain lines of text c#

I was wondering if someone could help me. I would like to able to be to have my program give the user the ability to only read a certain block of code from a text document. However I would like it to be placed behind a button so it can be turned on and off. I have experimented wih different ways of doing this but nothing has made the slightest bit of difference.

This is how my code stands at the moment.

namespace filestream
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string Read(string file)
        {
            StreamReader reader = new StreamReader(file);
            string data = reader.ReadToEnd();
            reader.Close();

            return data;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string data = Read(openFileDialog1.FileName);
                textBox1.Text = data;

            }
            else
            {
                //do nothing
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }
    }
}

Im quite new at this so any help would be greatly appreciated.

As far as I can see from what you have there, it should be working. The only thing I can think of why it is not is that your button is connected to the button1_Click routine instead of the btn1_Click routine. If when you click the button and it does not do anything, that is the only reason I can see. The code looks like it is written to ask for the user to select a file and then read the whole file in and place it in the text box.

If you can use .Net 3.5 and LINQ here is an option...

public static class Tools
{
    public static IEnumerable<string> ReadAsLines(this string filename)
    {
        using (var reader = new StreamReader(filename))
            while (!reader.EndOfStream)
                yield return reader.ReadLine();
    }
}
class Program
{
    static void Main(string[] args)
    {
        var lines = "myfile.txt".ReadAsLines()
                                // you could even add a filter query/formatting
                                .Skip(100).Take(10) //do paging here
                                .ToArray();
    }
}

... extended insanity to show filtering, parsing, and formatting ...

public static class Tools
{
    public static void Foreach<T>(this IEnumerable<T> input, Action<T> action)
    {
        foreach (var item in input)
            action(item);
    }
}
class Program
{
    static void Main(string[] args)
    {
        // the line below is standing in for your text file.  
        // this could be replaced by anything that returns IEnumerable<string>
        var data = new [] { "A 1 3", "B 2 5", "A 1 6", "G 2 7" };

        var format = "Alt: {1} BpM: {2} Type: {0}";

        var lines = from line in data
                    where line.StartsWith("A")
                    let parts = line.Split(' ')
                    let formatted = string.Format(format, parts)
                    select formatted;

        var page = lines.Skip(1).Take(2);

        page.Foreach(Console.WriteLine);
        // at this point the following will be written to the console
        //
        // Alt: 1 BpM: 6 Type: A
        //
    }
}

Have you tried taking the contents of btn1_click and putting it in button1_click ? Sometimes it works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM