Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Want Enter to put CR in Textbox (not just CTRL-Enter)

I'm using a form with VBA and the form has several text boxes. When entering
information I need to be able to put carriage return/line feeds into the
text. However just hitting the Enter key causes the next field in the form
to become active (leaving the field that I was editing) - like TAB does. If
I want carriage return/line feed I have to type CTRL-Enter. Is there a way
to cause carriage return/line feeds to be put into a form text box using just
the Enter key? This may seem like a small thing but others use this form too
and hitting just Enter (for CR/LF) is a hard habit to break.

Thanks,
Will
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Want Enter to put CR in Textbox (not just CTRL-Enter)

In the textbox properties is EnterKeyBehavior.
Set this to True.

RBS


"roadkill" wrote in message
...
I'm using a form with VBA and the form has several text boxes. When
entering
information I need to be able to put carriage return/line feeds into the
text. However just hitting the Enter key causes the next field in the
form
to become active (leaving the field that I was editing) - like TAB does.
If
I want carriage return/line feed I have to type CTRL-Enter. Is there a
way
to cause carriage return/line feeds to be put into a form text box using
just
the Enter key? This may seem like a small thing but others use this form
too
and hitting just Enter (for CR/LF) is a hard habit to break.

Thanks,
Will


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Want Enter to put CR in Textbox (not just CTRL-Enter)

Hi Will,

Bit tricky to get vbCrLf inserted in correct position and re-setting the
cursor position if Enter is pressed in the middle of the text. Anyway have a
go with this and see how you get on

Private Declare Function GetKeyState32 Lib "user32" _
Alias "GetKeyState" (ByVal vKey As Integer) As Integer

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Long, n As Long, cnt As Long
Dim nSelPos As Long
Dim sText As String
Dim by() As Byte

If GetKeyState32(vbKeyReturn) < 0 Then '-128, enter pressed

Cancel = True ' abort update
nSelPos = TextBox1.SelStart 'cursor pos
sText = TextBox1.Text

If Len(sText) Then
' problem: vbCrLf is two characters but seems
' .SelStart does not count the Cr character
' need to count no. of Chr(13)'s before cursor
' for each chr(13) need to add an extra character count
' left of cursor, complicated 'cos need to increase length
' of the left portion of the string during the search.
' Could use Instr but I like this byte array approach!

by = sText
n = nSelPos * 2 - 1
Do While i <= n
If by(i) = 13 Then
n = n + 2
cnt = cnt + 1
End If
i = i + 1 ' 2 bytes/char, interested in every other byte
Loop

End If

sText = Left(sText, nSelPos + cnt) & _
vbCrLf & _
Mid(sText, nSelPos + 1 + cnt, Len(sText))
TextBox1.Text = sText
TextBox1.SelStart = nSelPos + 1
End If

End Sub

Don't forget to set MultiLine & WordWrap prop's to True

Regards,
Peter T


"roadkill" wrote in message
...
I'm using a form with VBA and the form has several text boxes. When

entering
information I need to be able to put carriage return/line feeds into the
text. However just hitting the Enter key causes the next field in the

form
to become active (leaving the field that I was editing) - like TAB does.

If
I want carriage return/line feed I have to type CTRL-Enter. Is there a

way
to cause carriage return/line feeds to be put into a form text box using

just
the Enter key? This may seem like a small thing but others use this form

too
and hitting just Enter (for CR/LF) is a hard habit to break.

Thanks,
Will



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Want Enter to put CR in Textbox (not just CTRL-Enter)

Just seen RBS's EnterBehaviour tip, Aaaaargh !

ignore all this below

Peter T


"Peter T" <peter_t@discussions wrote in message
...
Hi Will,

Bit tricky to get vbCrLf inserted in correct position and re-setting the
cursor position if Enter is pressed in the middle of the text. Anyway have

a
go with this and see how you get on

Private Declare Function GetKeyState32 Lib "user32" _
Alias "GetKeyState" (ByVal vKey As Integer) As Integer

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Long, n As Long, cnt As Long
Dim nSelPos As Long
Dim sText As String
Dim by() As Byte

If GetKeyState32(vbKeyReturn) < 0 Then '-128, enter pressed

Cancel = True ' abort update
nSelPos = TextBox1.SelStart 'cursor pos
sText = TextBox1.Text

If Len(sText) Then
' problem: vbCrLf is two characters but seems
' .SelStart does not count the Cr character
' need to count no. of Chr(13)'s before cursor
' for each chr(13) need to add an extra character count
' left of cursor, complicated 'cos need to increase length
' of the left portion of the string during the search.
' Could use Instr but I like this byte array approach!

by = sText
n = nSelPos * 2 - 1
Do While i <= n
If by(i) = 13 Then
n = n + 2
cnt = cnt + 1
End If
i = i + 1 ' 2 bytes/char, interested in every other byte
Loop

End If

sText = Left(sText, nSelPos + cnt) & _
vbCrLf & _
Mid(sText, nSelPos + 1 + cnt, Len(sText))
TextBox1.Text = sText
TextBox1.SelStart = nSelPos + 1
End If

End Sub

Don't forget to set MultiLine & WordWrap prop's to True

Regards,
Peter T


"roadkill" wrote in message
...
I'm using a form with VBA and the form has several text boxes. When

entering
information I need to be able to put carriage return/line feeds into the
text. However just hitting the Enter key causes the next field in the

form
to become active (leaving the field that I was editing) - like TAB does.

If
I want carriage return/line feed I have to type CTRL-Enter. Is there a

way
to cause carriage return/line feeds to be put into a form text box using

just
the Enter key? This may seem like a small thing but others use this

form
too
and hitting just Enter (for CR/LF) is a hard habit to break.

Thanks,
Will





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Want Enter to put CR in Textbox (not just CTRL-Enter)

Good exercise though.

RBS


"Peter T" <peter_t@discussions wrote in message
...
Just seen RBS's EnterBehaviour tip, Aaaaargh !

ignore all this below

Peter T


"Peter T" <peter_t@discussions wrote in message
...
Hi Will,

Bit tricky to get vbCrLf inserted in correct position and re-setting the
cursor position if Enter is pressed in the middle of the text. Anyway
have

a
go with this and see how you get on

Private Declare Function GetKeyState32 Lib "user32" _
Alias "GetKeyState" (ByVal vKey As Integer) As Integer

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Long, n As Long, cnt As Long
Dim nSelPos As Long
Dim sText As String
Dim by() As Byte

If GetKeyState32(vbKeyReturn) < 0 Then '-128, enter pressed

Cancel = True ' abort update
nSelPos = TextBox1.SelStart 'cursor pos
sText = TextBox1.Text

If Len(sText) Then
' problem: vbCrLf is two characters but seems
' .SelStart does not count the Cr character
' need to count no. of Chr(13)'s before cursor
' for each chr(13) need to add an extra character count
' left of cursor, complicated 'cos need to increase length
' of the left portion of the string during the search.
' Could use Instr but I like this byte array approach!

by = sText
n = nSelPos * 2 - 1
Do While i <= n
If by(i) = 13 Then
n = n + 2
cnt = cnt + 1
End If
i = i + 1 ' 2 bytes/char, interested in every other byte
Loop

End If

sText = Left(sText, nSelPos + cnt) & _
vbCrLf & _
Mid(sText, nSelPos + 1 + cnt, Len(sText))
TextBox1.Text = sText
TextBox1.SelStart = nSelPos + 1
End If

End Sub

Don't forget to set MultiLine & WordWrap prop's to True

Regards,
Peter T


"roadkill" wrote in message
...
I'm using a form with VBA and the form has several text boxes. When

entering
information I need to be able to put carriage return/line feeds into
the
text. However just hitting the Enter key causes the next field in the

form
to become active (leaving the field that I was editing) - like TAB
does.

If
I want carriage return/line feed I have to type CTRL-Enter. Is there a

way
to cause carriage return/line feeds to be put into a form text box
using

just
the Enter key? This may seem like a small thing but others use this

form
too
and hitting just Enter (for CR/LF) is a hard habit to break.

Thanks,
Will








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Want Enter to put CR in Textbox (not just CTRL-Enter)

:-), in a gritted teeth kind of way!

Silly really, I knew about EnterKeyBehavior and even have it in one of my
app's, but forgot...

Regards,
Peter T

"RB Smissaert" wrote in message
...
Good exercise though.

RBS



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Want Enter to put CR in Textbox (not just CTRL-Enter)

Still, it may have given somebody a good laugh.

RBS


"Peter T" <peter_t@discussions wrote in message
...
:-), in a gritted teeth kind of way!

Silly really, I knew about EnterKeyBehavior and even have it in one of my
app's, but forgot...

Regards,
Peter T

"RB Smissaert" wrote in message
...
Good exercise though.

RBS




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Want Enter to put CR in Textbox (not just CTRL-Enter)

Thanks a bunch - it worked great! And a little levity during the work day is
always appreciated, Peter.
Will

"RB Smissaert" wrote:

Still, it may have given somebody a good laugh.

RBS


"Peter T" <peter_t@discussions wrote in message
...
:-), in a gritted teeth kind of way!

Silly really, I knew about EnterKeyBehavior and even have it in one of my
app's, but forgot...

Regards,
Peter T

"RB Smissaert" wrote in message
...
Good exercise though.

RBS





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What does Ctrl+Shift+Enter do? How does it differ from Enter? George Furnell[_2_] Excel Programming 2 January 16th 06 06:43 AM
What does hitting Ctrl + Shift + Enter to enter a formula do??? Help a n00b out. qwopzxnm Excel Worksheet Functions 2 October 20th 05 09:06 PM
ctrl+shift+enter vs enter tkaplan Excel Discussion (Misc queries) 7 May 27th 05 05:10 PM
HELP! I Lost The Ability To Advance From TextBox To TextBox With the ENTER Or The TAB Keys Minitman[_4_] Excel Programming 0 February 22nd 05 08:50 PM
how to enter 'CTRL-SHIFT-ENTER' in vb Mike Molyneaux Excel Programming 4 May 5th 04 09:32 PM


All times are GMT +1. The time now is 05:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"