Comments (4)
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.
@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.
@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.
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)
- Event handlers are no longer triggered. HOT 2
- Content Theme HOT 1
- Why not use Reshaper to improve code? HOT 8
- Position of dockContent
- Use dockpanelsuite's border in float windows HOT 4
- How to hide the caption of dockcontent? HOT 3
- Dock console HOT 3
- How can I show the float window contents while dragging with the mouse?
- ArgumentException: hrgn HOT 1
- Console App Icon
- Dockpanelsuite won't display icon HOT 1
- How to hide auto hide button? HOT 1
- I don't know how to activate the old tab HOT 1
- how to hide close button? HOT 3
- [ObjectListView_Keyboard]: Upon tab navigation the keyboard focus is not moving to column headers and list items. HOT 2
- Could not load type ContextMenu HOT 1
- DockPanel Presence of margins HOT 2
- The Close and Auto Hide buttons are not scaled in 4K screen with 200% DPI HOT 3
- Problem with Menu's HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dockpanelsuite.