2009-09-10 17 views
19

¿Es posible mostrar/ocultar la barra de desplazamiento en un cuadro de texto solo cuando el número de líneas en el cuadro de texto es mayor que el número de líneas?Mostrar barra de desplazamiento en el cuadro de texto cuando el contenido está fuera de los límites C#

+0

Desafortunadamente no. Puede establecer barras de desplazamiento en horizontal, vertical o en ambos, pero no para mostrar/ocultar cuando sea necesario. – Anders

+0

que está solo en el cuadro de texto bá sico - prueba RichTextBox – Cullub

Respuesta

25

Considere el uso de la RichTextBox - tiene que el comportamiento construida en

+1

Ahh gracias Austin. A veces las soluciones más obvias son las mejores :) – Anders

+1

No olvides agregar la propiedad ScrollViewer.VerticalScrollBarVisibility = "Auto" a RichTextBox – Smile4ever

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

Para aquellos que deben usar Textbox (como tuve que hacer ya que es un control personalizado) lo anterior la respuesta parece funcionar bien. Supuse que tenía que reemplazar el + con O para hacerlo en bit e hice el wordbreak condicional en el valor de textbox.wordwrap. Espero que ayude. – Tim

4

Gracias maniquí, funciona.! Aquí la versión corta de la respuesta simulada en C#

Llame a este código al final de su SizeChanged y TextChanged manipuladores:

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

yo tengo tnimas solución de trabajo en VB. Funciona bastante bien como está escrito y no he visto los errores.

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 
Cuestiones relacionadas