如果文本等于某物,则缩短代码,然后替换

时间:2019-06-26 02:14:43

标签: vb.net

我做些小游戏,我只是初学者。您能帮忙简称一下吗:D 我的代码太长,如果需要更多内容,请再次编写。 谢谢。

  If QuestionBox.Text.Contains("#Q1") Then
            QuestionBox.Text = QuestionBox.Text.Replace("Q1", "")
            PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q1.jpg"))
        ElseIf QuestionBox.Text.Contains("Q2") Then
            QuestionBox.Text = QuestionBox.Text.Replace("Q2", "")
            PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q2.jpg"))
        ElseIf QuestionBox.Text.Contains("Q3") Then
            QuestionBox.Text = QuestionBox.Text.Replace("Q3", "")
            PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q3.jpg"))
        ElseIf QuestionBox.Text.Contains("Q4") Then
            QuestionBox.Text = QuestionBox.Text.Replace("Q4", "")
            PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q4.jpg"))
        ElseIf QuestionBox.Text.Contains("Q5") Then
            QuestionBox.Text = QuestionBox.Text.Replace("Q5", "")
            PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q5.jpg"))
        ElseIf QuestionBox.Text.Contains("Q6") Then
            QuestionBox.Text = QuestionBox.Text.Replace("Q6", "")
            PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q6.jpg"))
        ElseIf QuestionBox.Text.Contains("Q7") Then
            QuestionBox.Text = QuestionBox.Text.Replace("Q7", "")
            PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q7.jpg"))
        ElseIf QuestionBox.Text.Contains("Q8") Then
            QuestionBox.Text = QuestionBox.Text.Replace("Q8", "")
            PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q8.jpg"))
        End If

1 个答案:

答案 0 :(得分:0)

您应该尝试类似的操作(例如)

    Dim testString = "aaaaaQ1"

    If testString.Contains("Q") Then

        Dim numero As String = Split(testString, "Q")(1)
        QuestionBox.Text = testString.Replace("Q" & numero, "")
        PictureBox.Image = Image.FromFile(My.Application.Info.DirectoryPath + ("\Gfx\Q" & numero & ".jpg"))

    End If