none
"\r\n" not showing in textbox.text RRS feed

  • Question

  • Hello Team,

    I dont wont new line in textbox or something

    "\r\n" , Consider this as string and bind to textbox.text.

    I tried but I am not able to see any chars in the textbox.

    I need it in Winforms Textbox.

    Requirement : Just Show "\r\n" in textbox.text as string.

    Saturday, September 21, 2019 1:19 PM

Answers

  • My recommendation is to have a method or extension method to handle these requirements which can if needed be changed together.

    public static class Extensions
    {
        public static string ShowNewLine ( this TextBox sender) => 
            Regex.Replace(sender.Text, @"\n", "\\n");
    
        public static string RemoveNewLine(this TextBox sender) => 
            sender.Text.Replace("\\n", "").Trim();
    
        public static string ShowNewLineAndCarriage(this TextBox sender) => 
            Regex.Replace(sender.Text, @"\r\n?|\n", "\\r\\n");
    }

    Even more flexible

    public static class Extensions
    {
        public static string ShowNewLine ( this string sender) => 
            Regex.Replace(sender, @"\n", "\\n");
    
        public static string RemoveNewLine(this string sender) => 
            sender.Replace("\\n", "").Trim();
    
        public static string ShowNewLineAndCarriage(this string sender) => 
            Regex.Replace(sender, @"\r\n?|\n", "\\r\\n");
    }
    Yes you could have a all encompassing method to call or even inherit a TextBox which has properties and methods to handle these requirements.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by ID GO Saturday, September 21, 2019 4:09 PM
    Saturday, September 21, 2019 3:24 PM
    Moderator

All replies

  • Hello,

    Here is a solution.

    using System;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    
    namespace DemoMe
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = "\r\n";
                textBox1.Text = Regex.Replace(textBox1.Text, @"\r\n?|\n", "\\r\\n");
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, September 21, 2019 1:43 PM
    Moderator
  • Hey Karen,

    Thanks for the quick response,

    I need a Generic one which can accommodate to textbox.text

    1. Eg : "\r\n"

    2. "\n"

    3. "\n\t"

    4. <br>

    Above are the few Examples, there are still plenty, 

    My Requirement a Generic which can convert to string and show in textbox as it is.....

    Saturday, September 21, 2019 2:28 PM
  • Hello,

    Here is a solution.

    using System;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    
    namespace DemoMe
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = "\r\n";
                textBox1.Text = Regex.Replace(textBox1.Text, @"\r\n?|\n", "\\r\\n");
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hey Karen,

    Thanks for the quick response,

    I need a Generic one which can accommodate to textbox.text

    1. Eg : "\r\n"

    2. "\n"

    3. "\n\t"

    4. <br>

    Above are the few Examples, there are still plenty, 

    My Requirement a Generic which can convert to string and show in textbox as it is.....


    Saturday, September 21, 2019 2:29 PM
  • My recommendation is to have a method or extension method to handle these requirements which can if needed be changed together.

    public static class Extensions
    {
        public static string ShowNewLine ( this TextBox sender) => 
            Regex.Replace(sender.Text, @"\n", "\\n");
    
        public static string RemoveNewLine(this TextBox sender) => 
            sender.Text.Replace("\\n", "").Trim();
    
        public static string ShowNewLineAndCarriage(this TextBox sender) => 
            Regex.Replace(sender.Text, @"\r\n?|\n", "\\r\\n");
    }

    Even more flexible

    public static class Extensions
    {
        public static string ShowNewLine ( this string sender) => 
            Regex.Replace(sender, @"\n", "\\n");
    
        public static string RemoveNewLine(this string sender) => 
            sender.Replace("\\n", "").Trim();
    
        public static string ShowNewLineAndCarriage(this string sender) => 
            Regex.Replace(sender, @"\r\n?|\n", "\\r\\n");
    }
    Yes you could have a all encompassing method to call or even inherit a TextBox which has properties and methods to handle these requirements.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by ID GO Saturday, September 21, 2019 4:09 PM
    Saturday, September 21, 2019 3:24 PM
    Moderator
  •     private void Button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = "\r\n";
                // textBox1.Text = Regex.Replace(textBox1.Text, @"\r\n?|\n", "\\r\\n");
                var dd = ToLiteral(textBox1.Text);
                textBox1.Text = dd.Replace('"', ' ').Trim();
            }
    
            private static string ToLiteral(string input)
            {
                using (var writer = new StringWriter())
                {
                    using (var provider = CodeDomProvider.CreateProvider("CSharp"))
                    {
                        provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
                        return writer.ToString();
                    }
                }
            }

    Found this....Its working, Thanks Karen


    • Edited by ID GO Sunday, September 22, 2019 2:52 AM
    Saturday, September 21, 2019 4:09 PM