zoukankan      html  css  js  c++  java
  • C# TextBox换行原理

      要让一个Windows Form的C# TextBox换行显示多行文本就得把它的Multiline属性设置为true。

         你往往会想到直接添加一个含有换行符" "的字符串给Text属性:

        aTextBox.Text = "First Line Second Line Third Line";    

         可是实际运行的时候你却发现它始终不会换行,显示的结果为"First LineSecond LineThirdLine"。

         其实主要是因为C# TextBox运行在Windows上。Windows能够显示的换行必须由两个字符组成:carriage return & line feed,也就是必须是" "。如果只是" "在Windows中不能显示为换行的,这与Linux/Unix等其他的操作系统不一样。所以上边如果把" "替换成" "就可以了。

         其实问题仍然没有很好的解决,因为用" "能够满足Windows的要求了,但是如果是其他平台怎么办?为了要确保让换行效果在各种平台上都能够正常的显示,请用Environment.NewLine。它可以确保在不同的平台下都能够返回正确的换行字符,在Windows下是 ,在Linux(Mono)下就应该是 了。所以上面的代码应该写成: 

      TextBox.Text = "First Line" +      

      Environment.NewLine + "Second Line" +      

      Environment.NewLine + "Third Line";   

         另外你也可以用verbatim string literal(用@开始的字符串)的形式来输入换行符:

      aTextBox.Text = @"First Line      

      Second Line     

      Third Line";   

      这样就能实现换行功能了。

  • 相关阅读:
    Anders Hejlsberg 和 Erich Gamma
    node-webkit
    用户密码加密存储十问十答,一文说透密码安全存储
    Spring Security-- 验证码功能的实现
    BigDecimal 专题
    基于微信开发 专题
    unwrapThrowable
    Android PullToRefreshExpandableListView的点击事件
    Android The content of the adapter has changed but ListView did not receive a notification
    Android入门:广播发送者与广播接收者
  • 原文地址:https://www.cnblogs.com/cleverJoe/p/txtBox_Multi_EnterInArea.html
Copyright © 2011-2022 走看看