'实现TextBox的高度自适应(MultiLine属性为ture情况下)
'在设计时还应将TextBox的minimumSize设为(0,21)或其它非空值,可选设置maximumsize
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'TextBox1 的行高
Dim intLineHeight As Integer = Me.TextBox1.MinimumSize.Height - Me.TextBox1.Font.Size
'获得行数
Dim intLineCount As Integer = Me.TextBox1.GetLineFromCharIndex(TextBox1.Text.Length - 1) + 1
'设置高度
'Me.TextBox1.Height = intLineHeight * intLineCount + Me.TextBox1.Font.Size
'如果有需要限制最大高度,用如下代码代替上面一行
Dim intTBHeight As Integer = intLineHeight * intLineCount + Me.TextBox1.Font.Size
If intTBHeight <= Me.TextBox1.MaximumSize.Height Then Me.TextBox1.Height = intTBHeight
End Sub
'在设计时还应将TextBox的minimumSize设为(0,21)或其它非空值,可选设置maximumsize
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'TextBox1 的行高
Dim intLineHeight As Integer = Me.TextBox1.MinimumSize.Height - Me.TextBox1.Font.Size
'获得行数
Dim intLineCount As Integer = Me.TextBox1.GetLineFromCharIndex(TextBox1.Text.Length - 1) + 1
'设置高度
'Me.TextBox1.Height = intLineHeight * intLineCount + Me.TextBox1.Font.Size
'如果有需要限制最大高度,用如下代码代替上面一行
Dim intTBHeight As Integer = intLineHeight * intLineCount + Me.TextBox1.Font.Size
If intTBHeight <= Me.TextBox1.MaximumSize.Height Then Me.TextBox1.Height = intTBHeight
End Sub