Fill RichTextBox with Get-Content

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Tom Kemp Tom Kemp 3 years, 10 months ago.

  • Author
    Posts
  • #7147
    Profile photo of YellowOnline
    YellowOnline
    Participant

    (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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #7155
    Profile photo of YellowOnline
    YellowOnline
    Participant

    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
    Profile photo of Tom Kemp
    Tom Kemp
    Participant

    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 😉

You must be logged in to reply to this topic.