I can't show a long string without it breaking a line on the screen

zequion 446 Reputation points
2024-01-18T09:02:28.09+00:00

In Winforms I use a RichTextBox control to display a long Base64 encoded string without line breaks but display the string with line breaks. Text = System.Convert.ToBase64String(Text, 0, 0, System.Base64FormattingOptions.None); Text = Text.Replace("\n", null).Replace("\r", null);

I have already tried with Rtb.WordWrap and Rtb.RightMargin but it does the same thing.

You can see that in the sample text that, depending on the character to be displayed, is when it breaks a line. If you delete that character, the line goes up. I'm seeing that it always breaks a line when it encounters a + sign or a /, which are allowed characters in Base64. So it is not a system to safely display text on the screen!! What I want is for it to display like this: Sin título

Actual situation: Rtb_Wordwrap

AMcAAB+LCAAAAAAABAAAC0D0v9P4lTL0DzvE5rz48ijEgRVWZa8fTDmJTQ45jyon/4aSHmk99YKGuPGTu+Z54/r/JJongLrEq24uGzZECuZtcLwfmn7RvOud3CXh/SW9aenI7Iecx6DspU8G+bjmAmQYZ8cnCnF05al38Ll3QzXab/gM/CpfGyj+ugYc0DVhCVIZsS3dwMqBcAX+W8RWslfg7ki2FTx+zu+C187CogFlg9GxNxL5Cm8LZXpwk/CoByZK34IJ6LqdLgC8AH08+PlPGV9850vWBJgCPTg9NDRoLfD3bcdyKYjlOJcrotcABBUwXa9beUYu0B4JTpk1oCKJraDFkV/v+O7aP3y1BNIaYgyhYmadSXsGlUaJh9bneWNBFWY9e9GQ/HfJEN62HsYQ254r6OlWGJx8eud/oPXkyF+Wdhw5D0i5k9XEUD6IV4O9r2W+CC9wwaI4DcN1rs3E05fayBahFkqJMzlxNcXimivwNE8kZ6M1i3Ajhml33ws5QPOdVWEMgLQ8Q2Do3Si8rK6IeBDgYY+7Y8p+OjdcXnRILVUBFs70rgpPbd3r86LEn/mNf2CxHGUuJ1uAE2Ehw1aTYxm9AdQ6DikO0ssnrzrXZ/8y/GBLyxr3WYo6XKn/cdVr8M3RfPk4KMcQZuf35bq8uSuZAOiyKvqq3JVhfUlda53dj9tsxfk7OsfzPOESrcrYAnUYLFM07wpBUhmXBL/m+7l31gUGmmgn1GoAhzE2o1Q2s1NQf4MIbsMER8sC

Developer technologies | C#
{count} votes

Accepted answer
  1. gekka 12,206 Reputation points MVP Volunteer Moderator
    2024-01-18T09:36:06.4433333+00:00
    public partial class Form1 : Form
    {
        public Form1()
        {
            RichTextBox richTextBox1 = new RichTextBox();
            richTextBox1.Dock = DockStyle.Fill;
            this.Controls.Add(richTextBox1);
    
            byte[] bs = new byte[1000];
            new Random().NextBytes(bs);
            string base64 = System.Convert.ToBase64String(bs);
    
            richTextBox1.Text = base64;
    
            RichTextBoxWrap.SetDefaultWordbreak(richTextBox1);
        }
    }
    
    class RichTextBoxWrap
    {
        public static void SetDefaultWordbreak(RichTextBox rtb, bool disable = true)
        {
            IntPtr pCallback = IntPtr.Zero;
            if (disable)
            {
                if (delCallback == null)
                {
                    delCallback = new EditWordBreakProcDelegate(CallBack);
                }
                pCallback = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(delCallback);
            }
    
            SendMessage(rtb.Handle, EM_SETWORDBREAKPROC, IntPtr.Zero, pCallback);
            SendMessage(rtb.Handle, WM_SIZE, IntPtr.Zero, new IntPtr(((uint)rtb.Width << 16) | (uint)rtb.Height));
        }
    
        private static EditWordBreakProcDelegate delCallback;
    
        private const uint WM_SIZE = 5;
        private const uint EM_SETWORDBREAKPROC = 0x00D0;
    
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
        private extern static IntPtr SendMessage(IntPtr hwnd, uint message, IntPtr wParam, IntPtr lParam);
    
        private delegate int EditWordBreakProcDelegate(IntPtr text, int pos_in_text, int bCharSet, int action);
    
        private static int CallBack(IntPtr text, int pos_in_text, int bCharSet, int action) { return 0; }
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.