Click here to Skip to main content
15,888,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day everyone, I drafted a code by which I search for values but only one value is ever written out, I think it should have been built with the help of stringbuilder, but i dont know how. Can someone help me please

What I have tried:

C#
using System;

using System.IO;

using System.Windows.Forms;

namespace mise2
{
    public partial class Form1 : Form
    {


        private String filePath_input = string.Empty;

        public Form1()
        {
            InitializeComponent();
            richTextBox1.AutoSize = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = @"C:\Users\";


            openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;


            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath_input = openFileDialog.FileName;
                textBox1.Text = filePath_input;
            }
        }


        private
            string FindLineAboveAsterisks(string path)
        {
            TextReader sr = File.OpenText(path);
                return FindLineAboveAsterisks(sr);
        }

        private string FindLineAboveAsterisks(TextReader reader)
        {
            
            string result = reader.ReadLine();
            string line = string.Empty;
            while (result is object && (line = reader.ReadLine()) is object)
            {
                int startIndex = 21;
                int length = 9;

                if (line.Contains("***"))
                {
                    return result;
                }
               
                {
                    result = line.Substring(startIndex, length);
                }
            }
            return string.Empty;
            
        }

         




        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = FindLineAboveAsterisks(filePath_input);
        }

}
}
Posted
Updated 5-Oct-20 2:31am
Comments
CHill60 5-Oct-20 8:18am    
Could you provide a clearer explanation of what this code is trying to do
dejf111 5-Oct-20 8:28am    
This code should be found in the text document that I select in the dialog box then find the line that sits above the line indicating "***"further, it should crop this line to the chosen parameters and list all matching ones in the textbox
dejf111 5-Oct-20 8:29am    
all is well except it lists just one found option

1 solution

Your code will only ever find the first line in the file. You should create a List<T> of the text lines as you read through the file, and return the list to the calling code. The caller can then display the resulting details as required.
 
Share this answer
 
Comments
CPallini 5-Oct-20 8:59am    
5.
Richard MacCutchan 5-Oct-20 9:33am    
Thanks. I could have written the actual code, but too many people already think development is "copy and paste".
dejf111 6-Oct-20 0:30am    
thank you I'll try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900