2012-02-18 62 views
1

我正在构建一个随机生成一个数字的程序,然后将这个数字链接到一个图片,并显示在图片框中。我有一个在它下面有一个文本框,其中一个图片是可变的,这取决于这个文本框所说的,所以我使用了一些if和else语句。但是我得到这个错误,'Else'必须在前面匹配'If'或'ElseIf'。可能有一个简单的解决方案,我只是没有看到,这里是代码。If和其他语句在Visual Basic中的问题

Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Timer1.Start() 
    Dim key As Integer 
    key = (Rnd() * 3) 
    Select Case key 
     Case 1 
      Label14.Text = "Assault" 
     Case 2 
      Label14.Text = "Support" 
     Case 3 
      Label14.Text = "Specialist" 
    End Select 
    If Label14.Text = "Assault" Then 
     Timer1.Start() 
     Dim key1 As Integer 
     key1 = (Rnd() * 15) 
     Select Case key1 
      Case 1 
       PictureBox10.Image = My.Resources.assault_1 
       TextBox7.Text = "AC130" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "11" 
       Else TextBox8.Text = "12" 
      Case 2 
       PictureBox10.Image = My.Resources.assault_2 
       TextBox7.Text = "Care Package" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "3" 
       Else TextBox8.Text = "4" 
      Case 3 
       PictureBox10.Image = My.Resources.assault_3 
       TextBox7.Text = "Juggernaut" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "14" 
       Else TextBox8.Text = "15" 
      Case 4 
       PictureBox10.Image = My.Resources.assault_4 
       TextBox7.Text = "Attack Helicopter" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "6" 
       Else TextBox8.Text = "7" 
      Case 5 
       PictureBox10.Image = My.Resources.assault_5 
       TextBox7.Text = "Pave Low" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "11" 
       Else TextBox8.Text = "12" 
      Case 6 
       PictureBox10.Image = My.Resources.assault_6 
       TextBox7.Text = "IMS" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "4" 
       Else TextBox8.Text = "5" 
      Case 7 
       PictureBox10.Image = My.Resources.assault_7 
       TextBox7.Text = "Assault Drone" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "9" 
       Else TextBox8.Text = "10" 
      Case 8 
       PictureBox10.Image = My.Resources.assault_8 
       TextBox7.Text = "Strafe Run" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "8" 
       Else TextBox8.Text = "9" 
      Case 9 
       PictureBox10.Image = My.Resources.assault_9 
       TextBox7.Text = "AH-6 Overwatch" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "8" 
       Else TextBox8.Text = "9" 
      Case 10 
       PictureBox10.Image = My.Resources.assault_10 
       TextBox7.Text = "Osprey Gunner" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "16" 
       Else TextBox8.Text = "15" 
      Case 11 
       PictureBox10.Image = My.Resources.assault_11 
       TextBox7.Text = "Percision Airstrike" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "5" 
       Else TextBox8.Text = "6" 
      Case 12 
       PictureBox10.Image = My.Resources.assault_12 
       TextBox7.Text = "Predator Missile" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "4" 
       Else TextBox8.Text = "5" 
      Case 13 
       PictureBox10.Image = My.Resources.assault_13 
       TextBox7.Text = "Reaper" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "8" 
       Else TextBox8.Text = "9" 
      Case 14 
       PictureBox10.Image = My.Resources.assault_14 
       TextBox7.Text = "Sentry Gun" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "4" 
       Else TextBox8.Text = "5" 
      Case 15 
       PictureBox10.Image = My.Resources.assault_15 
       TextBox7.Text = "Assault UAV" 
       If TextBox14.Text = "Hardline" Then TextBox8.Text = "2" 
       Else TextBox8.Text = "3" 

     End Select 
    End If 
End Sub 
+0

请缩短未来的问题的示例代码。 – usr 2012-02-18 21:52:30

回答

7

你不能有代码,那么后 “和” 不同的线路上有一个ELSE:

您代码:

If TextBox14.Text = "Hardline" Then TextBox8.Text = "11" 
    Else TextBox8.Text = "12" 

修复:

If TextBox14.Text = "Hardline" Then 
    TextBox8.Text = "5" 
Else 
    TextBox8.Text = "6" 
End If 

或:

If TextBox14.Text = "Hardline" Then TextBox8.Text = "5" Else TextBox8.Text = "6" 

或放置在所述端的下划线:当你开始If码块

If TextBox14.Text = "Hardline" Then TextBox8.Text = "11" _ 
    Else TextBox8.Text = "12" 
+0

感谢您的帮助:) – hammy78 2012-02-18 21:53:37

1

,必须结束该码块以End If

我没有在您的任何Case块中看到End If

下面是一个example from MSDN:如果你把它全部在一行上,你不需要End If

If condition [ Then ] 
    [ statements ] 
[ ElseIf elseifcondition [ Then ] 
    [ elseifstatements ] ] 
[ Else 
    [ elsestatements ] ] 
End If 

- 或 -

If condition Then [ statements ] [ Else [ elsestatements ] ] 
1

您还可以使用If操作:

TextBox8.Text = If(TextBox14.Text = "Hardline", "5", "6") 
+0

建议避免使用IIF *函数*而是使用IF *运算符*(如果VB支持它的版本,那就是) – 2012-02-20 14:50:47

+0

好的,我是ac#程序员,仍在使用VS 2008 。我只知道具有两个参数(C#??运算符)的If-运算符的版本。谢谢你的提示。 – 2012-02-20 15:31:34

0

我对此代码有同样的问题,并解决它:

If buttoncount = 1 Then 
     buttoncount = 1 
ElseIf (chkUnbook.Checked = True) Then 
     buttoncount = 0 
End if 

显然,你还必须放置一个,如果年底前if和else if,即使有一个已经存在(VB 2010)