Results 1 to 6 of 6

Thread: Replace Characters

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Replace Characters

    How can I replace spaces in a textbox with "_"?

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Replace Characters

    In real time? Otherwise, just use Replace(Text1.Text, " ", "_")

  3. #3
    Lively Member
    Join Date
    Nov 2007
    Location
    Ghana
    Posts
    120

    Re: Replace Characters

    Hello GDOG34,
    Merri is right. We hope it was helpful.

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Replace Characters

    Realtime...

    vb Code:
    1. '~~~ 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
    2. Private Sub Text1_KeyPress(KeyAscii As Integer)
    3.     If KeyAscii = 32 Then   '~~~ 32 is for SPACE character
    4.         KeyAscii = 95       '~~~ 95 is for underscore character
    5.     End If
    6. 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,...

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  6. #6
    Hyperactive Member
    Join Date
    Nov 2010
    Location
    Pakistan
    Posts
    289

    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
  •  



Click Here to Expand Forum to Full Width