Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I haven't seen an existing control to do this, but you can do it the hard way by using a RichTextBox and the TextRenderer's MeasureText method and repeatedly resizing the font. It's inefficient, but it works.</p> <p>This function is an event handler for the 'TextChanged' event on a RichTextBox.</p> <p>An issue I've noticed:</p> <p>When typing, the text box will scroll to the current caret even if scrollbars are disabled. This can result in the top line or left side getting chopped off until you move back up or left with the arrow keys. The size calculation is correct assuming you can get the top line to display at the top of the text box. I included some scrolling code that helps sometimes (but not always).</p> <p>This code assumes word wrap is disabled. It may need modification if word wrap is enabled.</p> <hr> <p>The code:</p> <pre><code>[DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, uint wMsg, int wParam, uint lParam); private static uint EM_LINEINDEX = 0xbb; private void richTextBox1_TextChanged(object sender, EventArgs e) { // If there's no text, return if (richTextBox1.TextLength == 0) return; // Get height and width, we'll be using these repeatedly int height = richTextBox1.Height; int width = richTextBox1.Width; // Suspend layout while we mess with stuff richTextBox1.SuspendLayout(); Font tryFont = richTextBox1.Font; Size tempSize = TextRenderer.MeasureText( richTextBox1.Text, richTextBox1.Font); // Make sure it isn't too small first while (tempSize.Height &lt; height || tempSize.Width &lt; width) { tryFont = new Font(tryFont.FontFamily, tryFont.Size + 0.1f, tryFont.Style); tempSize = TextRenderer.MeasureText(richTextBox1.Text, tryFont); } // Now make sure it isn't too big while (tempSize.Height &gt; height || tempSize.Width &gt; width) { tryFont = new Font(tryFont.FontFamily, tryFont.Size - 0.1f, tryFont.Style); tempSize = TextRenderer.MeasureText(richTextBox1.Text, tryFont); } // Swap the font richTextBox1.Font = tryFont; // Resume layout richTextBox1.ResumeLayout(); // Scroll to top (hopefully) richTextBox1.ScrollToCaret(); SendMessage(richTextBox1.Handle, EM_LINEINDEX, -1, 0); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload