23

My C# application includes grids with both simple text boxes and richtext boxes. Often the richtext boxes contain rich text copied and pasted from elsewhere, and often the rtf markup includes hardcoded font size (\fsXX, XX in half points). In most cases the rich text font size is the same or close to the simple text font size.

When the DPI scaling is set to anything other than the default 96 the rich text is distorted as follows:

a) When the application is NOT set to be DPI aware the richtext is shown smaller than the simple text and is blurry.

b) When the application is set to be DPI aware the rich text is larger than the simple text.

Is there a means to allow or force the richtext to scale with the simple text, short of editing the markup directly?

Steve RG
  • 231
  • 1
  • 4
  • Did you try richtextbox v5? – Jerry Oct 28 '13 at 14:39
  • Can you add some screenshots? It might make it easier to help you... – Steve Sheldon Jul 09 '15 at 16:26
  • How are the fonts defined for the simple text boxes are they in point sizes or pixels? – Steve Sheldon Jul 09 '15 at 16:28
  • One more thing, the world of high dpi on windows is slightly more complicated than you are seeing so far. As well as getting the app to work at 96 dpi, you also want to test at dpi's between 96 and 149 then >150. The dpi aware flag which causes stretching when set to false doesn't start working until the dpi is at least 150. – Steve Sheldon Jul 09 '15 at 16:29

6 Answers6

0

Try to set its property WordWrap to true.

Syed
  • 1
  • 2
0

One thing that might solve the problem is to set the RichTextBox on a form and set the AutoScaleMode property of the form to None (AutoScaleMode Enumeration documentation)

bman
  • 5,016
  • 4
  • 36
  • 69
0

Sure! Here's an example of how to add a simple text box to a grid in C#:

DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.HeaderText = "My Text Column";
column.Name = "MyTextColumn";
dataGridView1.Columns.Add(column);

And here's an example of how to add a rich text box to a grid in C#:

DataGridViewRichTextBoxColumn column = new DataGridViewRichTextBoxColumn();
column.HeaderText = "My Richtext Column";
column.Name = "MyRichtextColumn";
dataGridView1.Columns.Add(column);

Note that for the rich text box, you'll need to use a custom column type that you'll have to create. Here's an example implementation:

public class DataGridViewRichTextBoxColumn : DataGridViewColumn
{
    public DataGridViewRichTextBoxColumn() : base(new DataGridViewRichTextBoxCell())
    {

    }
}

public class DataGridViewRichTextBoxCell : DataGridViewCell
{
    public override Type ValueType
    {
        get { return typeof(string); }
    }

    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        var ctl = DataGridView.EditingControl as DataGridViewRichTextBoxEditingControl;
        if (Value == null)
            ctl.InitialText = string.Empty;
        else
            ctl.InitialText = Value.ToString();
    }

    public override Type EditType
    {
        get { return typeof(DataGridViewRichTextBoxEditingControl); }
    }

    public override object DefaultNewRowValue
    {
        get { return string.Empty; }
    }
}

public class DataGridViewRichTextBoxEditingControl : RichTextBox, IDataGridViewEditingControl
{
    private DataGridView dataGridView;
    private bool valueChanged = false;
    private int rowIndex;

    public DataGridViewRichTextBoxEditingControl()
    {
        this.BorderStyle = System.Windows.Forms.BorderStyle.None;
    }

    public object EditingControlFormattedValue
    {
        get { return this.Rtf; }
        set { this.Rtf = value as string; }
    }

    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return this.EditingControlFormattedValue;
    }

    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.ForeColor = dataGridViewCellStyle.ForeColor;
        this.BackColor = dataGridViewCellStyle.BackColor;
        this.BorderStyle = dataGridViewCellStyle.BorderStyle == DataGridViewCellBorderStyle.None ? BorderStyle.None : BorderStyle.Fixed3D;
    }

    public int EditingControlRowIndex
    {
        get { return rowIndex; }
        set { rowIndex = value; }
    }

    public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
    {
        switch (key & Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
            case Keys.PageDown:
            case Keys.PageUp:
                return true;

            default:
                return !dataGridViewWantsInputKey;
        }
    }

    public void PrepareEditingControlForEdit(bool selectAll)
    {
        
    }

    public bool RepositionEditingControlOnValueChange
    {
        get { return false; }
    }

    public DataGridView EditingControlDataGridView
    {
        get { return dataGridView; }
        set { dataGridView = value; }
    }

    public bool EditingControlValueChanged
    {
        get { return valueChanged; }
        set { valueChanged = value; }
    }

    public Cursor EditingPanelCursor
    {
        get { return base.Cursor; }
    }

    protected override void OnTextChanged(System.EventArgs e)
    {
        base.OnTextChanged(e);
        valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
    }
}

I hope this helps! Let me know if you have any questions.

John G
  • 319
  • 4
  • 8
-1

Maybe you can use a WPF form, so you don't have to worry about de DPI of different screens

user2461687
  • 173
  • 1
  • 13
-1
<script type="text/javascript">
tinyMCE.init({
        mode: "textareas",
        theme: "advanced",
        plugins: "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: false,
        template_external_list_url: "js/template_list.js",
        external_link_list_url: "js/link_list.js",
        external_image_list_url: "js/image_list.js",
        media_external_list_url: "js/media_list.js"
    });
</script>


<td class="textboxmain" style="height:300px; "><asp:TextBox id="textbox1" TextMode="MultiLine" Height="100%" runat="server" placeholder="test............"></asp:TextBox></td>
-3

Please try the following, it is supported only in .NET Framework 4.5.2 onwards. Microsoft has covered a few more controls for HighDpiAutoresizing.

<appSettings>
   <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>
Kajal Sinha
  • 1,565
  • 11
  • 20
  • 1
    this feature was introduced in 4.5.1 and still doesn't support scaling Richtextbox up to version 4.6.1. More infos here https://msdn.microsoft.com/en-us/library/ms171868%28v=vs.110%29.aspx – Souhaieb Besbes Feb 11 '16 at 16:32