Reputation: 97
I have 3 ListBoxes, 1 of them lists folders, 1 lists subfolders, the third lists picture files from current subfolder. 1 Picturebox loads the selected item(picture) of the 3rd listbox. I have 2 buttons to change previous image or next image calling this function (this make loops of the image list to make it endless):
Private Sub ShowNextImage()
If ListBox5.SelectedIndex = ListBox5.Items.Count - 1 Then
ListBox5.SelectedIndex = 0
Else
ListBox5.SelectedIndex += 1
End If
Me.PictureBox1.Image = Image.FromFile(lastfoldr & (ListBox5.SelectedItem))
End Sub
the button to change to righ is just this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ShowNextImage()
End Sub
Then I add function to change picturebox size and get fullscreen when you click the picturebox.
also I add picturebox_keydown
event to detect keypress of left or right key at keyboard.
As fullscreen the best idea is change photo directly from keyboard and hide or sendtbak buttons, To make it work, First I try to call the previos/next image functions directly but it dont works!:
Private Sub PictureBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles PictureBox1.KeyDown
ShowNextImage()
End If
The problem comes when you press the right or left key: the selected folder or subfolder changes to the next item and the third ListBox updates the list of images, and everithing get loose!! at the second try I use:
Private Sub PictureBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles PictureBox1.KeyDown
If e.KeyCode = Keys.Right Then
ActiveControl = Nothing 'also I try this!
Button1.PerformClick() '< simulate the click of the button
End If
but does the same! the current selected folder changes and the list become loose. Clicking the buttons physically at the form change the images correctly, instead with keys I cant reach to get it work! What im doing wrong? Im calling (simulating) button clicks wrongly??
Upvotes: 0
Views: 663
Reputation: 18320
In order to receive key presses the PictureBox
must have focus, which such controls rarely have since you can't really select them.
The best way to do this is to override the ProcessCmdKey()
function of your form and use it to prevent the left and right key presses from reaching any child controls. Doing so will stop your list boxes' selection from changing.
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Left Then
ShowPrevImage()
Return True 'This prevents the key press from reaching any child controls.
ElseIf keyData = Keys.Right Then
ShowNextImage()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Upvotes: 1