Home>

I want to create an image at Form1_Load and change the motion by clicking the moving image.
After displaying the image, you can change the movement by pressing a button, but you cannot click the image directly and move it individually.
It is an ugly code, thank you.

Applicable source code
Public Class Form1
    Inherits System.Windows.Forms.Form
    Const SIKAKUNUM As Integer = 2 'Number of squares Definition
    Dim ctr_pic (SIKAKUNUM) As PictureBox 'Class that holds images
    Dim iti (SIKAKUNUM) As Sikaku 'Class that manages the position of the square
    Dim push As Boolean
    Event switchmove (ByVal push As Boolean)
    Private Sub Button1_end_Click (sender As Object, e As EventArgs) Handles Button1_end.Click
        'Exit the program.
        End
    End Sub
    Private Sub Button2_start_Click (sender As Object, e As EventArgs) Handles Button2_start.Click
        'Start moving
        Timer1.Enabled = True
    End Sub
    Private Sub Form1_Load (sender As Object, e As EventArgs) Handles MyBase.Load 'Processing at startup
        'Display image as code
        Dim n As Integer
        For n = 0 To SIKAKUNUM
            'Set the image position
            iti (n) = New Sikaku
            iti (n) .X = 80 * n
            iti (n) .Y = 50 * n
            'Set and add image controls
            ctr_pic (n) = New PictureBox
            With ctr_pic (n)
                .Size = New Size (10, 10)
                .Location = New Point (iti (n) .X, iti (n) .Y)
                .Image = Image.FromFile ("pic1.bmp")
            End With
            Me.Controls.Add (ctr_pic (n))
        Next
    End Sub
    Private Sub Timer1_Tick (sender As Object, e As EventArgs) Handles Timer1.Tick
        'Move the square
        Dim n As Integer
        For n = 0 To SIKAKUNUM
            iti (n) .go (Me, push)
            ctr_pic (n) .Location = New Point (iti (n) .X, iti (n) .Y)
        Next
    End Sub
    Private Sub Button1_Click (sender As Object, e As EventArgs) Handles Button1.Click
        If push = False Then
            push = True
            Debug.WriteLine ("click = True")
        Else
            push = False
            Debug.WriteLine ("click = False")
        End If
    End Sub
End Class
Public Class Sikaku
    Inherits Form1
    'Class that manages the position of the square Inherit From1
    Private c_X As Integer
    Private c_Y As Integer
    'Property that holds the X coordinate of the square
    Public Property X () As Integer
        Get
            Return c_X
        End Get
        Set (ByVal Value As Integer)
            c_X = Value
        End Set
    End Property
    'Property that holds the Y coordinate of the square
    Public Property Y () As Integer
        Get
            Return c_Y
        End Get
        Set (ByVal Value As Integer)
            c_Y = Value
        End Set
    End Property
    Public Sub go (ByVal basyo As Form, ByVal pushu As Boolean)
        If pushu = True Then
            'Method to move square (X)
            c_X = c_X + 10
            If c_X>basyo.Size.Width-10 Then 'Width The horizontal length.
                c_X = 0
            End If
        Else
            'Method to move square (Y)
            c_Y = c_Y + 10
            If c_Y>basyo.Size.Height-10 Then 'Height Get the height.
                c_Y = 0
            End If
        End If
    End Sub
End Class
  • Answer # 1

    The image seems to be written in the PictureBox, so it should be changed in the Click event of the PictureBox.

    Adding event handlers
    If you search like this, various explanation pages will appear.
    Also see the AddHandler statement.

  • Answer # 2

    Public Class Form1
        Inherits System.Windows.Forms.Form
        Const SIKAKUNUM As Integer = 2 'Number of squares (defined number + 1)
        Dim ctr_pic (SIKAKUNUM) As Sikaku 'Class that holds images
        Dim iti (SIKAKUNUM) As Sikaku 'Class that manages the position of the square
        Private Sub Button1_end_Click (sender As Object, e As EventArgs) Handles Button1_end.Click
            'Exit the program.
            End
        End Sub
        Private Sub Button2_start_Click (sender As Object, e As EventArgs) Handles Button2_start.Click
            'Start moving
            Timer1.Enabled = True
        End Sub
        Private Sub Form1_Load (sender As Object, e As EventArgs) Handles MyBase.Load 'Processing at startup
            'Display image as code
            Dim n As Integer
            For n = 0 To SIKAKUNUM
                'Set the image position
                iti (n) = New Sikaku
                iti (n) .X = 80 * (n + 1)
                iti (n) .Y = 50 * (n + 1)
                'Set and add image controls
                ctr_pic (n) = New Sikaku
                With ctr_pic (n)
                    .Size = New Size (10, 10)
                    .Location = New Point (iti (n) .X, iti (n) .Y)
                    .Image = Image.FromFile ("pic1.bmp")
                End With
                Me.Controls.Add (ctr_pic (n))
            Next
        End Sub
        Private Sub Timer1_Tick (sender As Object, e As EventArgs) Handles Timer1.Tick
            'Move the square
            Dim n As Integer
            For n = 0 To SIKAKUNUM
                iti (n) .Go (Me, ctr_pic (n) .pushVal)
                ctr_pic (n) .Location = New Point (iti (n) .X, iti (n) .Y)
            Next
        End Sub
        Private Sub Button3_Click (sender As Object, e As EventArgs) Handles Button3.Click
            If Timer1.Enabled = True Then
                Timer1.Enabled = False
                Button3.Text = "Resume"
            ElseIf Timer1.Enabled = False Then
                Timer1.Enabled = True
                Button3.Text = "Active"
            End If
        End Sub
    End Class
    Public Class Sikaku
        Inherits PictureBox
        'Class that manages the position of the square Inherit From1
        Private c_X As Integer
        Private c_Y As Integer
        Private push As Boolean = True
        Public Property X () As Integer 'Property that holds the square X coordinate
            Get
                Return c_X
            End Get
            Set (ByVal Value As Integer)
                c_X = Value
            End Set
        End Property
        Public Property Y () As Integer 'Property that holds the Y coordinate of the square
            Get
                Return c_Y
            End Get
            Set (ByVal value As Integer)
                c_Y = value
            End Set
        End Property
        ReadOnly Property pushVal As Boolean
            Get
                Return push
            End Get
        End Property
        Private Sub Sikaku_Click (sender As Object, e As EventArgs) Handles Me.Click
            Me.push = Not push 'switch
            Debug.WriteLine (push)
        End Sub
        Public Sub Go (ByVal basyo As Form, ByVal muki As Boolean)
            If muki = True Then
                'Method to move square (X)
                c_X = c_X + 10
                If c_X>basyo.Size.Width-10 Then 'Width The horizontal length.
                    c_X = 0
                End If
            Else
                'Method to move square (Y)
                c_Y = c_Y + 10
                If c_Y>basyo.Size.Height-10 Then 'Height Get the height.
                    c_Y = 0
                End If
            End If
        End Sub
    End Class

Related articles