[英]What is an easy way to make scrollbars in a VB.Net TextBox only show up when needed?
我一直在四处张望,但到目前为止,我还没有完全看到我想要的东西。 我有一个TextBox,旨在使它看起来像VB.Net程序中的Label,因为Label不能完全具备TextBoxes的所有功能。 我希望仅在实际需要时才显示每个轴上的滚动条。 字体为不同的字符使用不同的宽度,这不是我要为此更改的内容。
这样的滚动条怎么能放在那儿呢? 就VB.Net GUI设计而言,我有点绿色,因此涉及30行代码来完成这一任务的答案可能很难遵循和应用。 如果可能的话,我真的需要一些不太复杂的东西。 谢谢!
更新
我已经在这里发布了带有Link
的Function
。 这应该为您做。 (请确保对于此测试,您的Textbox
是Textbox6
而不是Textbox1
)
Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
If CheckLength(TextBox6.Text) = True Then
TextBox6.ScrollBars = ScrollBars.Vertical
Else
TextBox6.ScrollBars = ScrollBars.None
End If
End Sub
Private Function CheckLength(ByVal longStr As String)
Dim TrueOrFalse As Boolean = False
Dim f As Font = Me.TextBox6.Font
Dim rect As Rectangle = TextBox6.ClientRectangle
Dim charFitted As Integer
Dim linesFitted As Integer
Using g As Graphics = TextBox6.CreateGraphics()
Dim sf As New StringFormat(StringFormatFlags.NoWrap)
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Near
sf.Trimming = StringTrimming.EllipsisCharacter
sf.FormatFlags = StringFormatFlags.DirectionVertical
g.MeasureString(longStr, f, rect.Size, sf, charFitted, linesFitted)
End Using
If charFitted < longStr.Length Then
TrueOrFalse = True
End If
Return TrueOrFalse
End Function
这将检查整个复选框的长度(是否为多行), CheckLength
是一个boolean Function
,如果超出了Textbox's Length
,则returns
True
。
Reguards
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.