2015-04-19 34 views
1

上的文本框的信件我要替換使用vb.net,這是我第一次嘗試一個文本框,每個字母或數字,但它只是在一個時間如何更換使用vb.net

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    Select Case True 
     Case TextBox1.Text.Contains("a") 
      TextBox1.Text = TextBox1.Text.Replace("a", "c") 
     Case TextBox1.Text.Contains("b") 
      TextBox1.Text = TextBox1.Text.Replace("b", "d") 
     Case TextBox1.Text.Contains("c") 
      TextBox1.Text = TextBox1.Text.Replace("c", "e") 
     Case TextBox1.Text.Contains("d") 
      TextBox1.Text = TextBox1.Text.Replace("d", "f") 
     Case TextBox1.Text.Contains("e") 
      TextBox1.Text = TextBox1.Text.Replace("e", "g") 
     Case TextBox1.Text.Contains("f") 
      TextBox1.Text = TextBox1.Text.Replace("f", "h") 
     Case TextBox1.Text.Contains("g") 
      TextBox1.Text = TextBox1.Text.Replace("g", "i") 
     Case TextBox1.Text.Contains("h") 
      TextBox1.Text = TextBox1.Text.Replace("h", "j") 
     Case TextBox1.Text.Contains("i") 
      TextBox1.Text = TextBox1.Text.Replace("i", "k") 
     Case TextBox1.Text.Contains("j") 
      TextBox1.Text = TextBox1.Text.Replace("j", "l") 
     Case TextBox1.Text.Contains("k") 
      TextBox1.Text = TextBox1.Text.Replace("k", "m") 
     Case TextBox1.Text.Contains("l") 
      TextBox1.Text = TextBox1.Text.Replace("l", "n") 
     Case TextBox1.Text.Contains("m") 
      TextBox1.Text = TextBox1.Text.Replace("m", "o") 
     Case TextBox1.Text.Contains("n") 
      TextBox1.Text = TextBox1.Text.Replace("n", "p") 
     Case TextBox1.Text.Contains("o") 
      TextBox1.Text = TextBox1.Text.Replace("o", "q") 
     Case TextBox1.Text.Contains("p") 
      TextBox1.Text = TextBox1.Text.Replace("p", "r") 
     Case TextBox1.Text.Contains("q") 
      TextBox1.Text = TextBox1.Text.Replace("q", "s") 
     Case TextBox1.Text.Contains("r") 
      TextBox1.Text = TextBox1.Text.Replace("r", "t") 
     Case TextBox1.Text.Contains("s") 
      TextBox1.Text = TextBox1.Text.Replace("s", "u") 
     Case TextBox1.Text.Contains("t") 
      TextBox1.Text = TextBox1.Text.Replace("t", "v") 
     Case TextBox1.Text.Contains("u") 
      TextBox1.Text = TextBox1.Text.Replace("u", "w") 
     Case TextBox1.Text.Contains("v") 
      TextBox1.Text = TextBox1.Text.Replace("v", "x") 
     Case TextBox1.Text.Contains("w") 
      TextBox1.Text = TextBox1.Text.Replace("w", "y") 
     Case TextBox1.Text.Contains("x") 
      TextBox1.Text = TextBox1.Text.Replace("x", "z") 
     Case TextBox1.Text.Contains("y") 
      TextBox1.Text = TextBox1.Text.Replace("y", "a") 
     Case TextBox1.Text.Contains("z") 
      TextBox1.Text = TextBox1.Text.Replace("z", "b") 

    End Select 
End Sub 
替換一個字母

這不是我想要的,所以我想這個

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 

    If TextBox1.Text.Contains("a") Then 
     TextBox1.Text = TextBox1.Text.Replace("a", "c") 
    End If 

    If TextBox1.Text.Contains("b") Then 
     TextBox1.Text = TextBox1.Text.Replace("b", "d") 
    End If 

    If TextBox1.Text.Contains("c") Then 
     TextBox1.Text = TextBox1.Text.Replace("c", "e") 
    End If 

    If TextBox1.Text.Contains("d") Then 
     TextBox1.Text = TextBox1.Text.Replace("d", "f") 
    End If 

    If TextBox1.Text.Contains("e") Then 
     TextBox1.Text = TextBox1.Text.Replace("e", "g") 
    End If 

    If TextBox1.Text.Contains("f") Then 
     TextBox1.Text = TextBox1.Text.Replace("f", "h") 
    End If 

    If TextBox1.Text.Contains("g") Then 
     TextBox1.Text = TextBox1.Text.Replace("g", "i") 
    End If 

    If TextBox1.Text.Contains("h") Then 
     TextBox1.Text = TextBox1.Text.Replace("h", "j") 
    End If 

    If TextBox1.Text.Contains("i") Then 
     TextBox1.Text = TextBox1.Text.Replace("i", "k") 
    End If 

    If TextBox1.Text.Contains("j") Then 
     TextBox1.Text = TextBox1.Text.Replace("j", "l") 
    End If 

    If TextBox1.Text.Contains("k") Then 
     TextBox1.Text = TextBox1.Text.Replace("k", "m") 
    End If 

    If TextBox1.Text.Contains("l") Then 
     TextBox1.Text = TextBox1.Text.Replace("l", "n") 
    End If 

    If TextBox1.Text.Contains("m") Then 
     TextBox1.Text = TextBox1.Text.Replace("m", "o") 

    End If 

    If TextBox1.Text.Contains("n") Then 
     TextBox1.Text = TextBox1.Text.Replace("n", "p") 
    End If 

    If TextBox1.Text.Contains("o") Then 
     TextBox1.Text = TextBox1.Text.Replace("o", "q") 
    End If 

    If TextBox1.Text.Contains("p") Then 
     TextBox1.Text = TextBox1.Text.Replace("p", "r") 
    End If 

    If TextBox1.Text.Contains("q") Then 
     TextBox1.Text = TextBox1.Text.Replace("q", "s") 
    End If 

    If TextBox1.Text.Contains("r") Then 
     TextBox1.Text = TextBox1.Text.Replace("r", "t") 
    End If 

    If TextBox1.Text.Contains("s") Then 
     TextBox1.Text = TextBox1.Text.Replace("s", "u") 
    End If 

    If TextBox1.Text.Contains("t") Then 
     TextBox1.Text = TextBox1.Text.Replace("t", "v") 
    End If 

    If TextBox1.Text.Contains("u") Then 
     TextBox1.Text = TextBox1.Text.Replace("u", "w") 
    End If 

    If TextBox1.Text.Contains("v") Then 
     TextBox1.Text = TextBox1.Text.Replace("v", "x") 
    End If 

    If TextBox1.Text.Contains("w") Then 
     TextBox1.Text = TextBox1.Text.Replace("w", "y") 
    End If 

    If TextBox1.Text.Contains("x") Then 
     TextBox1.Text = TextBox1.Text.Replace("x", "z") 
    End If 

    If TextBox1.Text.Contains("y") Then 
     TextBox1.Text = TextBox1.Text.Replace("y", "a") 
    End If 

    If TextBox1.Text.Contains("z") Then 
     TextBox1.Text = TextBox1.Text.Replace("z", "b") 
    End If 


End Sub 

它也不管用,只是在時間的一封信。

我希望能夠在文本框中編寫例如「自行車」,並在此例中替換同一文本框(或其他文本框)中的文本:「dkmg」但我看不到問題出在哪裏。

+0

首先,「如果」是沒有必要的。嘗試刪除它們,只保留「替換」的行。另外,請記住,這些「ifs」區分大小寫,所以「A」不等於「a」 - 它們看起來像2個不同的字符。 – Joe

+0

一旦你用'c'代替'a',它就會被'e'取代,如果你真的想在字母表中替換掉每一個字母,那麼你就不能使用這種方法。你需要做的是循環訪問文本框中的字符串,並根據處理每個字母創建一個新的字符串 - 你可以這樣做,因爲字符串的每個字符都是一個'char',可以轉換爲'int' - 然後將結果寫回到文本框。 – stuartd

+0

@Joe我刪除所有的if,letf只替換行,但他沒有工作:s –

回答

2

要擴展Joe的答案和僞代碼,通過字符串循環的簡單For Each將允許您一次處理一個字符。最簡單的方法是使用字符的ASCII值來確定替換它的內容,但是Joe注意到您必須處理邊緣情況,因爲ASCII表包含許多符號,而不僅僅是字母,而且它們'重新按照特定的順序。

在您的發佈代碼中,您似乎將每個字母替換爲距當前字母位置(即a = c,b = d等)的對應字母爲2個空格。

對於A到Z,ASCII表使用65-90,對於a到z,使用97-122。因此,Y,Z,y和z的邊緣情況 - 例如,如果將Z添加2,則會得到|,而不是B.這是If語句可以提供幫助的地方(還有其他方法可以實現此目的,如Select)。

示例代碼說明這一點:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 

    Dim newString As StringBuilder = New StringBuilder() 

    For Each character As Char In TextBox1.Text 
     If character = "Y"c Then 
      newString.Append(Chr(65)) 
     Else If character = "Z"c Then 
      newString.Append(Chr(66)) 
     Else If character = "y"c Then 
      newString.Append(Chr(97)) 
     Else If character = "z"c Then 
      newString.Append(Chr(98)) 
     Else 
      newString.Append(Chr(Asc(character) + 2)) 
     End If 
    Next 

    TextBox1.Text = newString.ToString() 
End Sub 

非常簡單。我使用StringBuilder,因爲它可以更有效一些,具體取決於字符串的長度。另一種選擇:

Dim newString As String 

newString = newString + Chr(character) 

將導致newString新副本正在取得通過循環每次迭代 - 所以,如果你有一個10字符串,你會風與的newString 10個副本 - 每個循環。

Asc函數獲取字符的ASCII值,而Chr函數獲取由ASCII值指定的字符。字符旁邊的小寫字母c(即"A"c)只是表示它是Char,而不是String

請注意,上面的代碼將處理任何ASCII值,所以如果你只想處理字符,你需要在循環中做更多的檢查。我將把它作爲讀者的練習。

+0

它的工作,非常感謝你!我只是改變了: 「Dim newString As StringBuilder = New StringBuilder()」TO 「Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()」 –

+0

不客氣。你可以在'System.Text'的代碼開始處使用'Imports'指令,而不必在以後完全限定它。 – Tim

3

那你必須做的是:

  1. 循環遍歷其他
  2. 以後每次文本框一個的每一個字符,你拿一個字符,「處理它」(在你的情況下更換),然後你把它寫在一個單獨的字符串,有點像這樣(僞代碼):

    new_string = "" 
    for char in textbox.text: 
        # do something with char, your 'if' routines would probably work 
        new_string = new_string + char 
    
  3. 然後你新的字符串指定到文本框:

    textbox.text = new_string 
    

如果您總是希望爲每個字母添加2個字母,則可以將每個字符視爲整數。有一個例子here(看底部)。一旦你有了這個,你可以簡單地打印回字符串前加「2」,以您的字符(可能需要一些轉換,我會讓你明白這一點):

for char in textbox.text: 
     new_string = new_string + (char + 2) 

這主要工作,但是你必須自己處理邊緣情況(y,z,Y,Z)。您可以使用this chart作爲參考。

+0

我會嘗試,然後我告訴你,如果它作品。提前致謝! –

+0

我來到這個代碼 私人小組Button5_Click(發送者爲對象,例如作爲EventArgs的)把手Button5.Click 昏暗new_string作爲字符串= 「」 對於每個字符爲CHAR在TextBox1.Text new_string = new_string + CHR 下一頁 TextBox2.Text = new_string End Sub 但我不知道如何做if語句現在..你能幫我嗎? –

+0

先生,我寫了僞代碼,而不是VB(對不起,我的VB真的很老) - 你必須找到如何將其轉換爲VB代碼(或者可能是一個stackoverflow成員可以複製/編輯我的答案) – Joe

1

在這裏你去:

TextBox1.Text = (From c In TextBox1.Text.ToLower 
        Where c >= "a"c AndAlso c <= "z"c 
        Select Chr(97 + (Asc(c) - 95) Mod 26)).ToArray 
0

要失去重心後更改第一個字母爲大寫,其餘的下部:

Private Sub CapsLock(TextCaps) 
    'تغير اول حرف الي حرف كير 
    Dim fNameS As String = "" 
    Dim liS As String = "" 
    Dim lis2 As String = "" 

    For i = 1 To Len(TextCaps) 
     liS = Mid(TextCaps, i, 1) 
     If i > 1 Then 
      lis2 = Mid(TextCaps, (i - 1), 1) 
     End If 

     If i = 1 Or lis2 = " " Then 

      liS = liS.ToUpper 
      fNameS = fNameS & liS 

     Else 

      liS = liS.ToLower 

      fNameS = fNameS & liS 


     End If 
    Next 
    TextCaps2 = fNameS