webForumDet fria alternativet

RichTextBox autosize

1 svar · 355 visningar · startad av Cactuz

CactuzMedlem sedan okt. 2001670 inlägg
#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.

cokMedlem 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.

133 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)