Click here to Skip to main content
15,890,947 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Eddy Vluggen9-Feb-22 2:40
professionalEddy Vluggen9-Feb-22 2:40 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Super Lloyd9-Feb-22 12:10
Super Lloyd9-Feb-22 12:10 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Eddy Vluggen9-Feb-22 12:13
professionalEddy Vluggen9-Feb-22 12:13 
AnswerRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Richard Deeming4-Jan-22 0:04
mveRichard Deeming4-Jan-22 0:04 
QuestionSetting DataGrid Cell Style From Code Behind Pin
Kevin Marois20-Dec-21 10:56
professionalKevin Marois20-Dec-21 10:56 
AnswerRe: Setting DataGrid Cell Style From Code Behind Pin
#realJSOP22-Dec-21 2:06
mve#realJSOP22-Dec-21 2:06 
QuestionInput Question Pin
Super Lloyd19-Dec-21 4:17
Super Lloyd19-Dec-21 4:17 
AnswerRe: Input Question Pin
#realJSOP19-Dec-21 22:26
mve#realJSOP19-Dec-21 22:26 
GeneralRe: Input Question Pin
Super Lloyd19-Dec-21 23:05
Super Lloyd19-Dec-21 23:05 
GeneralRe: Input Question Pin
#realJSOP19-Dec-21 23:18
mve#realJSOP19-Dec-21 23:18 
GeneralRe: Input Question Pin
Super Lloyd19-Dec-21 23:23
Super Lloyd19-Dec-21 23:23 
Question(Advanced?) Layout question Pin
Super Lloyd17-Dec-21 20:02
Super Lloyd17-Dec-21 20:02 
QuestionAbout RichTextBox size in a canvas Pin
Super Lloyd12-Dec-21 1:23
Super Lloyd12-Dec-21 1:23 
AnswerRe: About RichTextBox size in a canvas Pin
Richard Deeming12-Dec-21 21:49
mveRichard Deeming12-Dec-21 21:49 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd12-Dec-21 21:58
Super Lloyd12-Dec-21 21:58 
AnswerRe: About RichTextBox size in a canvas Pin
Gerry Schmitz13-Dec-21 7:22
mveGerry Schmitz13-Dec-21 7:22 
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 

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.