Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am wanting to be able to navigate through each picturebox in my panel1.controls through using the right and left arrow keys, not sure why the code I am using isnt allowing me to do so.

Here is the code I am using:

Sub convertPic(ByVal sender As System.Object, ByVal e As System.EventArgs)

     'CONVERT SENDER INTO PICTUREBOX, used on the images being displayed on the panel control, not used for images in database
     pic = CType(sender, PictureBox)

     ' Make new picturebox and fill it with inage data from pic, then after making dimesions fill dock with that image so that it
     ' can be streched or forshortend by dragging form.
     Dim expandpic As New PictureBox
     expandpic.Height = pic.Image.Height
     expandpic.Width = pic.Image.Width
     expandpic.Image = pic.Image
     expandpic.Dock = DockStyle.Fill
     expandpic.SizeMode = PictureBoxSizeMode.StretchImage

     ' Make a new form and add the new picture data to it
     Dim picform As New Form
     picform.Controls.Add(expandpic)
     picform.Height = 800
     picform.Width = 600
     picform.Show()


 End Sub
 Private Sub Navigate_pics(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Panel1.KeyDown


     If e.KeyCode = Keys.Right Then
         expandpic = CType(sender, PictureBox)
         picform.SelectNextControl(expandpic, True, True, True, True)
     End If




 End Sub


not sure whats going on.

What I have tried:

I tried to change picform, which is a form to panel1.controls and switched that out and it didnt work. Not sure what to do.
Posted
Updated 19-Jul-17 3:26am
v2
Comments
Ralf Meier 18-Jul-17 3:45am    
Perhaps you try it like this :

Private Sub Navigate_pics(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Panel1.KeyDown
Panel1.SelectNextControl ...
End Sub

??? What is pic inside the SelectNextControl ? Where is it defined and assigned ?
Member 11856456 18-Jul-17 3:54am    
Pic is the pictureboxes I have created from an array. Pic is added to panel1.controls. I am trying to use the right and left arrow keys to go back and forth between the images to that they can be displayed at a pace someone would like to go.
Ralf Meier 18-Jul-17 6:12am    
That is different to the code you have posted ...
When I see the code I suppose that you have a Panel with x PictureBoxes in it. Your Panel has the Focus and you want to select the next PictureBox inside the Panel.
So ... what do you really have ? A count of PictureBoxes inside a Panel or one PictureBox which should display severall Pictures ?
If you have a count of PictureBoxes and you select one of it the Panel has no longer the Focus. So now the Keydown-Event will come from the PictureBox ...
If you have one PictureBox you should think about building a customized Version of it which has the desired behaviour ...
Member 11856456 18-Jul-17 17:52pm    
I have improved the question, I thought it was going to be a simple answer that I just couldn't figure out. The reason I assume the panel holds the info I want is because the original pictureboxes, dubbed "pic" are located in the panel.


Ralf Meier 19-Jul-17 3:40am    
You haven't allready answered my question(s) ...
Please remember : I don't sit beside you and I can't see what you see ...

1 solution

This is a little Quick-and-Dirty Solution for you.
Not everything works optimal but I think you could see what basicly could be done ...

VB
Imports System.ComponentModel

Public Class myPB
    Inherits Control

    Public Sub New()
        Me.Size = New Size(640, 160)
        Me.BackColor = Color.Gainsboro
        SetStyle(ControlStyles.Selectable, True)
    End Sub

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
    ReadOnly Property Images As List(Of ImageDefinition)
        Get
            Return my_Images
        End Get
    End Property
    Private my_Images As New List(Of ImageDefinition)

 
    Property ImagesVisible As Integer
        Get
            Return my_ImagesVisible
        End Get
        Set(value As Integer)
            my_ImagesVisible = value
            my_ImageSize = New Size(Me.Width / my_ImagesVisible, Me.Height)
            Me.Invalidate()
        End Set
    End Property
    Private my_ImagesVisible As Integer = 3
    Private my_ImageSize As Size

    Property ActualImage As Integer
        Get
            Return my_ActualImage
        End Get
        Set(value As Integer)
            my_ActualImage = value '+ my_Images.Images.Count) Mod my_Images.Images.Count
            If my_ActualImage < 0 Then my_ActualImage = my_Images.Count - 1
            If my_ActualImage > my_Images.Count - 1 Then my_ActualImage = 0
              Me.Invalidate()
        End Set
    End Property
    Private my_ActualImage As Integer = 0


    Protected Overrides Sub OnResize(e As EventArgs)
        my_ImageSize = New Size(Me.Width / ImagesVisible, Me.Height)
        MyBase.OnResize(e)
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim gr As Graphics = e.Graphics

        If my_Images.Count > 0 Then
            Dim inx As Integer
            Dim n As Integer = my_ImagesVisible : If n > my_Images.Count Then n = my_Images.Count
            Dim myImage As Image
            For i As Integer = 0 To n - 1
                inx = (i + my_ActualImage) Mod my_Images.Count
                'If inx > my_Images.Images.Count Then inx = 0
                myImage = my_Images.Item(inx).xImage
                gr.DrawImage(myImage, New Rectangle(i * my_ImageSize.Width, 0, my_ImageSize.Width, my_ImageSize.Height), New Rectangle(0, 0, myImage.Width, myImage.Height), GraphicsUnit.Pixel)
            Next
        End If

        MyBase.OnPaint(e)
    End Sub

    'Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
    '    If e.KeyCode = Keys.Left Then
    '        ActualImage -= 1
    '    ElseIf e.KeyCode = Keys.Right Then
    '        ActualImage += 1
    '    Else
    '        MyBase.OnKeyDown(e)
    '    End If
    'End Sub

    Protected Overrides Sub OnMouseClick(e As MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then ActualImage += 1
        If e.Button = Windows.Forms.MouseButtons.Right Then ActualImage -= 1
          MyBase.OnMouseClick(e)
    End Sub

End Class

Public Class ImageDefinition

    Public Property xImage As Image

End Class


My Control in the moment only works with the MouseButtons. With the Keyboard is still unrealized (at the moment) ...

Additional :
to handle the Keyboard you have to add this method to the code :
VB
Protected Overrides Sub OnPreviewKeyDown(e As PreviewKeyDownEventArgs)
    If e.KeyCode = Keys.Left Then ActualImage += 1
    If e.KeyCode = Keys.Right Then ActualImage -= 1
      MyBase.OnPreviewKeyDown(e)
End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900