Programmer XYZ
Programmer XYZ

Reputation: 408

Making a DrawImage a button in VB.net 2010

I have these three images that I have drawn to my form.

    GraphicsBuffer.DrawImage(ButtonEasy, New Rectangle(25, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)
    GraphicsBuffer.DrawImage(ButtonMedium, New Rectangle(150, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)
    GraphicsBuffer.DrawImage(ButtonHard, New Rectangle(275, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)

But I want to make a Boolean expression for when they are clicked so I can trigger the events to load the game mode selected.

Do I do this through resource code or is there a simply way to do this. My idea seems like it would be bad and not syntaxically correct.

Edit: I've gotten to this:

Private Sub ButtonEasy_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) _
 Handles ButtonEasy.MouseClick

    Dim buttonEasyRect = New Rectangle(25, 330, 100, 50)
    If buttonEasyRect.Contains(e.Location) Then

    End If

End Sub

But not really sure where to go from this. Apparently "ButtonEasy.Mouseclick" Handles throws an error "WithEvents variable undefined". Not sure where to go from here.

Thanks in advance!

Edit2: After help from LarsTech I've gotten an Enum in and this:

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
    Dim level As Difficulty = Difficulty.None
    If e.Button = MouseButtons.Left Then
    End If

    If New Rectangle(25, 330, 100, 50).Contains(e.Location) Then
        level = Difficulty.Easy
    ElseIf New Rectangle(150, 330, 100, 50).Contains(e.Location) Then       
        level = Difficulty.Medium
    ElseIf New Rectangle(275, 330, 100, 50).Contains(e.Location) Then
        level = Difficulty.Hard
    End If

    If level = Difficulty.Easy Then
        GameMode = 1
    ElseIf level = Difficulty.Medium Then
        GameMode = 2
    ElseIf level = Difficulty.Hard Then
        GameMode = 3
    End If

End Sub

How do I call this in my loop? Currently I have the loop wait for Asynchkeypress to set timescale to 300 which starts the game.

Upvotes: 1

Views: 1038

Answers (1)

LarsTech
LarsTech

Reputation: 81675

Is there a reason you don't actually use Buttons to do this?

In any case, you probably should have a class for all that information, which image, which rectangle, etc. This button class would also hold the IsPushed logic.

But for what you currently have, having an enum would probably help:

Public Enum Difficulty
  None
  Easy
  Medium
  Hard
End Enum

Then in the MouseDown event:

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
  Dim level As Difficulty = Difficulty.None

  If e.Button = MouseButtons.Left Then
    If New Rectangle(25, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Easy
    ElseIf New Rectangle(150, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Medium
    ElseIf New Rectangle(275, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Hard
    End If
  End If

  If level <> Difficulty.None Then
    MessageBox.Show("You are playing " & level.ToString)
  End If
End Sub

Upvotes: 1

Related Questions