Click here to Skip to main content
15,505,828 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd13-Dec-21 12:42
Super Lloyd13-Dec-21 12:42 
GeneralRe: About RichTextBox size in a canvas Pin
Gerry Schmitz13-Dec-21 15:28
mveGerry Schmitz13-Dec-21 15:28 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd13-Dec-21 15:40
Super Lloyd13-Dec-21 15:40 
GeneralRe: About RichTextBox size in a canvas Pin
Gerry Schmitz14-Dec-21 8:28
mveGerry Schmitz14-Dec-21 8:28 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd14-Dec-21 13:28
Super Lloyd14-Dec-21 13:28 
AnswerRe: About RichTextBox size in a canvas Pin
Mycroft Holmes13-Dec-21 14:16
professionalMycroft Holmes13-Dec-21 14:16 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd13-Dec-21 14:27
Super Lloyd13-Dec-21 14:27 
AnswerRe: About RichTextBox size in a canvas Pin
#realJSOP14-Dec-21 1:37
mva#realJSOP14-Dec-21 1: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 1:52
Super Lloyd14-Dec-21 1:52 
GeneralRe: About RichTextBox size in a canvas Pin
#realJSOP14-Dec-21 9:42
mva#realJSOP14-Dec-21 9:42 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd14-Dec-21 13:26
Super Lloyd14-Dec-21 13:26 
GeneralRe: About RichTextBox size in a canvas Pin
#realJSOP15-Dec-21 0:49
mva#realJSOP15-Dec-21 0:49 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd15-Dec-21 1:02
Super Lloyd15-Dec-21 1:02 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd15-Dec-21 1:24
Super Lloyd15-Dec-21 1:24 
GeneralRe: About RichTextBox size in a canvas Pin
#realJSOP16-Dec-21 3:42
mva#realJSOP16-Dec-21 3:42 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd16-Dec-21 3:55
Super Lloyd16-Dec-21 3:55 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd14-Dec-21 3:10
Super Lloyd14-Dec-21 3:10 
QuestionWPF Converter Set or Library Pin
michaelbarb10-Dec-21 10:35
michaelbarb10-Dec-21 10:35 
AnswerRe: WPF Converter Set or Library Pin
Gerry Schmitz10-Dec-21 11:04
mveGerry Schmitz10-Dec-21 11:04 
GeneralRe: WPF Converter Set or Library Pin
michaelbarb10-Dec-21 12:14
michaelbarb10-Dec-21 12:14 
GeneralRe: WPF Converter Set or Library Pin
Gerry Schmitz11-Dec-21 6:51
mveGerry Schmitz11-Dec-21 6:51 
GeneralRe: WPF Converter Set or Library Pin
michaelbarb11-Dec-21 14:25
michaelbarb11-Dec-21 14:25 
GeneralRe: WPF Converter Set or Library Pin
Gerry Schmitz13-Dec-21 8:26
mveGerry Schmitz13-Dec-21 8:26 
GeneralRe: WPF Converter Set or Library Pin
michaelbarb13-Dec-21 15:44
michaelbarb13-Dec-21 15:44 
AnswerRe: WPF Converter Set or Library Pin
RedDk11-Dec-21 9:29
RedDk11-Dec-21 9: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.