从“字符串”到“类型”布尔值“的转换无效

时间:2015-03-22 05:01:22

标签: vba

我最近遇到了这个问题我正在制作一个学校加密程序的问题,而且我一直试图绕过这个错误,但我似乎无法让它工作,你是什么&# 39; s thing?

    TextBox1.Text = TextBox1.Text.ToLower
    Dim input As String = TextBox1.Text
    Dim charPos As Integer = 0
    Dim Cipher As String = ""
    Dim x As Boolean
    Dim Random As Boolean

    If input = "" Then
        MsgBox("Please enter your text for encryption including only characters A-Z")
    End If

    Select Case Random
        Case 0
            Random = ComboBox1.SelectedItem.ToString()
        Case 1
            Random = "selection1"
            x = True
        Case Else
            x = False
    End Select

    If x = True Then
        Dim alphabet As String = "abcdefghij klmnopqrstuvwxyz"
        Dim code As String = "kxgtlmpqbwcnderfahjusvi yoz"

        For i = 0 To input.Length - 1

            Try
                charPos = alphabet.IndexOf(input(i))
                Cipher = Cipher & code(charPos)
            Catch ex As Exception
                MsgBox("Please enter your text for encryption including only characters A-Z")
            End Try
        Next i
        TextBox1.Text = (Cipher)
    Else

    End If
End Sub

Visual Basic中的实际错误本身位于

Random = ComboBox1.SelectedItem.ToString()

很抱歉,如果这很难理解,我是Stackoverflow和VB的新手,所以如果你不太明白它请评论:)

1 个答案:

答案 0 :(得分:0)

以下是使用您的代码的示例。

    TextBox1.Text = TextBox1.Text.ToLower
    Dim input As String = TextBox1.Text
    Dim charPos As Integer = 0
    Dim Cipher As String = ""

    If input = "" Then
        MsgBox("Please enter your text for encryption including only characters A-Z")
    End If

    If ComboBox1.SelectedIndex = 0 Then
        Dim alphabet As String = "abcdefghij klmnopqrstuvwxyz"
        Dim code As String = "kxgtlmpqbwcnderfahjusvi yoz"

        For i = 0 To input.Length - 1

            Try
                charPos = alphabet.IndexOf(input(i))
                Cipher = Cipher & code(charPos)
            Catch ex As Exception
                MsgBox("Please enter your text for encryption including only characters A-Z")
            End Try
        Next i
        TextBox1.Text = (Cipher)
    Else

    End If
End Sub