Last week I was in the need to use a WPF TextBox that had multi line capabilities. I started Typing Multi and hit CTRL + Space to get the intelisense and I was shocked! No MultiLine TextBox, I did get the following:

   1: <MultiBinding></MultiBinding>
   2: <MultiBindingExpression></MultiBindingExpression>
   3: <MultiDataTrigger></MultiDataTrigger>
   4: <MultiTrigger></MultiTrigger>

But no MultiLine TextBox. I refused to believe so I headed on to the Visual Studio Toolbox and after a few minutes of search I finally gave up and opened up Google to search for a MultiLine TextBox. As it turns out it does exists and all you have to do is use a regular WPF TextBox and set the AcceptsReturn Property to True.

All of this is nice but it is not why I am writing this lets assume you need to write the following data into the TextBox:


I bet what most of you would write (and me too up until last week) will be this:

   1: TextBox1.AppendText("Test");
   2: TextBox1.AppendText("\n");
   3: TextBox1.AppendText("123");
   4: TextBox1.AppendText("\n");
   5: TextBox1.AppendText("Test");

This will work but if you are writing an application that will need to be localized or operate in different environments you will suffer dearly for your decision to use “\n” as you new line “maker”.

Always use Environment.NewLine when in need to signal a new line.

Tags :

10 Responses to “The Use of Environment.NewLine In a WPF MultiLine TextBox”

  1. Cornel

    Said on June 5, 2008 :

    textBox1.Text = String.Format(“First Line{0}Second Line{0}Third Line{0}”, Environment.NewLine);

    looks better :D

  2. Amit

    Said on June 5, 2008 :

    As long as you are using Environment.NewLine I am happy :)

    But you are correct, it looks better your way

  3. Tomer

    Said on June 5, 2008 :

    Enviroment.NewLine has nothing to do with localization. It’s about being portable to different operating systems (Such as Unix).

    However, because WPF is no where close to being portable to other operating systems, I wouldn’t worry about it.

  4. Shahar Y

    Said on June 5, 2008 :

    Hi Tomer,

    You are very wrong about the localization issue, just read about it: (ReSharper plugin for localization which uses System.Environment.NewLine instead of “\r\n”)
    or in another 2,570 google search results for “localization environment.newline”.

    Regarding WPF portability to Unix:
    1. The Mono team are working like crazy and Silverlight 1.0 for unix is out. Work on 2.0 is in progress. Check the Moonlight project.
    2. WPF – check the Mono-Olive project, the days are numbered before we can run WPF applications on Unix, so we do “worry” about it…

  5. -md-

    Said on June 5, 2008 :

    Don’t want to quibble but there is no project started yet to create open-source implementation of WPF (mono-olive is more about Linq and WCF). And even if the decision is made to implement WPF for mono, it will take many years before you can use it for something reasonable. Look how much time it took to create Windows Forms implementation and how much time it takes to create Silverlight implementation. Both of these are tiny projects in comparison with WPF.

  6. bitpusher

    Said on November 9, 2008 :

    do don’t quibble. Your logic is faulty. Arguing against best practices for the sake of saving a few keystrokes is the sign of a consumate amatuer.

4 Trackback(s)

  1. Jun 6, 2008: Dew Drop – June 6, 2008 | Alvin Ashcraft's Morning Dew
  2. Jun 9, 2008: Wöchentliche Rundablage: Silverlight 2, WPF, ASP.NET MVC, jQuery… | Code-Inside Blog
  3. Jun 9, 2008: Weekly Links: Silverlight 2, WPF, ASP.NET MVC, jQuery… | Code-Inside Blog International
  4. Jul 29, 2008: The Use of Environment.NewLine In a WPF MultiLine TextBox | TROMPYX

Post a Comment