For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > January 2006 > Carriage return removal









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Carriage return removal
David Clifford

2006-01-09, 10:46 pm

A quick question.
I have an input text box set to multiline, how can I prevent a user entering
a series of carriage returns into the box before typing text?

LTrim$ only strips out the spaces, I want to strip out any leading carriage
returns.

Thank you in advance

Best regards
David Clifford


David Clifford

2006-01-09, 10:46 pm

DOH!!

Fixed it.

Best regards

David

"David Clifford" <sarpg939tNoSpam@SomeISP.com> wrote in message
news:ev2s4bsBGHA.3896@TK2MSFTNGP09.phx.gbl...
> A quick question.
> I have an input text box set to multiline, how can I prevent a user

entering
> a series of carriage returns into the box before typing text?
>
> LTrim$ only strips out the spaces, I want to strip out any leading

carriage
> returns.
>
> Thank you in advance
>
> Best regards
> David Clifford
>
>



Michael Cole

2006-01-09, 10:46 pm

> "David Clifford" <sarpg939tNoSpam@SomeISP.com> wrote in message
> news:ev2s4bsBGHA.3896@TK2MSFTNGP09.phx.gbl...

David Clifford wrote:[color=darkred]
> DOH!!
>
> Fixed it.


Well don't be shy - tell us how?


--
Regards,

Michael Cole


David Clifford

2006-01-09, 10:46 pm

Sorry...not one to be shy!!

I prevent the user entering a carriage return if the textbox contains no
text, that is, it is empty to start with. Thereafter, they can add as many
as they want:

Private Sub Text1_KeyPress(KeyAscii As Integer)

If Len(LTrim$(Text1)) < 1 Then
'Don't allow ENTER Key to be pressed if no text
Select Case KeyAscii
Case vbKeyReturn
KeyAscii = 0
Beep
Case Else
End Select

End If

End Sub

There is probably a 'better' way, but this serves my purpose.

Best regards

David Clifford

"Michael Cole" <noone@hansen.com> wrote in message
news:e2r9xE1BGHA.3604@TK2MSFTNGP09.phx.gbl...
>
> David Clifford wrote:
>
> Well don't be shy - tell us how?
>
>
> --
> Regards,
>
> Michael Cole
>
>



Bob Butler

2006-01-09, 10:46 pm

"David Clifford" <sarpg939tNoSpam@SomeISP.com> wrote in message
news:uc2CwN9BGHA.1312@TK2MSFTNGP09.phx.gbl
> Sorry...not one to be shy!!
>
> I prevent the user entering a carriage return if the textbox contains
> no text, that is, it is empty to start with. Thereafter, they can add
> as many as they want:
>


You may still have to deal with the user pasting text that starts with CR or
LF

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

Rick Rothstein [MVP - Visual Basic]

2006-01-09, 10:46 pm

> You may still have to deal with the user pasting
> text that starts with CR or LF


....or if the user types in one or more characters, then a bunch of "newline"
characters (CR followed by LF) and then goes back and deletes the original
one or more characters leaving only the leading "newline" characters.

Rick


Rick Rothstein [MVP - Visual Basic]

2006-01-09, 10:46 pm

> > You may still have to deal with the user pasting
>
> ...or if the user types in one or more characters, then a bunch of

"newline"
> characters (CR followed by LF) and then goes back and deletes the original
> one or more characters leaving only the leading "newline" characters.


Actually, in looking at how the OP's code solution is implemented, nothing
stops the user from typing in single character, hitting Backspace and then
hitting the Enter key any number of times.

Rick


Rick Rothstein [MVP - Visual Basic]

2006-01-09, 10:46 pm

> I prevent the user entering a carriage return if the textbox contains no
> text, that is, it is empty to start with. Thereafter, they can add as many
> as they want:
>
> Private Sub Text1_KeyPress(KeyAscii As Integer)
>
> If Len(LTrim$(Text1)) < 1 Then
> 'Don't allow ENTER Key to be pressed if no text
> Select Case KeyAscii
> Case vbKeyReturn
> KeyAscii = 0
> Beep
> Case Else
> End Select
>
> End If
>
> End Sub


As Bob and I point out later on in this thread, there are "problems" with
using the above approach. You might want to give this code a try instead.
Remove your KeyPress event and copy/paste all of the following into your
form's code window.

'**********************Start of Code**********************
Dim LastPosition As Long

Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With Text1
If .Text Like vbNewLine & "*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
'**********************End of Code**********************

Rick


David Clifford

2006-01-09, 10:46 pm

Thanks Rick

I will give that a whirl after the holidays. I didn't really think of the
C&P and the backspace thing.
Thanks for your time and efforts in posting this solution.

Best regards

David Clifford

"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:eg27Oq9BGHA.4016@TK2MSFTNGP11.phx.gbl...
many[color=darkred]
>
> As Bob and I point out later on in this thread, there are "problems" with
> using the above approach. You might want to give this code a try instead.
> Remove your KeyPress event and copy/paste all of the following into your
> form's code window.
>
> '**********************Start of Code**********************
> Dim LastPosition As Long
>
> Private Sub Text1_Change()
> Static LastText As String
> Static SecondTime As Boolean
> If Not SecondTime Then
> With Text1
> If .Text Like vbNewLine & "*" Then
> Beep
> SecondTime = True
> .Text = LastText
> .SelStart = LastPosition
> Else
> LastText = .Text
> End If
> End With
> End If
> SecondTime = False
> End Sub
>
> Private Sub Text1_MouseDown(Button As Integer, _
> Shift As Integer, X As Single, Y As Single)
> With Text1
> LastPosition = .SelStart
> 'Place any other MouseDown event code here
> End With
> End Sub
>
> Private Sub Text1_KeyPress(KeyAscii As Integer)
> With Text1
> LastPosition = .SelStart
> 'Place any other KeyPress checking code here
> End With
> End Sub
> '**********************End of Code**********************
>
> Rick
>
>



David Clifford

2006-01-09, 10:46 pm

Sorry to sound stupid Rick...but this line:


What does the "*" mean please?

Thank you

David Clifford

"David Clifford" <sarpg939tNoSpam@SomeISP.com> wrote in message
news:eh3FAfkCGHA.4004@tk2msftngp13.phx.gbl...[color=darkred]
> Thanks Rick
>
> I will give that a whirl after the holidays. I didn't really think of the
> C&P and the backspace thing.
> Thanks for your time and efforts in posting this solution.
>
> Best regards
>
> David Clifford
>
> "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
> wrote in message news:eg27Oq9BGHA.4016@TK2MSFTNGP11.phx.gbl...
no[color=darkred]
> many
with[color=darkred]
instead.[color=darkred]
>
>



Rick Rothstein [MVP - Visual Basic]

2006-01-09, 10:46 pm

> Sorry to sound stupid Rick...but this line:
>
>
> What does the "*" mean please?


You will never sound stupid to me when asking for clarification on how
something works. The syntax of that line comes about from the use of the
Like operator. The Like operator is used to compare String values and is a
**very** weak (but still quite useful) cousin of a Regular Expression
Parser. The operator compares the String on the left side to a patterned
String value on the right side. There are a few ways you can build patterns,
so you should look up the Like Operator in your VB help files. For your
direct question, the asterisk is the "match zero or more characters"
metacharacter (a metacharacter is a character which has a functionality
other than displaying its normal text value). The logical test being
performed in the above If-Then test asks whether the text contained in the
TextBox's Text property "looks like" a newline character sequence followed
by zero or more text characters. That means the expression will be True if
the first two characters (first two because there are no characters or
metacharacters in front of it) in the Text property are a Carriage Return
followed immediately be a Line Feed no matter whether those are followed by
any other characters or not.

Rick


David Clifford

2006-01-09, 10:46 pm

Thank you for that detailed reply Rick. I now understand the code.

I really appreciate your time and effort.

Best regards

David Clifford

"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:#Sul1QvCGHA.2704@TK2MSFTNGP11.phx.gbl...
>
> You will never sound stupid to me when asking for clarification on how
> something works. The syntax of that line comes about from the use of the
> Like operator. The Like operator is used to compare String values and is a
> **very** weak (but still quite useful) cousin of a Regular Expression
> Parser. The operator compares the String on the left side to a patterned
> String value on the right side. There are a few ways you can build

patterns,
> so you should look up the Like Operator in your VB help files. For your
> direct question, the asterisk is the "match zero or more characters"
> metacharacter (a metacharacter is a character which has a functionality
> other than displaying its normal text value). The logical test being
> performed in the above If-Then test asks whether the text contained in the
> TextBox's Text property "looks like" a newline character sequence followed
> by zero or more text characters. That means the expression will be True if
> the first two characters (first two because there are no characters or
> metacharacters in front of it) in the Text property are a Carriage Return
> followed immediately be a Line Feed no matter whether those are followed

by
> any other characters or not.
>
> Rick
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com