views:

171

answers:

2

Hi 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.

+1  A: 

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
        //
    }
}
Matthew Whited
I was assuming by block you meant part of a file. If you just want the entire file you could either just use read to end or ignore the skip/take above. You could then set the line array property of your text box to the lines variable from above.
Matthew Whited
I added a version with filtering and parsing. This should be pretty easy to extend to do whatever you want/need.
Matthew Whited
+2  A: 

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.

David Parvin
Hey the program is meant to read a heart rate monitor for bikers. So there is alot of information under different headings i.e.[IntTimes]00:24:30.5 140 83 154 1740 0 0 41 112 330 0 0 0 00 12080 0 280 0 00 0 0 0 0 0[IntNotes][ExtraData][Summary-123]1470 0 1470 0 0 0180 0 0 701470 0 1470 0 0 0180 0 0 700 0 0 0 0 0180 0 0 700 1470[Summary-TH]1470 0 1470 0 0 0180What I want to be able to do is have a seperate button for each block of information that the user can then have access to. So originally the complete file opens and then the user can view the desired information
Ok, so it sounds a like what you want is something that will parse your string and depending on the button you press, you change the text in the text box. I would store the information read from the text file in a memory variable and as I click on each button, I would clear the text box and parse the text, or just remember the file name and reload the information from file each time just adding in the information I want based on the button pressed. Helping you code it will take you helping us identify the parsing rules. :)
David Parvin
Hey the program is meant to read a heart rate monitor for bikers. So there is alot of information under different headings i.e. [code][IntTimes] 00:24:30.5 140 83 154 174 0 0 0 41 112 33 0 0 0 0 0 0 12080 0 280 0 0 0 0 0 0 0 0 [IntNotes] [Summary-123] 1470 0 1470 0 0 0 180 0 0 70 1470 0 1470 0 0 0 180 0 0 70 0 0 0 0 0 0 180 0 070 0 1470[/code] Sorry about the mess that came out in. Above is the way that the data actually appears. What I would like is have button1 and when clicked the block which starts with [inttimes] is produced and button2 is [Summary-123] and so on.