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:
Test
123
Test
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.
By Cornel on Jun 5, 2008 | Reply
textBox1.Text = String.Format(”First Line{0}Second Line{0}Third Line{0}”, Environment.NewLine);
looks better
By Amit on Jun 5, 2008 | Reply
As long as you are using Environment.NewLine I am happy
But you are correct, it looks better your way
Thank.
By Tomer on Jun 5, 2008 | Reply
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.
By Shahar Y on Jun 5, 2008 | Reply
Hi Tomer,
You are very wrong about the localization issue, just read about it:
http://people.merea.se/david/2006/12/
http://www.codeproject.com/KB/macros/DTReSharperPlugin.aspx (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…
By -md- on Jun 5, 2008 | Reply
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.