是否可以将多个PictureBox分组?

时间:2018-12-21 20:49:38

标签: vb.net winforms

我可以将PictureBox拖动到窗体控件,选项卡控件或面板控件等上。并且可以将图像导入到PictureBox中。但是,我不知道如何在Visual Studio 2017中将多个PictureBox分组在一起。似乎没有这样的功能。我需要此功能,因为我想根据用户的输入生成一张大图片。大图片由多张小图片组成,其可见性由用户通过多个复选框控制。

在Excel中,我可以将多张图片放入其中,将它们分组在一起,使用VBA控制每张图片的可见性,最后将该图片组复制到Word文件中。我会使用vb.net在Visual Studio 2017中的VSTO Word文档项目中执行此操作。
我添加了一些图片以演示预期的功能。
Picture 1 shows the small pictures to be used in a big picture. (Please ignore the .vslx file)
Picture 2 shows a possible result based on user's input.

1 个答案:

答案 0 :(得分:2)

您可以创建自己的自定义控件。
这里是一个示例/建议,说明如何使用User control来实现它,并可以在您的应用程序中重复使用。在矩阵中,您可以为每个Event控件设置一个拖放Panel,用户将可以在每个面板上放置一个图片框:

用户控制:

Public Class UserControl1

    Public NumberOfPanelsInRow As Integer
    Sub New(ByVal height As Integer, width As Integer, Optional ByVal numberofPanelsInRow As Integer = 3)

        ' This call is required by the designer.'
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.'
        Me.Height = height
        Me.Width = width
        Me.NumberOfPanelsInRow = numberofPanelsInRow
    End Sub

    Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' grouped panels to hold picturebox you can drag & drop to them...'
        Dim panelHeight As Integer = Me.Height / NumberOfPanelsInRow
        Dim panelWidth As Integer = Me.Width / NumberOfPanelsInRow
        Dim colors() As Color = {Color.Pink, Color.Black, Color.Red, Color.Cyan, Color.Green, Color.Orange,
            Color.Red, Color.Pink, Color.Black, Color.Red, Color.Cyan, Color.Green, Color.Orange, Color.Red}
        Dim total As Integer = NumberOfPanelsInRow * NumberOfPanelsInRow
        Dim currentYlocation As Integer = 0
        Dim currentXlocation As Integer = 0
        Dim location As Point = New Point(0, currentYlocation)
        Dim rowcounter As Integer = 0
        Dim itemcounter As Integer = 0
        For i = 1 To total

            If rowcounter >= NumberOfPanelsInRow Then
                rowcounter = 0
                currentYlocation += panelHeight
                currentXlocation = 0
            End If

            ' to each one of this panel you can drag a picture box'
            Dim p As New Panel
            p.Size = New Size(panelWidth, panelHeight)
            p.Location = New Point(currentXlocation, currentYlocation)
            p.BackColor = colors(itemcounter)
            Me.Controls.Add(p)

            rowcounter += 1
            itemcounter += 1
            currentXlocation += panelWidth
        Next

    End Sub

End Class

从FORM1调用用户控件:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim uc = New UserControl1(300, 300)
        Me.Controls.Add(uc)
    End Sub
End Class

GUI输出:

enter image description here