Imports System.ComponentModel
''' <summary>
''' This is a multilined text box control where you
''' can set max lines value and max length value and
''' he will calculate the right value for max length.
''' </summary>
''' <remarks>
''' With this control you can specify the max number of lines
''' and the max length per line. Don't change the value of max length
''' because he auto changes it to the right values.
''' </remarks>
''' <history>Lasering 30.09.2006</history>
Public Class MultiLinedTextBox
    Inherits System.Windows.Forms.TextBox
    Dim MaxL As Integer = 2
    Dim MaxC As Integer = 20

    ''' <summary>Gets or sets the maximum number of lines the user can type or paste into the text box control.</summary>
    ''' <returns></returns>
    ''' <history>Lasering 30.09.2006</history>
    <Category("Behavior"), DefaultValue(2)> Public Property MaxLines() As Integer
        Get
            Return MaxL
        End Get
        Set(ByVal value As Integer)
            Me.Multiline = True
            MaxL = value
            Me.MaxLength = MaxC * MaxL + (MaxL - 1) * 2
            If value = 1 Then Me.Multiline = False
        End Set
    End Property

    ''' <summary>Gets or sets the maximum number of charaters the user can type or paste per line into the text box control.</summary>
    ''' <returns></returns>
    ''' <history>Lasering 30.09.2006</history>
    <Category("Behavior"), DefaultValue(20)> Public Property MaxLengthPerLine() As Integer
        Get
            Return MaxC
        End Get
        Set(ByVal value As Integer)
            Me.Multiline = True
            MaxC = value
            Me.MaxLength = MaxC * MaxL + (MaxL - 1) * 2
        End Set
    End Property

    Protected Sub MultiLinedTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
        If Me.Text.Length > 0 And MaxC > 0 Then
            Dim Line As Short = Me.GetLineFromCharIndex(Me.GetFirstCharIndexOfCurrentLine)
            If Line <> -1 And Me.Lines(Line).Length <> -1 And Me.Lines(Line).Length > MaxC Then
                Me.SelectionStart = Me.GetFirstCharIndexOfCurrentLine + MaxC
                Me.SelectionLength = Me.Text.Length - Me.SelectionStart
                Me.SelectedText = Nothing
            End If
        End If

        If Me.Lines.Length > MaxL Then
            Me.SelectionStart = Me.GetFirstCharIndexOfCurrentLine
            Me.SelectionLength = Me.Text.Length - Me.SelectionStart
            Me.SelectedText = Nothing
            Me.SelectionStart = MaxL
            Me.Update()
        End If
    End Sub

    Public Sub New()
        Me.Multiline = True
        Me.MaxLength = 42
    End Sub
End Class
