2009-09-10 4 views
19

Est-il possible d'afficher/masquer la barre de défilement dans une zone de texte uniquement lorsque le nombre de lignes dans la zone de texte est supérieur au nombre de lignes affichées?Afficher la barre de défilement dans la zone de texte lorsque le contenu dépasse les limites C#

+0

Malheureusement non. Vous pouvez définir les barres de défilement sur horizontal, vertical ou les deux, mais ne pas afficher/masquer si nécessaire. – Anders

+0

c'est seulement dans la boîte de texte bacic - essayez RichTextBox – Cullub

Répondre

25

Pensez à utiliser la RichTextBox - il a ce comportement construit en

+1

Ahh merci Austin. Parfois, les solutions les plus évidentes sont les meilleures :) – Anders

+1

Ne pas oublier d'ajouter la propriété ScrollViewer.VerticalScrollBarVisibility = "Auto" à la 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

Pour ceux qui doivent utiliser Textbox (comme je devais car c'est un contrôle personnalisé) ci-dessus la réponse semble fonctionner correctement. J'ai supposé que je devais remplacer le + avec OU pour le rendre au niveau du bit et j'ai fait le wordbreak conditionnel à la valeur de textbox.wordwrap. J'espère que cela pourra aider. – Tim

4

Merci mannequin, cela fonctionne.! Ici, version courte de la réponse factice dans C#

Appelez ce code à la fin de votre SizeChanged et gestionnaires 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

J'ai tnimas solution de travail en vb. Fonctionne très bien comme écrit et je n'ai pas vu les erreurs.

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 
Questions connexes