2009-09-10 31 views
19

仅当文本框中的行数超过显示的行数时,才可以在文本框中显示/隐藏滚动条?当内容超出界限时在文本框中显示滚动条C#

+0

不幸的不是。您可以将滚动条设置为水平,垂直或两者,但必要时不显示/隐藏。 – Anders 2009-09-10 21:31:19

+0

这只是在bacic文本框 - 尝试RichTextBox – Cullub 2014-09-17 12:24:20

回答

25

考虑使用RichTextBox - 它具有内置的这种行为

+1

啊,谢谢奥斯汀。有时最明显的解决方案是最好的:) – Anders 2009-09-11 12:41:25

+1

不要忘记将属性ScrollViewer.VerticalScrollBarVisibility =“Auto”添加到RichTextBox – Smile4ever 2017-02-01 12:59:00

7
Public Class TextBoxScrollbarPlugin 
    Private WithEvents mTarget As TextBox 

    ''' <summary> 
    ''' After the Handle is created, mTarget.IsHandleCreated always returns 
    ''' TRUE, even after HandleDestroyed is fired. 
    ''' </summary> 
    ''' <remarks></remarks> 
    Private mIsHandleCreated As Boolean = False 

    Public Sub New(item As TextBox) 
     mTarget = item 
     mIsHandleCreated = mTarget.IsHandleCreated 
    End Sub 

    Private Sub Update() 
     If Not mTarget.IsHandleCreated Then 
      Return 
     ElseIf Not mIsHandleCreated Then 
      Return 
     End If 
     Dim textBoxRect = TextRenderer.MeasureText(mTarget.Text, 
                mTarget.Font, 
                New Size(mTarget.Width, Integer.MaxValue), 
                TextFormatFlags.WordBreak + TextFormatFlags.TextBoxControl) 

     Try 
      If textBoxRect.Height > mTarget.Height Then 
       mTarget.ScrollBars = ScrollBars.Vertical 
      Else 
       mTarget.ScrollBars = ScrollBars.None 
      End If 
     Catch ex As System.ComponentModel.Win32Exception 
      'this sometimes throws a "failure to create window handle" 
      'error. 
      'This might happen if the TextBox is unvisible and/or 
      'to small to display a toolbar. 
      If mLog.IsWarnEnabled Then mLog.Warn("Update()", ex) 
     End Try 
    End Sub 

    Private Sub mTarget_HandleCreated(sender As Object, e As System.EventArgs) Handles mTarget.HandleCreated 
     mIsHandleCreated = True 
    End Sub 

    Private Sub mTarget_HandleDestroyed(sender As Object, e As System.EventArgs) Handles mTarget.HandleDestroyed 
     mIsHandleCreated = False 
    End Sub 

    Private Sub mTarget_SizeChanged(sender As Object, e As System.EventArgs) Handles mTarget.SizeChanged 
     Update() 
    End Sub 

    Private Sub mTarget_TextChanged(sender As Object, e As System.EventArgs) Handles mTarget.TextChanged 
     Update() 
    End Sub 

End Class 


Private mPlugins As New List(Of Object) 
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    mPlugins.Add(New TextBoxScrollbarPlugin(txtBoxOne)) 
    mPlugins.Add(New TextBoxScrollbarPlugin(txtBoxTwo)) 
    mPlugins.Add(New TextBoxScrollbarPlugin(txtBoxThree)) 
End Sub 
+1

对于那些必须使用Textbox(像我必须作为自定义控件一样)的人答案似乎工作正常。我推测我必须用OR替换+来使其成为按位,并且我在wordbox.wordwrap值的基础上创建了条件分解符。希望有所帮助。 – Tim 2014-09-01 10:13:20

4

感谢假人,它的作品!假答案,并在此短版在C#

调用此代码在您SizeChanged将结束框TextChanged处理程序:

Size textBoxRect = TextRenderer.MeasureText(
    this.YourTextBox.Text, 
    this.YourTextBox.Font, 
    new Size(this.YourTextBox.Width, int.MaxValue), 
    TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl); 
try 
{ 
    this.YourTextBox.ScrollBars = textBoxRect.Height > this.YourTextBox.Height ? 
     ScrollBars.Vertical : 
     ScrollBars.None; 
} catch (System.ComponentModel.Win32Exception) 
{ 
    // this sometimes throws a "failure to create window handle" error. 
    // This might happen if the TextBox is unvisible and/or 
    // too small to display a toolbar. 
} 
0

我有tnimas解决方案在VB中工作。函数写得很好,我没有看到错误。

Private Sub TextBoxSizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged 
    Dim textBoxRect As Size = TextRenderer.MeasureText(TextBox.Text, TextBox.Font, New Size(TextBox.Width, Integer.MaxValue), TextFormatFlags.WordBreak Or TextFormatFlags.TextBoxControl) 
    Try 
     TextBox.ScrollBar = If(textBoxRect.Height > TextBox.Height, ScrollBars.Vertical, ScrollBars.None) 
    Catch ex As Exception 
     'handle error 
    End Try 
End Sub 
相关问题