根据动态附加到其上的文本量增加文本框 [英] Increasing a textbox based on amount of text appended to it dynamically

查看:56
本文介绍了根据动态附加到其上的文本量增加文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,根据其中的行数改变大小,这些行从文本文件中读取,然后附加到richtextbox(在你提问之前,我需要文本格式化)



每次追加后,我都会在每次追加功能后设置一个增加的行数。



一旦附加了所有内容到文本框时,它会结束,而行不是什么,它然后运行函数来调整currenttb。



我无法弄清楚为什么tb没有' t似乎一直都是相同大小



附加文字的字体大小为12.



我想将行设置为main.linecount * 12. 12是附加的字体大小,但这不起作用。大小很小。

如果我去22,它适合大约18行,就好像有22,文本框太大了。如果我只有5行,那么盒子太小了。



我尝试了什么:



设置文本框高度按以下方式完成



私有子txtboxheight(ByVal linecount)

如果是currenttb。 InvokeRequired然后

currenttb.Invoke(New Action(Of String)(AddressOf txtboxheight),linecount)

Else



currenttb.Height = main.linecount * 22

结束如果

main.linecount = 0

结束Sub

I have a textbox which changes size depending on amount of lines in it, These lines are read from a text file, and then appended to a richtextbox (Before you ask, I require text formatting)

After each append, I have set a line count to increase after each run of the append function.

Once everything has been appended to the textbox, it hits an end while line isnot nothing, it then runs the function to adjust the currenttb.

I can't figure out why the tb doesn't seem the same size all the time

The font size as the text is being appended is 12.

I thought to set the line to be main.linecount * 12. 12 being the size of the font as it's appended, but this doesn't work. size is much to small.
If I go for 22, it suits for about 18 lines, where as if there are 22, the textbox is too big. If I only have 5 lines, the box is too small.

What I have tried:

the set textbox height is done as per below

Private Sub txtboxheight(ByVal linecount)
If currenttb.InvokeRequired Then
currenttb.Invoke(New Action(Of String)(AddressOf txtboxheight), linecount)
Else

currenttb.Height = main.linecount * 22
End If
main.linecount = 0
End Sub

推荐答案

这个工作得相当好......

我填充了RichTextBox
This works reasonably well...
I populated a RichTextBox
Dim sb As StringBuilder = New StringBuilder("")
For i = 1 To 20
    sb.AppendLine("Lorem Ipsum etc")
Next
sb.Length -= Environment.NewLine.Length 'Remove final newline
RichTextBox1.Text = sb.ToString()

txtboxheight(RichTextBox1)



这是我对txtboxheight例程的解释


This is my interpretation of the txtboxheight routine

Private Sub Txtboxheight(rtb As RichTextBox)
    Dim rtbSize As Size = TextRenderer.MeasureText(RichTextBox1.Text, RichTextBox1.Font)
    RichTextBox1.Width = rtbSize.Width + (2 * RichTextBox1.Font.SizeInPoints)
    RichTextBox1.Height = rtbSize.Height + RichTextBox1.Font.SizeInPoints
End Sub



MSDN参考 - TextRenderer类(系统.Windows.Forms) [ ^ ]



需要考虑的事项:

这假定所有行是相同的高度 - 如果你添加了格式,那么可能不是这种情况。您可以使用.Rtf属性,但如果您这样做,则宽度不能正确计算。



为了在底部添加填充我已经使用字体大小(以磅为单位)作为任意缓冲区 - 您可能想要使用它。类似地,对于宽度,我使用两倍于我尝试的所有字体的字体大小。



如果你不调整宽度,那么要注意而不是 WordWrap = True 将完全破坏计算


MSDN reference - TextRenderer Class (System.Windows.Forms)[^]

Things to consider:
This assumes that all the lines are the same height - if you have added formatting then this might not be the case. You could possibly use the .Rtf property but if you do then the Width is not calculated properly.

For the addition of the "padding" at the bottom I've used the Font size (in points) as an arbitrary buffer - you might want to play around with that. Similarly for the width I used twice the font size which worked for all the fonts I tried.

If you don't adjust the width then be aware than having WordWrap = True will completely break the calculation


字体高度通常以磅为单位给出,每个点都是1/72英寸。因此,您需要将其转换为像素,调整为屏幕的像素密度。请参阅 c# - 将像素转换为点 - 堆栈溢出 [ ^ ]。
A font height is usually given in points, each of which is 1/72 inches. So you need to convert that to pixels, adjusted to the pixel density of your screen. See c# - Convert Pixels to Points - Stack Overflow[^].


这篇关于根据动态附加到其上的文本量增加文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆