【问题标题】:Display scroll bar in textbox when contents are beyond the bounds C#当内容超出范围时在文本框中显示滚动条C#
【发布时间】:2010-11-27 07:50:59
【问题描述】:

是否只有当文本框中的行数大于显示的行数时才能显示/隐藏文本框中的滚动条?

【问题讨论】:

  • 很遗憾没有。您可以将滚动条设置为水平、垂直或两者,但在必要时不显示/隐藏。
  • 只在基本文本框中 - 试试 RichTextBox

标签: c# textbox scrollbar


【解决方案1】:

考虑使用RichTextBox——它内置了这种行为。

【讨论】:

  • 啊,谢谢奥斯汀。有时最明显的解决方案是最好的:)
  • 不要忘记将属性 ScrollViewer.VerticalScrollBarVisibility="Auto" 添加到 RichTextBox
【解决方案2】:
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  

【讨论】:

  • 对于那些必须使用文本框(就像我不得不因为它是一个自定义控件)的人来说,上面的答案似乎工作正常。我假设我必须用 OR 替换 + 以使其按位,并且我以 textbox.wordwrap 值为条件设置了分词。希望有帮助。
【解决方案3】:

谢谢笨蛋,它有效!这是 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.
}

【讨论】:

    【解决方案4】:

    我有 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
    

    【讨论】:

      【解决方案5】:

      我自己尝试了 tnimas 的解决方案,但无法捕捉到异常,所以我使用 WinApi 来切换滚动条的可见状态,而不是像这样:

      Size textBoxRect = TextRenderer.MeasureText(YourTextBox.Text, YourTextBox.Font, new Size(YourTextBox.Width, int.MaxValue), TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl);
      
      WinApi.ShowScrollBar(YourTextBox.Handle, (int)WinApi.ScrollBar.SB_VERT, textBoxRect.Height > YourTextBox.Height ? true : false);
      

      此方法不会导致异常,但应该注意,像这样隐藏滚动条会禁用滚动消息,但如果您只是在文本区域无法滚动时隐藏滚动条,这很好。

      【讨论】:

        猜你喜欢
        • 2013-05-28
        • 1970-01-01
        • 2020-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-06
        • 1970-01-01
        • 2016-07-02
        相关资源
        最近更新 更多