Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当内容超出边界C时,在文本框中显示滚动条#_C#_Textbox_Scrollbar - Fatal编程技术网

C# 当内容超出边界C时,在文本框中显示滚动条#

C# 当内容超出边界C时,在文本框中显示滚动条#,c#,textbox,scrollbar,C#,Textbox,Scrollbar,只有当文本框中的行数大于显示的行数时,才可以显示/隐藏文本框中的滚动条吗?考虑使用--它内置了这种行为。公共类TextBoxScrollbar插件 Public Class TextBoxScrollbarPlugin Private WithEvents mTarget As TextBox ''' <summary> ''' After the Handle is created, mTarget.IsHandleCreated always retur

只有当文本框中的行数大于显示的行数时,才可以显示/隐藏文本框中的滚动条吗?

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

公共类TextBoxScrollbar插件
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  
私有WithEvents mTarget作为文本框 ''' ''创建句柄后,mTarget.IsHandleCreated始终返回 “没错,即使HandleDestroyed被解雇了。 ''' ''' 私有错误处理创建为布尔值=False 公共子新建(项目作为文本框) mTarget=项目 mIsHandleCreated=mTarget.IsHandleCreated 端接头 私有子更新() 如果未创建mTarget.ishandleCreate,则 返回 如果没有处理不当的话 返回 如果结束 Dim textBoxRect=TextRenderer.MeasureText(mTarget.Text, mTarget.Font, 新大小(mTarget.Width、Integer.MaxValue), TextFormatFlags.WordBreak+TextFormatFlags.TextBoxControl) 尝试 如果textBoxRect.Height>mTarget.Height,则 mTarget.ScrollBars=ScrollBars.Vertical 其他的 mTarget.ScrollBars=滚动条。无 如果结束 捕获ex为System.ComponentModel.Win32Exception '这有时会引发“创建窗口句柄失败” “错误。 '如果文本框不可见和/或 '变小以显示工具栏。 如果启用了mLog.iswarn,则为mLog.Warn(“更新()”,例如) 结束尝试 端接头 私有子mTarget\u HandleCreated(发送方作为对象,e作为System.EventArgs)处理mTarget.HandleCreated mIsHandleCreated=True 端接头 私有子mTarget\u HandleDestroyed(发送方作为对象,e作为System.EventArgs)处理mTarget.HandleDestroyed mIsHandleCreated=False 端接头 私有子mTarget\u SizeChanged(发件人作为对象,e作为System.EventArgs)处理mTarget.SizeChanged 更新() 端接头 私有子mTarget\u TextChanged(发件人作为对象,e作为System.EventArgs)处理mTarget.TextChanged 更新() 端接头 末级 私有mPlugins作为新列表(对象) 私有子表单_Load(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load 添加(新的TextBoxScrollbarPlugin(txtBoxOne)) 添加(新的TextBoxScrollbarPlugin(txtBoxTwo)) 添加(新的TextBoxScrollbarPlugin(txtBoxThree)) 端接头
谢谢,笨蛋,它能用!这里是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.
}

我已经在vb中使用了tnimas解决方案。函数写得很好,我没有看到错误

    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

我自己尝试了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);

此方法不会导致异常,但应注意,这样隐藏滚动条将禁用滚动消息,但如果您只是在文本区域无法滚动时隐藏滚动条,则此方法没有问题。

不幸的是,没有。您可以将滚动条设置为水平、垂直或两者,但在必要时不显示/隐藏。这仅在BAIC文本框中-请尝试RichTextBoxAH谢谢Austin。有时最明显的解决方案是最好的:)不要忘记将属性ScrollViewer.VerticalScrollBarVisibility=“Auto”添加到RichTextBox对于那些必须使用Textbox的人(就像我不得不使用的那样,因为它是一个自定义控件),上面的答案似乎可以。我假设我必须将+替换为或使其按位,并将分词条件设置为textbox.wordwrap值。希望有帮助。