0

I am writing a simple text editor and currently I am having trouble with the open function. I can successfully read in text from a file, however the trouble I am having is importing it into a text box. Also, I am aware there are questions like this that have already been asked, however I cannot get a working solution from any of them (trust me, I have been trying this for hours). So this is my current open function:

private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog openDialog = new OpenFileDialog();

        openDialog.Filter = ".TXT File|*.txt|.BAT File|*.bat|All Files|*.*";
        openDialog.Title = "Open file";

        openDialog.ShowDialog();

        if (openDialog.FileName != "")
        {
            path = openDialog.FileName;

            try
            {
                string[] lines = File.ReadAllLines(path);
                //all text from file is now stored in the array "lines"
                //put it into text box
                for (int i = 0; i < lines.Length; i++)
                {
                    mainTextEntry.AppendText(lines[i]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while opening file. Original error:\n\n" + ex);
            }
        }
    }

Allow me to point out a few things from that piece of code:

  • My text box is called mainTextEntry.
  • My openFileDialog is called openDialog.
  • Lines is an array, and contains the read in text.
  • You can ignore the catch at the bottom, that is just to prevent crashes.

Ok, so that code above gives the following output:

hellotherehow are you?

As you guessed, the file I am reading in has some line breaks in it:

hello
there
how are you?

Of course that is what I want to be printed in mainTextEntry.Text. How would I go about doing this?

EDIT: full source can be found here: MainWin.cs (if you're paranoid click here)

carefulnow1
  • 803
  • 12
  • 30
  • 1
    `mainTextEntry.Lines = File.ReadAllLines(path);` – Ňɏssa Pøngjǣrdenlarp Feb 20 '16 at 02:05
  • I'm assuming you have `multiline` set to true? – Gabe Feb 20 '16 at 02:17
  • No - that's good. I was just making sure otherwise `Environment.NewLine` is redundant. Strange issue. – Gabe Feb 20 '16 at 02:22
  • @Gabe, may it be something earlier in the code? I have gone through and cannot find anything, but if you want I can post the full "mainWin.cs" (156 lines). – carefulnow1 Feb 20 '16 at 02:24
  • Doubtful to be honest. Let me test something. Ill update my answer if it works – Gabe Feb 20 '16 at 02:26
  • In case any one else may find it useful, I have added a link to the source (it's on Google Drive but I don't think you need an account to read/download it). – carefulnow1 Feb 20 '16 at 02:31
  • 1
    @odixon try the code in my adjusted stuff. `StringBuilder` is overkill for this and probably redundant. But I wanted to be sure. – Gabe Feb 20 '16 at 02:39
  • @Gabe hmm... I am looking at this question here http://stackoverflow.com/questions/9498422/c-sharp-read-in-a-large-150mb-text-file-into-a-rich-text-box and it looks like a rich text box might be the better option here. – carefulnow1 Feb 20 '16 at 02:39
  • 1
    @odixon - doesn't really matter what option you choose to be honest. Unless you need RTB's functionality for formatting purposes, it's overkill. I honestly think that its your multiline property is set to false. – Gabe Feb 20 '16 at 02:40
  • @Gabe I am just going to use RTB, however thanks so much for the help. Also... http://imgur.com/7gviVkI – carefulnow1 Feb 20 '16 at 02:46
  • @odixon - I'm sure it's there like that in VS, but there might be a line of code somewhere that disables it when compiled. I'm just making sure by adding that one line of code. If it doesn't then I'm stumped. Give it a try anyway, I'm curious – Gabe Feb 20 '16 at 02:47
  • Ok, I'll have a look around what VS generates also. – carefulnow1 Feb 20 '16 at 02:48
  • @odixon - yeah just try adding `mainTextEntry.MultiLine = true;` just above and outside of your for loop. Then try. Tell me what happens\ – Gabe Feb 20 '16 at 02:49
  • @Gabe I have had that there for ages. I don't think it is a multi line issue... I think it is a compile issue. Also, this is what the designer generates: http://i.imgur.com/ywoNtJB.png?1 – carefulnow1 Feb 20 '16 at 02:50
  • @odixon - yep, i'm stumped. It should work. No clue why it isn't man. You could try deleting that textbox control and readding it to 'refresh' it, but if the issue persists after that.. No clue. Sorry bud. – Gabe Feb 20 '16 at 02:52
  • @odixon doubtful because it works as intended on my side. So it's isolated already to yours. – Gabe Feb 20 '16 at 02:54
  • Sorry I couldn't help, probably best to delete this post as it just serves to confuse people and it's not an actual question that can be reproduced – Gabe Feb 20 '16 at 02:56
  • Apparently it has answers so cannot be deleted... – carefulnow1 Feb 20 '16 at 02:59

4 Answers4

1

It's probably overkill but this works for me. But the other options should work. I would say, more than likely is that you're getting that result because somewhere in your code - especially if you created the textbox programmatically - that mainTextEntry.MultiLine is false, when it needs to be true. If I disable multiline in vs I get the same result you're receiving.

//...    
mainTextEntry.Multiline = true; //add this just to be sure
var sb = new StringBuilder();

for (int i = 0; i < lines.Length; i++)
{
    sb.AppendLine(lines[i]);
}

mainTextEntry.Text = sb.ToString();

// or
mainTextEntry.Multiline = true; //add this just to be sure
for (int i = 0; i < lines.Length; i++)
{
    mainTextEntry.AppendLine(lines[i] + Environment.NewLine);
}

Try appending Environment.NewLine to the end as you display it.

mainTextEntry.AppendText(lines[i] + Environment.NewLine);
Gabe
  • 570
  • 1
  • 4
  • 15
1

Use Environment.NewLine to break the content in presentation.

mainTextEntry.AppendText(String.Join(Environment.NewLine, File.ReadAllLines(path)));
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0

You're missing the new line. You're simply adding to the string. Here's a nifty way of doing it with linq and join

string formattedText = String.Join(Environment.NewLine, File.ReadAllLines(path));

It will take all your lines, format them with your line break, and give you a string all in one go :)

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • @odixon That's because the syntax is incorrect. Check Hari Prasad post for correct usage. – Gabe Feb 20 '16 at 02:15
-1

textbox has a property called 'Mutiline',you can try set this property 'true'.

jyj6536
  • 31
  • 1