robertb_NZ
Well-known member
I am trying to implement a drag-and-drop into a textbox, where the text is dropped into the cursor position.
For testing I have pre-filled my textbox (called "txtScreen") with data and set its font to a fixed-pitch font (Consolas), so that it starts out looking like this: -
00....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
01....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
02....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
03....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
04....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
05....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
06....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
07....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
08....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
09....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
10....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
I have a treeview elsewhere in the window. I drag an entry from the treeview and drop it somewhere, say line 02, character "1" (=position 12 on this line). If the text passed across is "xxxx", then I want line 2 to become
02....*....xxxx.*....2....*....3....*....4....*....5....*....6....*....7....*....8
But the "xxxx" is going somewhere else.
Here is my code that is handling the DragDrop: -
Private Sub txtScreen_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtScreen.DragDrop
' Paste the text.
Dim DropPoint As Point
Dim Linenbr, CharIndex As Integer
DropPoint = New Point(e.X, e.Y)
Debug.Print(DropPoint.X & ", " & DropPoint.Y)
CharIndex = txtScreen.GetCharIndexFromPosition(DropPoint)
Linenbr = txtScreen.GetLineFromCharIndex(CharIndex)
Debug.Print("CharIndex:" & CharIndex & ", LineNbr:" & Linenbr)
Mid(txtScreen.Text, CharIndex, Len(e.Data.GetData(DataFormats.Text))) = e.Data.GetData(DataFormats.Text)
End Sub
The Debug.print diagnostics show
614, 276
CharIndex:2099, LineNbr:24
No matter what the values of DropPoint, in this series of tests CharIndex is always 2099 and LineNbr = 24. Other tests have given different results, but none have been correct. The actual text dropped is correct, it's just dropped in the wrong position.
I assume that I'm making a dumb mistake with
CharIndex = txtScreen.GetCharIndexFromPosition(DropPoint)
Can somebody tell me what I'm doing wrong?
Thank you, Robert
For testing I have pre-filled my textbox (called "txtScreen") with data and set its font to a fixed-pitch font (Consolas), so that it starts out looking like this: -
00....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
01....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
02....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
03....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
04....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
05....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
06....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
07....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
08....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
09....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
10....*....1....*....2....*....3....*....4....*....5....*....6....*....7....*....8
I have a treeview elsewhere in the window. I drag an entry from the treeview and drop it somewhere, say line 02, character "1" (=position 12 on this line). If the text passed across is "xxxx", then I want line 2 to become
02....*....xxxx.*....2....*....3....*....4....*....5....*....6....*....7....*....8
But the "xxxx" is going somewhere else.
Here is my code that is handling the DragDrop: -
Private Sub txtScreen_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtScreen.DragDrop
' Paste the text.
Dim DropPoint As Point
Dim Linenbr, CharIndex As Integer
DropPoint = New Point(e.X, e.Y)
Debug.Print(DropPoint.X & ", " & DropPoint.Y)
CharIndex = txtScreen.GetCharIndexFromPosition(DropPoint)
Linenbr = txtScreen.GetLineFromCharIndex(CharIndex)
Debug.Print("CharIndex:" & CharIndex & ", LineNbr:" & Linenbr)
Mid(txtScreen.Text, CharIndex, Len(e.Data.GetData(DataFormats.Text))) = e.Data.GetData(DataFormats.Text)
End Sub
The Debug.print diagnostics show
614, 276
CharIndex:2099, LineNbr:24
No matter what the values of DropPoint, in this series of tests CharIndex is always 2099 and LineNbr = 24. Other tests have given different results, but none have been correct. The actual text dropped is correct, it's just dropped in the wrong position.
I assume that I'm making a dumb mistake with
CharIndex = txtScreen.GetCharIndexFromPosition(DropPoint)
Can somebody tell me what I'm doing wrong?
Thank you, Robert