|
-
Feb 8th, 2011, 09:53 AM
#1
Thread Starter
Fanatic Member
Replace Characters
How can I replace spaces in a textbox with "_"?
-
Feb 8th, 2011, 10:01 AM
#2
Re: Replace Characters
In real time? Otherwise, just use Replace(Text1.Text, " ", "_")
-
Feb 8th, 2011, 10:26 AM
#3
Lively Member
Re: Replace Characters
Hello GDOG34,
Merri is right. We hope it was helpful.
-
Feb 8th, 2011, 11:25 AM
#4
Re: Replace Characters
Realtime...
vb Code:
'~~~ When the user presses a key, it's ASCII value is checked to find whether it is SPACE or not. If so, replace it with "_"'s ASCII value
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then '~~~ 32 is for SPACE character
KeyAscii = 95 '~~~ 95 is for underscore character
End If
End Sub
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Feb 8th, 2011, 12:08 PM
#5
Re: Replace Characters
akhileshbc's solution doesn't prevent pasting text with spaces from the clipboard. For that the hard thing is that text can be pasted using both mouse & keyboard, there is no general event for finding out the user is pasting text, and thus the code becomes a bit lengthy:
Code:
Option Explicit
Private m_ClipOld As String
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyV And Shift = vbCtrlMask Then
If Clipboard.GetFormat(vbCFText) Then
m_ClipOld = Clipboard.GetText
Clipboard.SetText Replace(m_ClipOld, " ", "_")
End If
End If
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyV And Shift = vbCtrlMask Then
If StrPtr(m_ClipOld) <> 0 Then
Clipboard.SetText m_ClipOld
m_ClipOld = vbNullString
End If
End If
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Clipboard.GetFormat(vbCFText) Then
m_ClipOld = Clipboard.GetText
Clipboard.SetText Replace(m_ClipOld, " ", "_")
End If
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If StrPtr(m_ClipOld) <> 0 Then
Clipboard.SetText m_ClipOld
m_ClipOld = vbNullString
End If
End Sub
And it is a bit of a hack at it. The contents of the clipboard are temporarily changed if paste is detected being pressed from the keyboard and each time mouse button is pushed down. Not very clean, but works.
-
Feb 8th, 2011, 12:11 PM
#6
Hyperactive Member
Re: Replace Characters
Above both codes are correct and this would also help u a lot
http://www.vbforums.com/showthread.php?t=636550
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|