> I am looking for a text control (or API) that can emulate the behavior
of CodeWrite where you can move the cursor in any direction on a blank
text area and begin typing (so you are not confined to the left
margin). Visual Basic used to be like this in Versions prior to 5.
I can't for the life of me figure out what to call this "feature" so
it has been hard to search for.
Any help would be appreciated
I think the feature name you are thinking of is something like "free-form
text entry".
I've never seen CodeWrite, so I don't know how it operates; however, I've
taken a guess. First and foremost, for this to make any sense in operation,
you will have to use a fixed-width font so up/down arrows take you to a
predictable location. I'd suggest you use something like Courier or Courier
New (which looks smoother to me). Since you can put the cursor anywhere
within the TextBox, I figured it should operate in TypeOver (as opposed to
Insert) Mode, hitting return would just take you to the beginning of the
next line (lines wouldn't be split), selections are replaced in place with
the characters after it remaining in place, and so on.
Here is some **rough** code that will give you an idea to get you started
(you'll want to play with it, I'm sure). Place a TextBox on the form and set
the Font to "Courier New" and pick a FontSize you like (do these at
design-time only). Then paste this code into the form's code window. That
should be all you need to do; just run the program and try it out.
Rick - MVP
Dim CharsWide As Long
Dim LinesHigh As Long
Private Sub Form_Load()
Dim CharWidth As Long
Dim CharHeight As Long
With Text1
Set Me.Font = .Font
CharWidth = Me.TextWidth("X")
CharHeight = Me.TextHeight("X")
CharsWide = .Width \ CharWidth
LinesHigh = .Height \ CharHeight
For x = 1 To LinesHigh
.SelText = String$(CharsWide, " ")
Next
.MaxLength = CharsWide * LinesHigh
End With
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim TextFromClipBoard As String
Dim CurrentSelStart As Long
With Text1
If KeyCode = vbKeyDelete Then
If .SelLength = 0 Then .SelLength = 1
CurrentSelStart = .SelStart
.SelText = String(.SelLength, " ")
.SelStart = CurrentSelStart
KeyCode = 0
ElseIf KeyCode = vbKeyBack Then
.SelLength = 0
ElseIf (Shift = vbShiftMask And KeyCode = vbKeyInsert) Or _
(Shift = vbCtrlMask And KeyCode = vbKeyV) Then
CurrentSelStart = .SelStart
TextFromClipBoard = Clipboard.GetText
.SelText = String(.SelLength, " ")
.SelStart = CurrentSelStart
.SelLength = Len(TextFromClipBoard)
.SelText = TextFromClipBoard
KeyCode = 0
End If
End With
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
If KeyAscii = 13 Then
If .SelStart \ CharsWide < LinesHigh - 1 Then
SendKeys "{DOWN}{HOME}"
End If
KeyAscii = 0
ElseIf KeyAscii = 8 Then
If .SelStart Then
.SelStart = .SelStart - 1
.SelLength = 1
.SelText = " "
.SelStart = .SelStart - 1
End If
KeyAscii = 0
ElseIf KeyAscii > 31 Then
.SelLength = 1
End If
End With
End Sub