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.

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.IO;


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)
        {
            {

                TextReader tr = new StreamReader("C:\\Users\\Bob\\Documents\\Uni Work year 3\\Software\\ASDB2010\\09072501.hrm");
                int NumberOfLines = 15;
                string[] ListLines = new string[NumberOfLines];
                for (int i = 1; i < NumberOfLines; i++)
                {
                    ListLines[i] = tr.ReadLine();
                }
                Console.WriteLine(ListLines[5]);
                Console.WriteLine(ListLines[1]);



            }

        }
    }
}

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

Hi there you could look into putting the text in as a String and calling a substring using start and end values.

similiar to what I am trying to say -->
http://dotnetperls.com/substring

Jamesonh20'

commented: good site tip! +6

Hey thanks. Is there a way of doing it without having to type out the text that you want displayed?

Honestly, I've never tried that route. I'm not sure if you may be able to import the file or use a path. I only have a general understanding of the concept. Check the MSDN lib. Let me know if you get it to work, Im interested in what the code would look like!

Jamesonh20'

I'm not quite sure what your specific requirements are based on the description. If, for example, you are allowing your users to specify line numbers for the start and end of the block, you could use LINQ and extension methods to accomplish the goal. This is a sample.

First, my source file, alphabet.txt

A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEFGH
ABCDEFGHI
ABCDEFGHIJ
ABCDEFGHIJK
ABCDEFGHIJKL
ABCDEFGHIJKLM
ABCDEFGHIJKLMN
ABCDEFGHIJKLMNO
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNOPQ
ABCDEFGHIJKLMNOPQR
ABCDEFGHIJKLMNOPQRS
ABCDEFGHIJKLMNOPQRST
ABCDEFGHIJKLMNOPQRSTU
ABCDEFGHIJKLMNOPQRSTUV
ABCDEFGHIJKLMNOPQRSTUVW
ABCDEFGHIJKLMNOPQRSTUVWX
ABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ

And the code to display specific lines (can be user input, this is hardcoded)

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        string[] fileContents = File.ReadAllLines(@"C:\Temp\alphabet.txt");

        // assume this is user input and not hardcoded
        int startingLine = 7;
        int endingLine = 12;

        var query = fileContents.Skip(startingLine - 1).Take(endingLine - (startingLine - 1));

        foreach (string line in query)
            Console.WriteLine(line);

        Console.Read();
    }
}

And the output

ABCDEFG
ABCDEFGH
ABCDEFGHI
ABCDEFGHIJ
ABCDEFGHIJK
ABCDEFGHIJKL
commented: nice! +6

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 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]

[ExtraData]

[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 0 70
0 1470

[Summary-TH]
1470 0 1470 0 0 0
180 0 0 70
0 1470

What 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

Ah. In that case, you'd probably want to find all the regions of the document (as indicated by the square brackets) and allow the user to select one. You could then find that region and read until you find the next "[" character (or the end of file, whichever came first) and display that to screen. You could accomplish that with the Substring method mentioned above if the entire file was in a single string. Sample:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string fileContents = File.ReadAllText(@"C:\Temp\alphabet.txt");

        string findRegion = "[REGION B]";
        int startingPoint = fileContents.IndexOf(findRegion) + findRegion.Length;
        int endingPoint = fileContents.IndexOf("[", startingPoint) - 1;
        string displayBlock = string.Empty;

        if (endingPoint > -1)
            displayBlock = fileContents.Substring(startingPoint, endingPoint - startingPoint);
        else
            displayBlock = fileContents.Substring(startingPoint);


        Console.WriteLine(displayBlock);

        Console.Read();
    }
}

Source file

[REGION A]
A
AB
ABC
ABCD

[REGION B]
ABCDE
ABCDEF
ABCDEFG
ABCDEFGH
ABCDEFGHI
ABCDEFGHIJ
ABCDEFGHIJK

[REGION C]
ABCDEFGHIJKL
ABCDEFGHIJKLM
ABCDEFGHIJKLMN
ABCDEFGHIJKLMNO
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNOPQ

[REGION D]
ABCDEFGHIJKLMNOPQR
ABCDEFGHIJKLMNOPQRS
ABCDEFGHIJKLMNOPQRST
ABCDEFGHIJKLMNOPQRSTU
ABCDEFGHIJKLMNOPQRSTUV
ABCDEFGHIJKLMNOPQRSTUVW
ABCDEFGHIJKLMNOPQRSTUVWX
ABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ

You could also use modify the previous code sample I provided, and do the read all lines but find the specific line that was the region you were looking for, then find the next line that indicated a region, and use LINQ to extract everything between those lines. You've got options.

Thats excellent. Just one more thing. Can just put that code behind a button tool as this is where i seem to be running into problems. No matter what I try it makes no difference and im beginng to think that it may be because im putting the code in the wrong place.

Wow, ncie code apegram :)

deathtoall141, are you copying apegrams code exactly? The code he gave you is a Console Application intended as an example. You will need to make a few changes to run it on a button click.
Firstly, ensure you have correctly created your buttonclick event handler. Easiest way is to double click the button in the designer, this will autogenerate the eventhandler and the code block in which to place your code.

Next, you will need to adjust the output method. apegram has used Console.WriteLine to display results, but Forms dont use the Console for output. You will need to add a textbox to your form and set its Text property to the output. You will probably want to populate the findRegion variable according to what section you are trying to display.

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.