如何在panel1.control中查看图片框? [英] How can I view pictureboxes in a panel1.control?
问题描述
我希望能够通过使用左右箭头键浏览我的panel1.controls中的每个图片框,不知道为什么我使用的代码不允许我这样做。
这是我正在使用的代码:
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
不确定最新情况。
我拥有什么尝试过:
我试图更改picform,这是一个表格到panel1.controls并将其切换出去并且它没有用。不知道该怎么做。
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.
推荐答案
这对你来说是一个快速而简单的解决方案。
并非一切都是最优的但是我认为你可以看到基本上可以做什么...
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 ...
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
我的控件目前只适用于MouseButtons。键盘仍未实现(目前)...
附加:
处理键盘你必须添加此方法代码:
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 :
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
这篇关于如何在panel1.control中查看图片框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!