How to insert a '*' in certain controls when Enter is pressed.

  • Thread starter Thread starter ThomasAJ
  • Start date Start date
T

ThomasAJ

An important user wants to have a '*' inserted at the beginning of a NEW LINE
in certain controls when the ENTER key is pressed.

OK so in the Form_KeyDown event I can determine if the enter key has been
pressed, and I also know the name of the ActiveControl. The controls in
question have the NewLine property set to YES.

So how to insert a '*' at the beginning of the new line. I have tried some
silly code in the Form_KeyDown event but I think it is the wrong place to go
changing control text.

Also bear in mind that the ENTER key may have been pressed in the middle of
existing text and not just at the end.
 
An important user wants to have a '*' inserted at the beginning of a NEW LINE
in certain controls when the ENTER key is pressed.

OK so in the Form_KeyDown event I can determine if the enter key has been
pressed, and I also know the name of the ActiveControl. The controls in
question have the NewLine property set to YES.

So how to insert a '*' at the beginning of the new line. I have tried some
silly code in the Form_KeyDown event but I think it is the wrong place to go
changing control text.

Also bear in mind that the ENTER key may have been pressed in the middle of
existing text and not just at the end.

In the control's keypress event, use Replace to search for the newline
character and replace with a * followed by a new line. Don't forget to
use the Text property of the Textbox rather than the value property.

-- James
 
There could be many other NEW LINES either before or after the location of
where the NEW new line is called for. In other words the '*' must be inserted
AFTER where the NEW new line is created.

The end result is to look like:
* blah blah1
* blah blah2
* blah blah3
* blah blah4
 
Thanks for that.

Any chance of more help.

I am using very clunky code that is not working.
eg the new line is being created at the start of the text, also when the
enter key is pressed at the end of some text the code falls over.

My code is:

MiscDetails.Text = Mid$(MiscDetails, 1, MiscDetails.SelStart) & "*" &
Mid$(MiscDetails, MiscDetails.SelStart + 1, Len(MiscDetails) -
MiscDetails.SelStart + 1)

Apart from the fact it is not working I bet there is a more specific
function I can use as well.
 
Paste the function below into a module.

Then call it like this from sample text box named Text0:

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
Call StarEnter(Me.Text0, KeyCode, Shift)
End Sub

Function StarEnter(ctl As Control, KeyCode As Integer, Shift As Integer)
'Purpose: Replace Enter keystroke with CrLf and asterisk.
Dim strPrior As String 'Text before the cursor.
Dim strAfter As String 'Text after the cursor.
Dim lngLen As Long 'Number of characters
Dim iSelStart As Integer 'Where cursor is.

'Ignore keystrokes other than Enter; ignore if Shift, Alt or Ctrl
pressed.
If (KeyCode = 13) And (Shift = 0) Then
With ctl
lngLen = Len(.Text)
'SelStart can't cope with more than 32k characters.
If lngLen <= 32764 Then
'Remember characters before cursor.
iSelStart = .SelStart
If iSelStart > 1 Then
strPrior = Left$(.Text, iSelStart)
End If
'Remember characters after selection.
If iSelStart + .SelLength < lngLen Then
strAfter = Mid$(.Text, iSelStart + .SelLength + 1)
End If
'Destroy the keystroke.
KeyCode = 0
'Assign prior characters, star, enter, and later characters.
.Text = strPrior & vbCrLf & "*" & strAfter
'Put the cursor back where it as, after the CR, LF, and *.
.SelStart = iSelStart + 3
End If
End With
End If
End Function
 
Allen, I think you can get the same effect using the SelText
property:

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As
Integer)
Dim lngCursor As Long

If (KeyCode = vbKeyReturn) And (Shift = 0) Then
With Me.Text0
lngCursor = .SelStart
.SelText = vbCrLf & "*"
.SelStart = lngCursor + 3
End With
KeyCode = 0
End If
End Sub
 
Thanks, Marsh.

That's a much simpler solution for the case ThomasAJ wanted.

Where I've had problems with just setting the SelText is if the inserted
text needs to end with a space. It seems to get trimmed off if you set the
SelText (or even the Text.)
 
Allen said:
Thanks, Marsh.

That's a much simpler solution for the case ThomasAJ wanted.

Where I've had problems with just setting the SelText is if the inserted
text needs to end with a space. It seems to get trimmed off if you set the
SelText (or even the Text.)


Bummer!

A2003 only trimmed trailing spaces when the cursor was at
the end of the existing text. This would be irritating if
Thomas wanted some indenting after appending a new line.
 
Thank you very much Allen, you are incredible!

Re Marshall's solution; yes ideally I do want to insert '* ' and the space
does disappear. However I will run with Marshall for the moment and come back
to your solution when I have a bit of time soon.

Thanks again.
 
Thank you very very much. I really appreciate the effort. Thanks.
But ideally I do want to insert '* ' and the space disappears.

--
Regards
Tom


Marshall Barton said:
Allen, I think you can get the same effect using the SelText
property:

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As
Integer)
Dim lngCursor As Long

If (KeyCode = vbKeyReturn) And (Shift = 0) Then
With Me.Text0
lngCursor = .SelStart
.SelText = vbCrLf & "*"
.SelStart = lngCursor + 3
End With
KeyCode = 0
End If
End Sub
--
Marsh
MVP [MS Access]


Allen said:
Paste the function below into a module.

Then call it like this from sample text box named Text0:

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
Call StarEnter(Me.Text0, KeyCode, Shift)
End Sub

Function StarEnter(ctl As Control, KeyCode As Integer, Shift As Integer)
'Purpose: Replace Enter keystroke with CrLf and asterisk.
Dim strPrior As String 'Text before the cursor.
Dim strAfter As String 'Text after the cursor.
Dim lngLen As Long 'Number of characters
Dim iSelStart As Integer 'Where cursor is.

'Ignore keystrokes other than Enter; ignore if Shift, Alt or Ctrl
pressed.
If (KeyCode = 13) And (Shift = 0) Then
With ctl
lngLen = Len(.Text)
'SelStart can't cope with more than 32k characters.
If lngLen <= 32764 Then
'Remember characters before cursor.
iSelStart = .SelStart
If iSelStart > 1 Then
strPrior = Left$(.Text, iSelStart)
End If
'Remember characters after selection.
If iSelStart + .SelLength < lngLen Then
strAfter = Mid$(.Text, iSelStart + .SelLength + 1)
End If
'Destroy the keystroke.
KeyCode = 0
'Assign prior characters, star, enter, and later characters.
.Text = strPrior & vbCrLf & "*" & strAfter
'Put the cursor back where it as, after the CR, LF, and *.
.SelStart = iSelStart + 3
End If
End With
End If
End Function
 
Yes it's me again.

I tried your code Allen and it works except when the enter key is pressed
when the cursor is after the last character AND when '* ' (space after the *)
is inserted. I have slightly changed your code in order to add a space after
the '*' thus:

'Assign prior characters, star, enter, and later characters.
.Text = strPrior & vbCrLf & "* " & strAfter
'Put the cursor back where it as, after the CR, LF, and *.
.SelStart = iSelStart + 4
 
Back
Top