none
Append a Symbol on textbox on key space pressed

    Question

  • Append a Symbol on textbox on key space pressed

    Hi Hello, I Try To Make A Search Engine Program and i need to insert % wen they press space keyboard.

    I Have Succeed To Insert The Symbol % but It Make The Empty space To After It.

    I Liked To append The Symbol With Out The Empty Space.

    Tank You In Advance

    This Is My Code

    Public Class Form2
        Public Sub New()
            Me.InitializeComponent()
            Me.KeyPreview = True
        End Sub
    
        Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
            tbSearch.AppendText("%")
            'If e.KeyCode = Keys.Space Then
            'e.SuppressKeyPress = True
            'End If
        End Sub
    
        Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.Space Then
                tbSearch.AppendText("%")
                tbSearch.Text.Trim()
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim pict = "https://i.ytimg.com/vi/" + TextBox1.Text + "/hqdefault.jpg?custom=true&w=168&h=94&stc=true&jpg444=true&jpgq=90&sp=68&sigh=6rUlsKOOsNW2_I8q7wgQJZ-z3Mw"
            PictureBox1.ImageLocation = pict
            Dim Link = "http://www.youtube.com/results?search_type=&search_query=" + TextBox1.Text + "&aq=f"
    
            ProgressBar1.Value = 0
    
            ProgressBar1.Value = 100
            Form1.TextBox1.Text = Me.TextBox1.Text
            MsgBox("Video Have Been Load And Are Ready To Play...")
            ProgressBar1.Value = 0
            Me.Close()
    
        End Sub
    
        Private Sub BackMainPageToolStripMenuItem_Click(sender As Object, e As EventArgs)
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
        End Sub
    
        Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    
        End Sub
    
        Public Function SwapClipboardHtmlText( _
        ByVal replacementHtmlText As String) As String
    
            Dim returnHtmlText As String = Nothing
    
            If (Clipboard.ContainsText(TextDataFormat.Html)) Then
                returnHtmlText = Clipboard.GetText(TextDataFormat.Html)
                Clipboard.SetText(replacementHtmlText, TextDataFormat.Html)
            End If
    
            Return returnHtmlText
            TextBox1.Text = returnHtmlText
        End Function
    
        Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
            Dim pict = "https://i.ytimg.com/vi/" + TextBox1.Text + "/hqdefault.jpg?custom=true&w=168&h=94&stc=true&jpg444=true&jpgq=90&sp=68&sigh=6rUlsKOOsNW2_I8q7wgQJZ-z3Mw"
            Me.WebBrowser2.ScriptErrorsSuppressed() = True
            Me.WebBrowser1.ScriptErrorsSuppressed() = True
            Dim info = "http://gdata.youtube.com/feeds/api/videos/" + tbSearch.Text
    
            Me.WebBrowser2.Navigate("https://www.google.pt/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=" + tbSearch.Text + "&*")
            Process.Start("chrome", "https://www.youtube.com/results?search_query=" + tbSearch.Text)
            
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Process.Start("chrome", "http://www.google.com/search?hl=en&q=" + tbSearch.Text + "&aq=f&oq=")
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            tbSearch.Select()
            MsgBox("Type Your Search Keyword And Press Search Youtube")
        End Sub
    End Class

    Wednesday, March 29, 2017 6:11 PM

All replies

  • Try executing ‘e.SuppressKeyPress = True’ instead of ‘tbSearch.Text.Trim()’.


    Wednesday, March 29, 2017 6:44 PM
  •  You can use the TextChanged event of the TextBox if you want the spaces in the TextBox itself to actually change to "%" signs.

        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Dim si As Integer = TextBox1.SelectionStart
            TextBox1.Text = TextBox1.Text.Replace(" ", "%")
            TextBox1.SelectionStart = si
        End Sub
    
     

     However,  if you want the spaces in the TextBox to stay as spaces but,  want to use a String variable with the spaces changed,  you can do the same thing using the Replace method.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SearchString As String = TextBox1.Text.Replace(" ", "%")
    
            'do the search using the SearchString....
        End Sub
    


    If you say it can`t be done then i`ll try it

    Wednesday, March 29, 2017 6:56 PM
  • Hi helder227,

    You could set e.SuppressKeyPress = True and then set

    Private Sub tbSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbsearch.KeyDown
            If e.KeyCode = Keys.Space Then
                tbsearch.AppendText("%")
                e.SuppressKeyPress = True
            End If
    End Sub
    

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 30, 2017 5:21 AM
    Moderator