Code Monkey home page Code Monkey logo

Comments (4)

ChristopherPH avatar ChristopherPH commented on July 4, 2024 1

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;
        }
    }
}

from dockpanelsuite.

krulci avatar krulci commented on July 4, 2024

@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

from dockpanelsuite.

ChristopherPH avatar ChristopherPH commented on July 4, 2024

@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);
        }
    }
}

from dockpanelsuite.

ChristopherPH avatar ChristopherPH commented on July 4, 2024

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!

from dockpanelsuite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.