webForumDet fria alternativet

RichTextBox autosize

.NET

1 svar · 355 visningar · startad av Cactuz

Medlem sedan okt. 2001670 inlägg
Frågan#1

Jag försöker få en RichTextBox att ändra storlek så att all text får plats i den utan att det behövs någon scroll. Hur jag än bär mig åt dock så får jag det inte att bli exakt. Man tror ju att (antalet rader) * (typsnittets linespace) borde räcka.

Några tips? Detta är en windows form.

Medlem sedan dec. 2005664 inlägg
#2

Nått i stil med detta:

            Point pos = new Point(0, 0);
            int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
            int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);

            pos.X = ClientRectangle.Width;
            pos.Y = ClientRectangle.Height;
            int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);
            int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);

            pos = richTextBox1.GetPositionFromCharIndex(lastIndex);
            this.Height = panelTop.Height + (pos.Y) + 20 ;

r: i mitt fall är this en egenskapad kontroll, men du borde utifrån pos.Y kunna fixa till din engen hörjd på textboxen.

257 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
118 ms — deklarationer (db)
0 ms — hämta statistik (cache)
133 ms — hämta tråd, inlägg och bilagor (db)
122 ms — ändringar (db)