New PictureBox Not Showing

DrivenToProgram

New member
Joined
Sep 27, 2013
Messages
1
Programming Experience
Beginner
I am having an issue with some code I am trying to get to work. What I am trying to do is allow the main object to deploy more than one small object if the space bar is pressed more than once. Then I want the timer to control every new small object and send it in the direction it is facing.

I am new to programming, so if I get a few things wrong, please bear with me and point me in the right direction.

Here is my code. No errors or anything come up, the new picturebox just doesn't show when space bar is pressed.

VB.NET:
    Public Class Form1


        Dim newPictureBox As New PictureBox


        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            Select Case e.KeyCode
                Case Keys.Up
                    MainObject.Image = imlObjects.Images.Item(0)
                    MainObject.Top -= 4
                Case Keys.Down
                    MainObject.Image = imlObjects.Images.Item(1)
                    MainObject.Top += 4
                Case Keys.Right
                    MainObject.Image = imlObjects.Images.Item(2)
                    MainObject.Left += 4
                Case Keys.Left
                    MainObject.Image = imlObjects.Images.Item(3)
                    MainObject.Left -= 4
                Case Keys.Space
                    If picMainObject.Image Is imlObjects.Images.Item(0) Then
                        newPictureBox.Image = My.Resources.SmallObjectUp 'Object faces up
                    ElseIf picMainObject.Image Is imlObjects.Images.Item(1) Then
                        newPictureBox.Image = My.Resources.SmallObjectDown
                    ElseIf picMainObject.Image Is imlObjects.Images.Item(2) Then
                        newPictureBox.Image = My.Resources.SmallObjectRight
                    ElseIf picMainObject.Image Is imlObjects.Images.Item(3) Then
                        newPictureBox.Image = My.Resources.SmallObjectLeft
                    End If
                    newPictureBox.Location = New Point(picMainObject.Left + (picMainObject.Width / 2), picMainObject.Top + (picMainObject.Height / 2))
                    newPictureBox.SizeMode = PictureBoxSizeMode.AutoSize
                    newPictureBox.Visible = True
                    Me.Controls.Add(newPictureBox)
                    tmrSmallObjectControl.Start()
            End Select
        End Sub


        Private Sub tmrSmallObjectControl_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrSmallObjectControl.Tick
            If newPictureBox.Image Is My.Resources.SmallObjectUp Then
                newPictureBox.Top -= 10
            ElseIf newPictureBox.Image Is My.Resources.SmallObjectDown Then
                newPictureBox.Top += 10
            ElseIf newPictureBox.Image Is My.Resources.SmallObjectRight Then
                newPictureBox.Left += 10
            ElseIf newPictureBox.Image Is My.Resources.SmallObjectLeft Then
                newPictureBox.Left -= 10
            End If
        End Sub
    End Class
 
First of all, do you realise that that code is never going to display more than one Image? You're only creating one PictureBox so you're only ever going to display one Image.

Secondly, have you confirmed that that KeyDown event handler is even being executed? If it's not then obviously the code inside it will not run. If there are other controls on the form that receive keyboard input then you must set the form's KeyPreview property to True in order for it to raise keyboard events too. If in doubt, place a breakpoint (F9) on the event handler and then the debugger will break each time that line is hit.
 
Back
Top