Click here to Skip to main content
15,867,453 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd13-Dec-21 11:42
Super Lloyd13-Dec-21 11:42 
GeneralRe: About RichTextBox size in a canvas Pin
Gerry Schmitz13-Dec-21 14:28
mveGerry Schmitz13-Dec-21 14:28 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd13-Dec-21 14:40
Super Lloyd13-Dec-21 14:40 
GeneralRe: About RichTextBox size in a canvas Pin
Gerry Schmitz14-Dec-21 7:28
mveGerry Schmitz14-Dec-21 7:28 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd14-Dec-21 12:28
Super Lloyd14-Dec-21 12:28 
AnswerRe: About RichTextBox size in a canvas Pin
Mycroft Holmes13-Dec-21 13:16
professionalMycroft Holmes13-Dec-21 13:16 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd13-Dec-21 13:27
Super Lloyd13-Dec-21 13:27 
AnswerRe: About RichTextBox size in a canvas Pin
#realJSOP14-Dec-21 0:37
mve#realJSOP14-Dec-21 0:37 
You can do this by handling each keypress, measuring the length of the displayed string, and changing the size of the RTB as needed. I would probably also turn off the scroll bars.


C#
using System.Drawing;

private void RtbField_TextChanged(object sender, TextChangedEventArgs e)
{
    RichTextBox rtb = sender as RichTextBox;
    double horzPadding = rtb.Padding.Left + rtb.Padding.Right;
    double vertPadding = rtb.Padding.Top + rtb.Padding.Bottom;
    System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular;
    if (!Enum.TryParse(rtb.FontStyle.ToString(), out style))
    {
        style = System.Drawing.FontStyle.Regular;
    }
    System.Drawing.Font measureFont = new Font(rtb.FontFamily.ToString(), (float)rtb.FontSize, style, GraphicsUnit.Pixel);
    TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

    Bitmap image = new Bitmap(1, 1);
    Graphics graphics = Graphics.FromImage(image);
    rtb.Width  = graphics.MeasureString(textRange.Text.Replace("\r\n",""), measureFont).Width + horzPadding+10;
    rtb.Height = graphics.MeasureString(textRange.Text.Replace("\r\n",""), measureFont).Height + vertPadding;
}


XAML
<Canvas Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
    <RichTextBox x:Name="rtbField" Height="30" Width="0" 
                 Padding="0,2"
                 VerticalContentAlignment="Center"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                 ScrollViewer.VerticalScrollBarVisibility="Disabled" 
                 TextChanged="RtbField_TextChanged"/>
</Canvas>

I gotta ask, though - aren't you concerned with too much text being entered to keep your layout reasonable?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013


modified 14-Dec-21 6:43am.

GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd14-Dec-21 0:52
Super Lloyd14-Dec-21 0:52 
GeneralRe: About RichTextBox size in a canvas Pin
#realJSOP14-Dec-21 8:42
mve#realJSOP14-Dec-21 8:42 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd14-Dec-21 12:26
Super Lloyd14-Dec-21 12:26 
GeneralRe: About RichTextBox size in a canvas Pin
#realJSOP14-Dec-21 23:49
mve#realJSOP14-Dec-21 23:49 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd15-Dec-21 0:02
Super Lloyd15-Dec-21 0:02 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd15-Dec-21 0:24
Super Lloyd15-Dec-21 0:24 
GeneralRe: About RichTextBox size in a canvas Pin
#realJSOP16-Dec-21 2:42
mve#realJSOP16-Dec-21 2:42 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd16-Dec-21 2:55
Super Lloyd16-Dec-21 2:55 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd14-Dec-21 2:10
Super Lloyd14-Dec-21 2:10 
QuestionWPF Converter Set or Library Pin
michaelbarb10-Dec-21 9:35
michaelbarb10-Dec-21 9:35 
AnswerRe: WPF Converter Set or Library Pin
Gerry Schmitz10-Dec-21 10:04
mveGerry Schmitz10-Dec-21 10:04 
GeneralRe: WPF Converter Set or Library Pin
michaelbarb10-Dec-21 11:14
michaelbarb10-Dec-21 11:14 
GeneralRe: WPF Converter Set or Library Pin
Gerry Schmitz11-Dec-21 5:51
mveGerry Schmitz11-Dec-21 5:51 
GeneralRe: WPF Converter Set or Library Pin
michaelbarb11-Dec-21 13:25
michaelbarb11-Dec-21 13:25 
GeneralRe: WPF Converter Set or Library Pin
Gerry Schmitz13-Dec-21 7:26
mveGerry Schmitz13-Dec-21 7:26 
GeneralRe: WPF Converter Set or Library Pin
michaelbarb13-Dec-21 14:44
michaelbarb13-Dec-21 14:44 
AnswerRe: WPF Converter Set or Library Pin
RedDk11-Dec-21 8:29
RedDk11-Dec-21 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.