Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Down Then
movelineup()
ElseIf e.KeyCode = Keys.Up Then
movelinedown()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
What I have tried:
now what should i Right in the function so it can work with a multi line text box
Code borrowed but can't get to work:
Sub CopyLine(ByVal movingDown As Boolean)
DTE.UndoContext.Open("CopyLine")
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
Dim topPoint As TextPoint = objSel.TopPoint
Dim bottomPoint As TextPoint = objSel.BottomPoint
Dim lTopLine As Long = topPoint.Line
Dim lTopColumn As Long = topPoint.LineCharOffset
Dim lBottomLine As Long = bottomPoint.Line
Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
Dim readLine As Long = lTopLine
Dim endLine As Long = lBottomLine + 1
Dim selectionPresent As Boolean = ((lTopLine <> lBottomLine) Or (lTopColumn <> lBottomColumn))
If (selectionPresent And (lBottomColumn = 1)) Then
endLine = lBottomLine
End If
Dim verticalOffset As Integer = 0
If (movingDown) Then
verticalOffset = endLine - lTopLine
End If
Dim insertLine As Long = endLine
While (readLine < endLine)
objSel.MoveToLineAndOffset(readLine, 1)
objSel.EndOfLine(True)
Dim lineTxt As String = objSel.Text.Clone
objSel.MoveToLineAndOffset(insertLine, 1)
objSel.Insert(lineTxt)
objSel.NewLine()
readLine = readLine + 1
insertLine = insertLine + 1
End While
objSel.MoveToLineAndOffset(lBottomLine + verticalOffset, lBottomColumn)
objSel.MoveToLineAndOffset(lTopLine + verticalOffset, lTopColumn, True)
DTE.UndoContext.Close()
End Sub
Sub CopyLineDown()
CopyLine(True)
End Sub
Sub CopyLineUp()
CopyLine(False)
End Sub
Sub MoveLineUp()
DTE.UndoContext.Open("MoveLineUp")
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
Dim topPoint As TextPoint = objSel.TopPoint
Dim bottomPoint As TextPoint = objSel.BottomPoint
Dim lTopLine As Long = topPoint.Line
Dim lTopColumn As Long = topPoint.LineCharOffset
Dim lBottomLine As Long = bottomPoint.Line
Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
Dim textLineAbove As TextSelection = DTE.ActiveDocument.Selection
textLineAbove.MoveToLineAndOffset(lTopLine - 1, 1, False)
textLineAbove.MoveToLineAndOffset(lTopLine, 1, True)
Dim indentChange As Integer = CountIndentations(textLineAbove.Text) * -1
Dim lEffectiveBottomLine = lBottomLine
If ((lBottomColumn = 1) And (lBottomLine <> lTopLine)) Then
lEffectiveBottomLine = lBottomLine - 1
End If
objSel.MoveToLineAndOffset(lTopLine - 1, 1)
Do
DTE.ExecuteCommand("Edit.LineTranspose")
Loop Until (objSel.BottomPoint.Line >= lEffectiveBottomLine)
lTopLine = lTopLine - 1
lBottomLine = lBottomLine - 1
IndentBlockAndRestoreSelection(objSel, lBottomLine, lBottomColumn, lTopLine, lTopColumn, indentChange)
DTE.UndoContext.Close()
End Sub
Sub MoveLineDown()
DTE.UndoContext.Open("MoveLineDown")
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
Dim topPoint As TextPoint = objSel.TopPoint
Dim bottomPoint As TextPoint = objSel.BottomPoint
Dim lTopLine As Long = topPoint.Line
Dim lTopColumn As Long = topPoint.LineCharOffset
Dim lBottomLine As Long = bottomPoint.Line
Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
Dim lEffectiveBottomLine = lBottomLine
If ((lBottomColumn = 1) And (lBottomLine <> lTopLine)) Then
lEffectiveBottomLine = lBottomLine - 1
End If
Dim textLineBelow As TextSelection = DTE.ActiveDocument.Selection
textLineBelow.MoveToLineAndOffset(lEffectiveBottomLine + 1, 1, False)
textLineBelow.MoveToLineAndOffset(lEffectiveBottomLine + 2, 1, True)
Dim indentChange As Integer = CountIndentations(textLineBelow.Text)
objSel.MoveToLineAndOffset(lEffectiveBottomLine, 1)
Dim lineCount As Long = lEffectiveBottomLine - lTopLine
Do
DTE.ExecuteCommand("Edit.LineTranspose")
objSel.LineUp(False, 2)
lineCount = lineCount - 1
Loop Until (lineCount < 0)
lTopLine = lTopLine + 1
lBottomLine = lBottomLine + 1
IndentBlockAndRestoreSelection(objSel, lBottomLine, lBottomColumn, lTopLine, lTopColumn, indentChange)
DTE.UndoContext.Close()
End Sub
Sub IndentBlockAndRestoreSelection(ByVal objSel As TextSelection, ByVal lBottomLine As Long, ByVal lBottomColumn As Long, ByVal lTopLine As Long, ByVal lTopColumn As Long, ByVal indentChange As Integer)
objSel.MoveToLineAndOffset(lBottomLine, lBottomColumn)
objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
If (indentChange = 0) Then
Return
End If
If (lBottomLine = lTopLine) Then
If (indentChange > 0) Then
objSel.StartOfLine()
Else
objSel.StartOfLine()
objSel.WordRight()
End If
End If
objSel.Indent(indentChange)
Dim lNewBottomColumn As Long = (lBottomColumn + indentChange)
Dim lNewTopColumn As Long = (lTopColumn + indentChange)
If ((lNewBottomColumn < 2) Or (lBottomColumn = 1)) Then
If ((lTopLine = lBottomLine) Or (lBottomColumn = 1)) Then
lNewBottomColumn = 1
Else
lNewBottomColumn = 2
End If
End If
If ((lNewTopColumn < 2) Or (lTopColumn = 1)) Then
lNewTopColumn = 1
End If
objSel.MoveToLineAndOffset(lBottomLine, lNewBottomColumn)
objSel.MoveToLineAndOffset(lTopLine, lNewTopColumn, True)
End Sub
Function CountIndentations(ByVal text As String) As Integer
Dim indent As Integer = 0
While (Text.Length > 0)
If (Text.StartsWith("//")) Then
Dim endOfLine As Integer = Text.IndexOf("\n", 2)
If (Equals(endOfLine, -1)) Then
Exit While
End If
Text = Text.Substring(endOfLine + 1)
End If
If (Text.StartsWith("/*")) Then
Dim endComment As Integer = Text.IndexOf("*/", 2)
If (Equals(endComment, -1)) Then
Exit While
End If
Text = Text.Substring(endComment + 1)
End If
If (Text.StartsWith("{")) Then
indent = indent + 1
Else
If (Text.StartsWith("}")) Then
indent = indent - 1
End If
End If
Text = Text.Substring(1)
End While
Return indent
End Function