Fill RichTextBox with Get-Content

Welcome Forums General PowerShell Q&A Fill RichTextBox with Get-Content

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
5 years, 7 months ago.

  • Author
    Posts
  • #7147

    Participant
    Points: 0
    Rank: Member

    (OK, I managed to add content to my thread... the site doesn't seem to function in FF 21.0... IE works)

    I'm creating a GUI application, an offline UBB text editor, with Powershell. I'm new to GUI development and struggling with getting my content into a RichTextBox.

    What I do right now is:

    $smnuOpen_Click = {$dlgOpen.ShowDialog() ; $txtEditor.Text = (Get-Content $dlgOpen.FileName)}

    The content is indeed put into my RichTextbox, but returns have been lost.

    If my input is

    Foo
    Bar

    The RichTextBox will show:

    FooBar

    Tabs are not a problem on the other hand. Anyone knows a solution for this? Keeping the formatting is quite essential to my application 🙂

  • #7153

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    We don't support the "code" bb tags. You're meant to use the <> button in the toolbar to paste code.

    Get-Content doesn't read your text as a single string. It parses the text, and essentially splits the file on CRLF. Each line in the file thus becomes a System.String object. When you set the RTB it's probably calling ToString() on those, which won't produce CRLFs.

    Try doing something like gc filename.txt | Out-String to force the shell to produce a single string for you.

  • #7154

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    (oh, and I use the site almost exclusively in FF 21.0, FWIW).

  • #7155

    Participant
    Points: 0
    Rank: Member

    Hi Don. I feel like God is talking to me right now.
    I noticed about the [code]-tags. The crayon-method doesn't seem to like me either though ^^

    Most importantly: your suggestion was spot on. It works like a charm. Thank you very, very much!

  • #7184

    Participant
    Points: 0
    Rank: Member

    In some cases you might need to include some `n`r in your strings to force a CRLF.
    Some times I find I need both and some times either of them is enough on its own.

    However, where you can use it, Don's method is preferable as it will respect the CRLFs that are already in the source 😉

The topic ‘Fill RichTextBox with Get-Content’ is closed to new replies.