2

I've been doing a little bit of looking around, but so far, I haven't seen exactly what I'm looking for. I have a TextBox that is designed to look like a Label in a VB.Net program, because Labels aren't capable of all the same things TextBoxes are. I want a scrollbar on each axis to be shown when and only when it would actually be needed. The font uses different widths for different characters, and that's not something I'm going to be able to change for this.

How can scrollbars like this be put on there? I'm a little green in terms of VB.Net GUI design, so answers that involve 30 lines of code to do this one thing are probably going to be a little hard to follow and apply. I really need something that isn't overly complicated, if that's possible. Thanks!

Panzercrisis
  • 4,590
  • 6
  • 46
  • 85

1 Answers1

4

Update

I wrote you a Function with the Link i have already posted here. This should do it for you. ( Please make sure that for this test your Textbox is Textbox6 not Textbox1)

Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
    If CheckLength(TextBox6.Text) = True Then
        TextBox6.ScrollBars = ScrollBars.Vertical
    Else
        TextBox6.ScrollBars = ScrollBars.None
    End If
End Sub

Private Function CheckLength(ByVal longStr As String)
    Dim TrueOrFalse As Boolean = False

    Dim f As Font = Me.TextBox6.Font
    Dim rect As Rectangle = TextBox6.ClientRectangle
    Dim charFitted As Integer
    Dim linesFitted As Integer
    Using g As Graphics = TextBox6.CreateGraphics()
        Dim sf As New StringFormat(StringFormatFlags.NoWrap)
        sf.LineAlignment = StringAlignment.Center
        sf.Alignment = StringAlignment.Near
        sf.Trimming = StringTrimming.EllipsisCharacter
        sf.FormatFlags = StringFormatFlags.DirectionVertical
        g.MeasureString(longStr, f, rect.Size, sf, charFitted, linesFitted)
    End Using

    If charFitted < longStr.Length Then
        TrueOrFalse = True
    End If

    Return TrueOrFalse
End Function

This checks the length of entire check box ( multi-line or not ), CheckLength is a boolean Function that returns True if the Textbox's Length has been exceeded.

Reguards

Don Thomas Boyle
  • 3,055
  • 3
  • 32
  • 54
  • Thanks. I'm thinking about accepting this. My only concern is that certain fonts don't space out their characters evenly, and that can kind throw this off a little. But it looks like this is basically the right idea, and it looks like TextRenderer.MeasureText can be added here to deal with that complication. – Panzercrisis Sep 04 '13 at 21:55
  • besides won't you use a specific font anyway? - therefore text size will always be the same? on your machine or a customers? Although if you really want to get into it this post `http://www.pcreview.co.uk/forums/get-number-visible-characters-textbox-t1321864.html` has a somewhat simple solution of how to GRAB all visible characters within the textbox area and use its number as the max length for the textbox. - not perfect but the closes char count you will get from my understanding – Don Thomas Boyle Sep 05 '13 at 00:49
  • The problem is that with many fonts, that number isn't a constant. The width of "XXXXX" would be greater than that of "iiiii", even though they have the same number of characters. So even if you know the font ahead of time, being able to tell whether text is going to get cut off is generally more complicated than just counting the number of characters. – Panzercrisis Sep 05 '13 at 12:34
  • @Panzercrisis hope the new updates make sence :) - font doesnt matter. – Don Thomas Boyle Sep 05 '13 at 13:04
  • This is not going to work well, TextBox uses a very different text renderer. You'll need to use the TextRenderer class with the special TextFormatFlags.TextBoxControl option. – Hans Passant Sep 05 '13 at 13:29
  • @Hans Passant, I have tested this in an application, works fine for me. Have used this on Big fonts , small fonts different fonts. Always seems to populate the Vertiacal Scrollbar as it needed and was supposed to. - not saying its a perfect science but to say it won't work well is not correct. – Don Thomas Boyle Sep 05 '13 at 13:40
  • These difference are subtle. You can make them obvious by using text with lots of i letters. Check [this answer](http://stackoverflow.com/a/7269191/17034) to see it fall apart. – Hans Passant Sep 05 '13 at 13:46
  • As it seems when someone types "Hiiii...." you are correct, thanks for the know-how on this, ill have to study up on that. Hope this answer still can help someone. – Don Thomas Boyle Sep 05 '13 at 13:52