Hassan Ibrahem
Hassan Ibrahem

Reputation: 43

How to make a rich textbox automatically size in wpf?

How to make a RichTextBox automatically size? I want my rich textbox to fit with any text I assign to it without having scroll bars. Thanks.

Upvotes: 4

Views: 8253

Answers (3)

s7urmi
s7urmi

Reputation: 23

Try using the ViewBox

<Viewbox>
    <RichTextBox />
</Viewbox>

Upvotes: -1

Dean Chalk
Dean Chalk

Reputation: 20451

Set the HorizontalAlignment and VerticalAlignment to something other than Stretch (which is the default). This will make the TextBox shrink to fit its contents. If you then stick it inside a StackPanel with Orientation of Horizontal it will not scroll vertically but be clipped by its containing panel, which I think is what you are after. If you want you containing panel to resize itself to the text, then you need to configure the panel accordingly

Upvotes: 1

Diainwater
Diainwater

Reputation: 180

        Bitmap objBmpImage = new Bitmap(1, 1);

        int intWidth = 0;
        int intHeight = 0;

        // Create the Font object for the image text drawing.
        Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);

        // Create a graphics object to measure the text's width and height.
        Graphics objGraphics = Graphics.FromImage(objBmpImage);

        // This is where the bitmap size is determined.
        intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
        intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;

You already have string to set textbox. Firstly, you can calculate like above to measure proper size of richtextbox. you can just set intWidth and intHeight at textbox. I think that makes you helpful.

Upvotes: 1

Related Questions