views:

254

answers:

1

Hi,

I've got my own custom TextBox control, wich inherits from System.Windows.Forms.TextBox. I've overrided OnKeyDown method, because I want to select the previous or next control if the user press either up or down keys.

    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)

    MyBase.OnKeyDown(e)

    If e.KeyCode = Keys.Up Then
        If Not Multiline Then
            Me.FindForm().SelectNextControl(Me, False, True, True, True)
        Else
            'TODO: If the current line is the first one, select the previous control
        End If
    ElseIf e.KeyCode = Keys.Down Then
        If Not Multiline Then
            Me.FindForm().SelectNextControl(Me, True, True, True, True)
        Else
            'TODO: If the current line is the last one, select the next control
        End If
    End If

End Sub

In a multiline textbox, what is the better way to know if I'm in the first or last line?

Thanks very much

+3  A: 

It's rough, but it should do the job.

    If Me.Text.IndexOf(Environment.NewLine, 0, Me.SelectionStart) = -1 Then
        'no new lines before
    End If

    If Me.Text.IndexOf(Environment.NewLine, SelectionStart) = -1 Then
        'no new lines after
    End If
Tom Anderson