2012-02-18 19 views
1

ランダムに番号を生成するプログラムを構築していますが、この番号はピクチャボックスに表示されているピクチャにリンクされています。下にテキストボックスがあり、このテキストボックスの内容に応じてピクチャの1つが変わるので、if文とelse文を使用しました。しかし、私はこのエラーを受け取ります。 'Else'の前に一致する 'If'または 'ElseIf'を付ける必要があります。おそらく私が見ていない単純な解決策があります。ここにコードがあります。Visual BasicでIf文とElse文の問題

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

答えて

7

あなたはTHENの後のコードを持っている "AND" 別の行に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 TextBox14.Text = "Hardline" Then TextBox8.Text = "11" _ 
    Else TextBox8.Text = "12" 
+0

助けてくれてありがとう – hammy78

1

あなたはIfとのコードブロックを開始すると、あなたはEnd Ifでそのコードブロックを終了する必要があります。

ブロックCaseのいずれにもEnd Ifが表示されません。ここで

example from MSDNです:あなたは1行にすべてを置く場合、あなたはまた、If演算子を使用することができますEnd If

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

- または -

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

は必要ありません。

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

IF *演算子*を使用することを推奨します(VBのバージョンがそれをサポートしている場合) –

+0

OK、私はプログラマーで#VS 2008私は2つの引数(C#??演算子)を持つIf演算子のバージョンしか知りませんでした。ヒントありがとうございます。 –

0

このコードで同じ問題が発生し、解決しました:

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

どうやら、あなたもあれば終了前に配置する必要があります場合は、他1既に存在している(VB 2010)