ICreativeCrum
ICreativeCrum

Reputation: 1

Need help converting text into NATO phonetic alphabet

I am trying to convert a text in a text box into the phonetic alphabet and I am not sure how. I have TextBox1 as my main textbox, the button to convert is Button9 and I would like the output to be in Label10.

An example of what I am trying to get is someone enters Test for example into the textbox, clicks the button, and the label shows Tango Echo Sierra Tango.

If possible it would be nice to have it automatically translate while you type it in.

I have tried using some sample code I found on the internet from How to replace letters on a textbox using vb.net but it is not doing it the way I would like when translating.

(Sample of what I tried without adding in the actual phonetic alphabet)

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

Each letter was translating, and it was not showing it in the right box. I am fairly new to VB and have not done this type of thing before.

Upvotes: 0

Views: 819

Answers (1)

Martin
Martin

Reputation: 16433

There are a variety of ways to accomplish what you are trying to do.

Here is one of these. It may not be the best but it does give you an idea of how you can achieve it.

Firstly, I've defined a dictionary (Phonetic) that maps a capitalised first letter to the phonetic equivalent:

Dim Phonetic As Dictionary(Of String, String)

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    ' List of phonetic words
    Dim Words() As String = {"Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X - ray", "Yankee", "Zulu"}

    ' Create dictionary to hold words and load with words
    Phonetic = New Dictionary(Of String, String)
    For Each Word As String In Words
        Phonetic.Add(Word.Substring(0, 1), Word)
    Next
End Sub

This dictionary is a member variable so it can be accessed anywhere on the form. Make sure to define it (Dim ...) at the Form level and not inside a method or event handler.

Next, when the button is clicked, each letter of the textbox is evaluated, and the appropriate phonetic word returned from the dictionary. A space is also added. The result is trimmed (to remove the last space) and displayed in the label:

Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
    LoadOutput()
End Sub

Private Sub LoadOutput()
    If Not Phonetic Is Nothing Then
        Dim Output As String = String.Empty

        For Each Letter As String In TextBox1.Text.ToUpper()
            Output += Phonetic(Letter) + " "
        Next
        Output = Output.Trim()

        Label10.Text = Output
    End If
End Sub

When clicking the button, this will output what you are looking for. There are many other ways of accomplishing this task - this is just an example of one way you could do it.

To automatically translate the text when you type into the TextBox, just use the TextChanged event:

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    LoadOutput()
End Sub

EDIT

Following comment from OP to also handle invalid characters (including spaces).

This is achieved in LoadOutput by checking for the presence of the letter in the Dictionary. If the letter does not exist, we append it without converting. If it does, we just convert it as before and append it:

Private Sub LoadOutput()
        ...
        For Each Letter As String In TextBox1.Text.ToUpper()
            If Phonetic.ContainsKey(Letter) Then
                Output += Phonetic(Letter) + " "
            Else
                Output += Letter
            End If
        Next
        ...
End Sub

Regarding the request to convert 1 to One and so forth, that requires some additional effort. You would have to manually add these to the dictionary during Form1_Load:

Private Sub Form1_Load(...)
    ...
        Phonetic.Add(Word.Substring(0, 1), Word)
    Next
    Phonetic.Add("1", "One")
    Phonetic.Add("2", "Two")
    ... (And so on)
End Sub

Upvotes: 2

Related Questions