测量字符串RichTextBox控件内 [英] Measure String inside RichTextBox Control

查看:190
本文介绍了测量字符串RichTextBox控件内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能请解释一下我怎么会去衡量一个RichTextBox控件里面的绳子,让我可以根据其内容自动调整RichTextBox控件?

Can somebody please explain how I would go about measuring the string inside a richtextbox control so that the I can automatically resize the richtextbox control according to its content?

感谢您

编辑:

我已经想过这个问题,而且由于下面的答案将无法正常工作,如果有不同的字体在RichTextBox控件,如果,我能得到RichTextBox控件的左上角COORDS然后拿到RTB内的文本的最后一行的右下角COORDS。这将从根本上给我的宽度和RichTextBox控件内部的字符串的高度。这可能吗? ?或者这是一个坏主意,做这种方式

I've thought about it, and since the below answer won't work if there are different fonts in the RichTextBox Control, what if, I could get the upper-left coords of the richtextbox control and then get the bottom-right coords of the very last line of text inside the rtb. That would essentially give me the Width and Height of the string inside the RichTextBox Control. Is this possible? Or is this a bad idea to do it this way?

推荐答案

把下面的代码的 ContentsResized 事件:

Put the following code in the ContentsResized event:

Private Sub rtb_ContentsResized(ByVal sender As Object, ByVal e As System.Windows.Forms.ContentsResizedEventArgs) Handles txtQuestion.ContentsResized
        Dim h = e.NewRectangle.Height, w = e.NewRectangle.Width
        h = Math.Max(h, sender.Font.Height)
        h = Math.Min(h, Me.ClientSize.Height - 10 - sender.Top)
        h += sender.Height - sender.ClientSize.Height + 1
        sender.Height = h
End Sub

这篇关于测量字符串RichTextBox控件内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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