Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichTextBox lose color during float #681

Open
krulci opened this issue Jul 3, 2022 · 4 comments
Open

RichTextBox lose color during float #681

krulci opened this issue Jul 3, 2022 · 4 comments
Labels

Comments

@krulci
Copy link

krulci commented Jul 3, 2022

I have a richtextbox that logs events of my app.
I uses AppendText to append each lines and set the coloring of the line
When the Log windows is released from dock to float, all the coloring disappear.
Any ideas to fix this?

@lextm lextm added the question label Jul 3, 2022
@ChristopherPH
Copy link

ChristopherPH commented Jul 14, 2022

I spent many hours fixing this - it is an issue with the RichTextBox control.

Docking and undocking a form with a RichTextBox control triggers an OnFontChanged() event, which resets the font and any colouring information.

/*
 * Copyright (c) 2022 Christopher Hayes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using System;
using System.Windows.Forms;

namespace TheBlackRoom.WinForms.Controls
{
    /// <summary>
    /// RichTextBox with workarounds for:
    ///     setting ZoomFactor
    ///     losing ZoomFactor on clear
    ///     losing RTF colourtable and formatting on font change or parent font change
    /// </summary>
    public class RichTextBoxEx : RichTextBox
    {
        protected override void OnFontChanged(EventArgs e)
        {
            //HACK: save the RTF as changing the font (or having the parent form's font change)
            //      causes the RTF to lose colour and formatting
            var savedRTF = this.Rtf;
            var savedSelectionStart = this.SelectionStart;
            var savedSelectionLength = this.SelectionLength;

            //HACK: save the zoom factor as setting the RTF sets it back to 1
            var tmpZoomFactor = this.ZoomFactor;

            //this line here causes the formatting loss
            base.OnFontChanged(e);

            //reset to the saved RTF
            this.Rtf = savedRTF;

            //Changing the RTF resets the zoom factor back to 1
            this.ZoomFactor = tmpZoomFactor;

            //Restore selection
            this.SelectionStart = savedSelectionStart;
            this.SelectionLength = savedSelectionLength;

            //Restore scroll position
            this.ScrollToCaret();
        }

        public new float ZoomFactor
        {
            get { return base.ZoomFactor; }
            set
            {
                //HACK: in order to properly set the zoom factor, set it twice.
                //      sometimes the underlying zoomfactor changes but the property
                //      doesn't.
                base.ZoomFactor = 1.0f;
                base.ZoomFactor = value;
            }
        }

        public new void Clear()
        {
            //HACK: save and restore the zoom factor as Clear()
            //      resets the zoom factor back to 1
            var tmpZoomFactor = ZoomFactor;

            base.Clear();

            ZoomFactor = tmpZoomFactor;
        }
    }
}

@krulci
Copy link
Author

krulci commented Jul 14, 2022

@ChristopherPH Love it. Thanks for the solution. Any idea if richtextbox is not the focus tab/windows but is still being input in the background. When focused, it shows a section of plain white and recolor and styles when scroll

@ChristopherPH
Copy link

@krulci I'm not quite sure I can help with that, but you can always play around with the RichTextBox.HideSelection property, in case that is causing the render to be a little weird. In my application I have it set to false.

Or perhaps when you are adding the RTF messages to the richtextbox, the selection is not being changed. I use the following to append my RTF. Perhaps that might help?

If not, I am at the end of my knowledge :)

/*
 * Copyright (c) 2022 Christopher Hayes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using System.Windows.Forms;

namespace TheBlackRoom.WinForms.Controls
{
    public static class RichTextBoxExtensions
    {
        public static void AppendRtf(this RichTextBox richTextBox, string Rtf)
        {
            if (string.IsNullOrEmpty(Rtf))
                return;

            //put selection at the end of the text
            richTextBox.Select(richTextBox.TextLength, 0);

            //Append rtf text
            richTextBox.SelectedRtf += Rtf;

            //put selection at the end of the text
            richTextBox.Select(richTextBox.TextLength, 0);
        }
    }
}

@ChristopherPH
Copy link

I just re-read your original question and you use AppendText(). In my app I generate RTF with the colour coding and fonts and append it via the extension method above, so my last post probably won't help you too much.

Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants