首页 > 解决方案 > 是否可以对多个图片框进行分组?

问题描述

我可以将 PictureBox 拖到表单控件、选项卡控件或面板控件等上。我可以将图像导入到 PictureBox 中。但是,我不知道如何在 Visual Studio 2017 中将多个 PictureBoxes 组合在一起。看起来没有这样的功能。我需要这个功能,因为我想根据用户的输入生成一张大图。该大图由多张小图组成,其可见性由用户通过多个复选框控制。

在 Excel 中,我可以将多张图片放入其中,将它们组合在一起,使用 VBA 控制每张图片的可见性,最后将该图片组复制到 Word 文件中。我将使用 vb.net 在 Visual Studio 2017 的 VSTO Word 文档项目中执行此操作。
我添加了一些图片来演示预期的功能。
图 1 显示了要在大图中使用的小图。(请忽略 .vslx 文件)
图 2 显示了基于用户输入的可能结果。

标签: vb.netwinforms

解决方案


您可以制作自己的自定义控件。
这是一个示例/建议如何User control使用可以在您的应用程序中重用的示例/建议。用户控件将面板保存在矩阵中,您可以为每个控件
设置拖放操作,用户将能够在每个面板上放置一个图片框:EventPanel

用户控制:

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

图形用户界面输出:

在此处输入图像描述


推荐阅读