2016-04-02 53 views
0

我是Visual Basic的新手。我想在textbox中使用like運算符來更改一個字符,如果它跟有任何其他字符。但它應該在key-up事件。如果後面跟着任何字符,則替換字符串visual basic

任何人都請幫助我:我如何使下面的代碼工作?

Public Class Form1 
Dim myString As String 
Dim sMatch As Boolean = myString Like "x?" 

Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp 
    If sMatch = True Then 
     TextBox1.Text = TextBox1.Text.Replace(myString, "z") 
    End If 
End Sub 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    myString = "x" 
End Sub 
End Class 

回答

0

試試這個問題。你的變量myString從來沒有從TextBox獲取它的值。您只需在load事件中將其設置爲x即可。

Public Class Form1 
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp 
    If TextBox1.Text Like "x?" Then 
     TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, "z") 
    End If 
End Sub 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    TextBox1.Text = "x" 
End Sub 
End Class 
+0

這是有效的,但只有一次。你能否建議修改來循環代碼? –

+0

它的作品每次寫「x?」時在盒子裏 !沒有必要循環。每次發生關鍵事件時,該方法都會執行。現在取決於你的要求是什麼。 –

相關問題