vb.NET - 将我的控制台应用程序代码转移到Windows窗体,我需要一些调整来显示结果

时间:2015-09-26 16:39:10

标签: vb.net console-application

我已经在我的大学项目的控制台应用程序中编写了一些程序,但现在我需要使用Windows窗体来正确显示我的结果。我转移了我的控制台代码,取出console.writeline(s)console.readline(s),而不是使用以下方法保存文本框中的输入:

Dim SoundexString As String = StringInput.Text

无论如何,我的代码在控制台中输出正常,我已经设法使用' messagebox.show(xxx)和' textbox.text = xxx'在Windows窗体中输出其他内容。出于某种原因,我目前的代码只是输出了最终变量" SoundexCode" ...我将不胜感激任何建议。代码如下:

Public Class Form1

Public Sub ButtonConvert_Click(sender As Object, e As EventArgs) Handles ButtonConvert.Click

    Dim SoundexString As String = StringInput.Text

    Dim SoundexCode(5) As Char
    For i = 1 To 5
        SoundexCode(i) = "0"
    Next

    GetSoundex(SoundexString, SoundexCode)
    MessageBox.Show(SoundexCode)


End Sub

Function GetSoundex(ByVal SoundexString As String, ByRef SoundexCode() As Char)

    Dim StringLength As Integer = Len(SoundexString)
    Dim LetterArray(StringLength) As Char

    'Soundex arrays to check each rule before creating soundex code
    Dim SoundexNumbers(StringLength) As Char
    Dim DoubleLetters(StringLength) As Char
    Dim ConsonantVowel(StringLength) As Char

    'Assigning Number Locations to Individual Letters, Each Letter is in Correct Position
    For i = 1 To StringLength
        LetterArray(i) = CChar(SoundexString(i - 1))
    Next

    '1. Soundex Letters into Numbers
    GetSoundexNumbers(LetterArray, SoundexNumbers, StringLength)

    '2. Names with Double Letters / Double Soundex Numbers
    RemoveDuplicateLetters(LetterArray, DoubleLetters, StringLength, SoundexNumbers)

    '3. Consonant Vowel Seperation
    SeperateConsonantVowel(LetterArray, StringLength, SoundexNumbers, ConsonantVowel)

    '4. Creating Soundex 
    GetSoundexCode(LetterArray, StringLength, SoundexNumbers, DoubleLetters, ConsonantVowel, SoundexCode)

    Return SoundexCode

End Function

Function GetSoundexNumbers(ByVal LetterArray() As Char, ByRef SoundexNumbers() As Char, ByVal StringLength As Integer)

    For i = 1 To StringLength
        SoundexNumbers(i) = LetterArray(i)
    Next

    For i = 1 To StringLength
        Select Case SoundexNumbers(i)
            Case "b", "f", "p", "v", "B", "F", "P", "V"
                SoundexNumbers(i) = "1"
            Case "c", "g", "j", "k", "q", "s", "x", "z", "C", "G", "J", "K", "Q", "S", "X", "Z"
                SoundexNumbers(i) = "2"
            Case "d", "t", "D", "T"
                SoundexNumbers(i) = "3"
            Case "l", "L"
                SoundexNumbers(i) = "4"
            Case "m", "M", "n", "N"
                SoundexNumbers(i) = "5"
            Case "r", "R"
                SoundexNumbers(i) = "6"
                'Unwanted Cases - Vowels / Phonetic Vowels
            Case "a", "e", "i", "o", "u", "h", "w", "y", "A", "E", "I", "O", "U", "H", "W", "Y"
                SoundexNumbers(i) = "!"
        End Select
    Next

    Return SoundexNumbers

End Function

Function RemoveDuplicateLetters(ByVal LetterArray() As Char, ByRef DoubleLetters() As Char, ByVal StringLength As Integer, ByVal SoundexNumbers() As Char)

    For i = 1 To StringLength
        DoubleLetters(i) = LetterArray(i)
    Next

    'Checking Double Letters

    For i = 1 To StringLength
        If i < StringLength Then
            If DoubleLetters(i) = DoubleLetters(i + 1) Then
                DoubleLetters(i + 1) = "!"
            End If
        End If
    Next

    'Checking Double Soundex Numbers
    For i = 1 To StringLength
        If i < StringLength Then
            If SoundexNumbers(i) = SoundexNumbers(i + 1) Then
                DoubleLetters(i + 1) = "!"
            End If
        End If
    Next

    Return DoubleLetters
End Function

Function SeperateConsonantVowel(ByVal LetterArray() As Char, ByVal StringLength As Integer, ByVal SoundexNumbers() As Char, ByRef ConsonantVowel() As Char)

    For i = 1 To StringLength
        ConsonantVowel(i) = LetterArray(i)
    Next

    'Checking that a Vowel does not have Letters both Sides which Share same Soundex Number
    For i = 1 To StringLength
        If i > 1 And i < StringLength Then
            If ConsonantVowel(i) = "a" Or ConsonantVowel(i) = "e" Or ConsonantVowel(i) = "i" Or ConsonantVowel(i) = "o" Or ConsonantVowel(i) = "u" Or ConsonantVowel(i) = "A" Or ConsonantVowel(i) = "E" Or ConsonantVowel(i) = "I" Or ConsonantVowel(i) = "O" Or ConsonantVowel(i) = "U" Then
                If SoundexNumbers(i - 1) = SoundexNumbers(i + 1) Then
                    ConsonantVowel(i - 1) = "!"
                End If
            End If
        End If
    Next

    Return ConsonantVowel

End Function

Function GetSoundexCode(ByVal LetterArray() As Char, ByVal StringLength As Integer, ByVal SoundexNumbers() As Char, ByVal DoubleLetters() As Char, ByVal ConsonantVowel() As Char, ByRef SoundexCode() As Char)

    SoundexCode(1) = LetterArray(1)
    SoundexCode(2) = "-"

    Dim Counter As Integer
    Dim CounterStore As Integer

    For Counter = 2 To StringLength
        If SoundexNumbers(Counter) <> "!" And DoubleLetters(Counter) <> "!" And ConsonantVowel(Counter) <> "!" Then
            SoundexCode(3) = SoundexNumbers(Counter)
            CounterStore = Counter
            Exit For
        End If
    Next

    If CounterStore < StringLength Then
        For Counter = CounterStore + 1 To StringLength
            If SoundexNumbers(Counter) <> "!" And DoubleLetters(Counter) <> "!" And ConsonantVowel(Counter) <> "!" Then
                SoundexCode(4) = SoundexNumbers(Counter)
                CounterStore = Counter
                Exit For
            End If
        Next
    ElseIf CounterStore = StringLength Then
        SoundexCode(4) = "0"
        SoundexCode(5) = "0"
    End If

    If CounterStore < StringLength Then
        For Counter = CounterStore + 1 To StringLength
            If SoundexNumbers(Counter) <> "!" And DoubleLetters(Counter) <> "!" And ConsonantVowel(Counter) <> "!" Then
                SoundexCode(5) = SoundexNumbers(Counter)
                CounterStore = Counter
                Exit For
            End If
        Next
    ElseIf CounterStore = StringLength Then
        SoundexCode(5) = "0"
    End If

    Return SoundexCode
End Function

End Class

1 个答案:

答案 0 :(得分:0)

字符串是从0开始的数组(第一个字母从索引0开始)但是SoundexCode是一个基于1的数组(即索引0为空)所以当它转换为字符串时,SoundexCode显示为空字符串。

要修复代码,只需使用String(值为As Char(),startIndex As Integer,length as Integer)构造函数而不是常规构造函数。

即。     MessageBox.Show(New String(SoundexCode,1,5))