Home > Archive > Visual Basic Syntax > February 2005 > Question about replacing a character
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 |
Question about replacing a character
|
|
|
| Hello all,
I have a question about replacing a character, let me explain if I can. I am
capturing scanned data from a USB barcode scanner.
I have to use the USB scanner because it is the customers requirement, I
would rather use serial but the customer is always right!!!
Anyways, I am putting the data in a text field called txtScan. In this
barcode there are line feeds that I need to replace with a caret "^", so I
have a continuous string of data, then I can parse it. I think I need to do
something like this I think.
Private Sub txtScan_Change()
Dim i As Integer
For i = 1 To Len(txtScan)
txtScan = Replace(txtScan, vbLF, "^")
Next i
End Sub
Can anyone help?
Thanks
Rick Dunmire
| |
| Frank Lehmann 2005-02-23, 4:03 pm |
| txtScan.Text = Replace$(txtScan.Text, vbCrLf, "^")
No loop. Replace replaces every occurence of line feed by ^.
Try whether your scanned line feed is a vbLf or vbCrLf or what else.
Frank
"BOB" <NO@SPAM.NET> schrieb im Newsbeitrag news:#184LdbGFHA.2384@TK2MSFTNGP10.phx.gbl...
> Hello all,
>
> I have a question about replacing a character, let me explain if I can. I am
> capturing scanned data from a USB barcode scanner.
> I have to use the USB scanner because it is the customers requirement, I
> would rather use serial but the customer is always right!!!
> Anyways, I am putting the data in a text field called txtScan. In this
> barcode there are line feeds that I need to replace with a caret "^", so I
> have a continuous string of data, then I can parse it. I think I need to do
> something like this I think.
>
> Private Sub txtScan_Change()
> Dim i As Integer
> For i = 1 To Len(txtScan)
> txtScan = Replace(txtScan, vbLF, "^")
> Next i
> End Sub
>
> Can anyone help?
>
> Thanks
> Rick Dunmire
>
>
| |
| Mike D Sutton 2005-02-23, 4:03 pm |
| > I have a question about replacing a character, let me explain if I can. I am
> capturing scanned data from a USB barcode scanner.
> I have to use the USB scanner because it is the customers requirement, I
> would rather use serial but the customer is always right!!!
> Anyways, I am putting the data in a text field called txtScan. In this
> barcode there are line feeds that I need to replace with a caret "^", so I
> have a continuous string of data, then I can parse it. I think I need to do
> something like this I think.
<code snipped>
Have a look at the Replace$() function:
'***
txtScan.Text = Replace$(txtScan.Text, vbLf, "^")
'***
Depending on what format your input data is in, the line feeds may be vbCr, vbLf or vbCrLf sequences, to convert any of
these you could use something like:
'***
txtScan.Text = Replace$(Replace$(Replace$(txtScan.Text, vbCr, vbLf), vbLf & vbLf, vbLf), vbLf, "^")
'***
Hope this helps,
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/
| |
| Ken Halter 2005-02-23, 4:03 pm |
| "BOB" <NO@SPAM.NET> wrote in message
news:%23184LdbGFHA.2384@TK2MSFTNGP10.phx.gbl...
> Hello all,
>
> Private Sub txtScan_Change()
> Dim i As Integer
> For i = 1 To Len(txtScan)
> txtScan = Replace(txtScan, vbLF, "^")
> Next i
> End Sub
>
> Can anyone help?
>
> Thanks
> Rick Dunmire
Since Replace will replace all occurances of that character, you only need
to call it once. The Static variable below will prevent recursion problems
you might get when changing the text in the change event. You may not need
it... it's easy enough to get rid of <g>
'=================
Private Sub txtScan_Change()
Static bHereAlready As Boolean
If Not bHereAlready Then
bHereAlready = True
With txtScan
.Text = Replace(.Text, vbLf, "^")
'put the cursor back at the end of text
.SelStart = Len(.Text)
End With
bHereAlready = False
End If
End Sub
'=================
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
| |
|
| Thanks Guys,
I can make this work now.
Rick Dunmire
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:OVdU%23nbGFHA.2976@TK2MSFTNGP09.phx.gbl...
> "BOB" <NO@SPAM.NET> wrote in message
> news:%23184LdbGFHA.2384@TK2MSFTNGP10.phx.gbl...
>
> Since Replace will replace all occurances of that character, you only need
> to call it once. The Static variable below will prevent recursion problems
> you might get when changing the text in the change event. You may not need
> it... it's easy enough to get rid of <g>
> '=================
> Private Sub txtScan_Change()
> Static bHereAlready As Boolean
> If Not bHereAlready Then
> bHereAlready = True
> With txtScan
> .Text = Replace(.Text, vbLf, "^")
> 'put the cursor back at the end of text
> .SelStart = Len(.Text)
> End With
> bHereAlready = False
> End If
> End Sub
> '=================
>
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> Please keep all discussions in the groups..
>
| |
| Russ Holsclaw 2005-02-25, 4:03 pm |
| "BOB" <NO@SPAM.NET> wrote in message
news:%23quxdFfGFHA.3076@tk2msftngp13.phx.gbl...
> Thanks Guys,
>
> I can make this work now.
>
> Rick Dunmire
Actually No, Rick, because everyone who answered your original post missed
the real problem. The problem is that a regular Textbox does not accept a
linefeed character, and won't put it into the string in the Text property
(unless you make it a multi-line textbox.) To do the replacement, you need
to catch the linefeed as soon as it's entered and tranlate it there, using
the KeyPress event, like this:
Private Sub txtScan_KeyPress(KeyAscii As Integer)
If KeyAscii = 10 Then KeyAscii = Asc("^")
End Sub
This one actually works...try it.
Russ
| |
| Rick Rothstein 2005-02-25, 4:03 pm |
| > ..... The problem is that a regular Textbox does not accept a
> linefeed character, and won't put it into the string in the Text
> property (unless you make it a multi-line textbox.)
Are you sure about that? I've not worked with USB or Serial inputs
myself, but this "simulation" appears to work fine (with or without
MultiLine set to True)
Private Sub Command1_Click()
Text1.Text = "Hello" & vbLf & "There"
End Sub
Private Sub Text1_Change()
Text1.Text = Replace(Text1.Text, vbLf, "^")
End Sub
Is there something about USB or Serial inputs that would change this?
Rick - MVP
| |
| Russ Holsclaw 2005-02-25, 4:03 pm |
| >> ..... The problem is that a regular Textbox does not accept a
>
> Are you sure about that? I've not worked with USB or Serial inputs
> myself, but this "simulation" appears to work fine (with or without
> MultiLine set to True)
>
> Private Sub Command1_Click()
> Text1.Text = "Hello" & vbLf & "There"
> End Sub
>
> Private Sub Text1_Change()
> Text1.Text = Replace(Text1.Text, vbLf, "^")
> End Sub
I assume he's talking about one of those bar-code scanners that simulates
keyboard input. I believe they call it a "wedge"-type device. It's a pretty
common form of scanner-to-PC interface, even though it's largely a pain in
the ass. Then, you have to make sure that something like a textbox has
focus when the user scans something with the scanner. Yuck!
If it were a serial port device, he could accept input using the MSComm
object or other method of serial-device input. Your program can *own* a COM
port, but you can't really *own* the keyboard. But many barcode scanners
simulate keyboard input, which I assume is why he's looking for the input
in a textbox.
I never said you couldn't *set* the string with a linefeed into a textbox,
but the control won't let you *type* one (unless, as I said, it's
multiline, in which case it becomes a CRLF-sequence.)
| |
| Rick Rothstein 2005-02-25, 4:03 pm |
| > >> ..... The problem is that a regular Textbox does not accept a
>
> I assume he's talking about one of those bar-code scanners that
simulates
> keyboard input. I believe they call it a "wedge"-type device. It's a
pretty
> common form of scanner-to-PC interface, even though it's largely a
pain in
> the ass. Then, you have to make sure that something like a textbox has
> focus when the user scans something with the scanner. Yuck!
>
> If it were a serial port device, he could accept input using the
MSComm
> object or other method of serial-device input. Your program can *own*
a COM
> port, but you can't really *own* the keyboard. But many barcode
scanners
> simulate keyboard input, which I assume is why he's looking for the
input
> in a textbox.
>
> I never said you couldn't *set* the string with a linefeed into a
textbox,
> but the control won't let you *type* one (unless, as I said, it's
> multiline, in which case it becomes a CRLF-sequence.)
Okay, I see what you are talking about now. Thanks for the
clarification.
Rick
| |
| Jeff Johnson [MVP: VB] 2005-02-25, 4:03 pm |
|
"Russ Holsclaw" <russ@holsclaw.nyet> wrote in message
news:%23FI83R2GFHA.1172@TK2MSFTNGP12.phx.gbl...
> I never said you couldn't *set* the string with a linefeed into a textbox,
> but the control won't let you *type* one (unless, as I said, it's
> multiline, in which case it becomes a CRLF-sequence.)
Actually, what you said was:
[color=darkred]
Personally, I don't automatically replace the word "accept" with "allow you
to type."
| |
| Russ Holsclaw 2005-02-25, 9:01 pm |
| >>>> ..... The problem is that a regular Textbox does not accept a
>
> Personally, I don't automatically replace the word "accept" with "allow
> you to type."
And (personally) I don't either. I try to interpret words within the
context of the discussion in which they are used, rather than in isolation.
In this case, it seemed obvious to me that Rick was using the textbox as a
means of receiving keyboard input, rather than as an inefficient substitute
for a String variable. So, in this context, "accept" means, specifically
"accept from the keyboard (or virtual equivalent)".
But, alas!, human languages are notoriously ambiguous at times, and we
don't always choose our words carefully enough, do we? Perhaps, also, I
should have said "...I never *meant* to say...", which is what I meant to
say, anyway, if you follow me...
Would that have pleased you more?
[Egad! I just noticed how many NG's this is crossposted to... maybe it's
time to cut this thread short.]
|
|
|
|
|