2016-06-19 5 views
0

私はゲーム用のコードを書いています。都市の写真をランダムに見て、そのテキストをテキストボックスに書き込む必要があります。どのようにピクチャがピクチャボックス内にあるかをプログラムは知っていますか?

これは私が今持っているものです。

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    Me.BackColor = Color.Yellow 

    'achtergrond kleur is geel' 


End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim stad As Integer 
    Randomize() 
    stad = Int(Rnd() * 10) + 1 

    If stad = 1 Then PictureBox1.Image = My.Resources.amsterdam 
    If stad = 2 Then PictureBox1.Image = My.Resources.berlijn 
    If stad = 3 Then PictureBox1.Image = My.Resources.moskou 
    If stad = 4 Then PictureBox1.Image = My.Resources.neworleans 
    If stad = 5 Then PictureBox1.Image = My.Resources.newyork 
    If stad = 6 Then PictureBox1.Image = My.Resources.parijs 
    If stad = 7 Then PictureBox1.Image = My.Resources.rome 
    If stad = 8 Then PictureBox1.Image = My.Resources.sanfransisco 
    If stad = 9 Then PictureBox1.Image = My.Resources.shanghai 
    If stad = 10 Then PictureBox1.Image = My.Resources.tokio 

End Sub 

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 
    Dim name As String 
    name = TextBox1.Text 

    If PictureBox1.Image Is My.Resources.amsterdam And name = "amsterdam" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.berlijn And name = "berlijn" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.moskou And name = "moskou" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.neworleans And name = "new orleans" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.newyork And name = "newyork" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.parijs And name = "parijs" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.rome And name = "rome" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.sanfransisco And name = "san fransisco" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.tokio And name = "tokio" Then MsgBox("Dit antwoord is goed voor 1 punt") 
    If PictureBox1.Image Is My.Resources.shanghai And name = "shanghai" Then MsgBox("Dit antwoord is goed voor 1 punt") 

    If name = "" Then MsgBox("U heeft nog niks ingevuld, vul de juiste stad in a.u.b.") 

End Sub 

問題は、プログラムがピクチャボックスにあるもの絵を知っているので、どのように私は、ピクチャボックス内にある画像を知らせていないのですか?

+1

宣言 'stad'クリックイベント外とあなたが他の場所でそれを必要とするか、画像を設定する際のPictureBox Tagプロパティに名前を保存するとき、それはまだ後に、インデックスを保持します。 – Plutonix

+1

My.Resources.somethingプロパティは、使用するたびに* new *オブジェクトを作成します。あなたはPictureBox1.Imageプロパティに一致することはありません。また、ビットマップオブジェクトを深刻にリークすると、プログラムが非常に長く実行される可能性は低くなります。イメージオブジェクトを格納する変数を作成して、My.Resources.something * once *を使用する必要があります。フォームコンストラクターで変数を初期化します(Sub New)。 Cityが列挙型である場合、 'Dictionary(Of City、Image)'を使って、それをきれいにしてください。 –

答えて

0

下記を試してください。 Button1_Clickに複数の画像ボックス画像の条件を追加してください。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim stad As Integer 
     Randomize() 
     stad = Int(Rnd() * 10) + 1 

     If stad = 1 Then 
      PictureBox1.Image = My.Resources.amsterdam 
      PictureBox1.Tag = "amsterdam" 
     ElseIf stad = 2 Then 
      PictureBox1.Image = My.Resources.berlijn 
      PictureBox1.Tag = "berlijn" 
     End If 

    End Sub 

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 
     Dim name As String 
     name = TextBox1.Text 

     If name = "" Then 
      MsgBox("U heeft nog niks ingevuld, vul de juiste stad in a.u.b.") 
     ElseIf TextBox1.Text.Trim = PictureBox1.Tag Then 
      MsgBox("Dit antwoord is goed voor 1 punt") 
     End If 

    End Sub 
関連する問題