【问题标题】:How to add a line break in dynamically generated control in vb.net如何在 vb.net 中动态生成的控件中添加换行符
【发布时间】:2021-05-25 08:25:35
【问题描述】:

您好,我正在做一个小项目,我想以编程方式在面板上显示 PictureBox,尽管由于 StackOverflow 我以某种方式实现了这一点,但现在问题是我只希望在第一行显示 4 到 5 个图片框以及第二行的下一个框,依此类推 这是我在面板中添加图片框的代码

Try
    Dim numberOfPics As Integer = images.Count
    Dim lastLeft As Integer = 15
    Const spacer As Integer = 5

    For parser As Integer = 1 To numberOfPics - 1
        Dim PicBox As New PictureBox
        PicBox.Width = 88
        PicBox.Height = 135
        PicBox.Top = 25
        PicBox.Left = lastLeft
        lastLeft = PicBox.Width + PicBox.Left + spacer
        PicBox.SizeMode = PictureBoxSizeMode.StretchImage
        PicBox.BorderStyle = BorderStyle.FixedSingle
        Me.Panel1.Controls.Add(PicBox)
        Dim tClient As WebClient = New WebClient
        Dim tImage As Bitmap = Bitmap.FromStream(New MemoryStream(tClient.DownloadData(images.Item(parser).Groups(1).Value)))
        PicBox.Image = tImage
    Next
Catch ex As Exception
    MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
End Try

此代码像这样在单行中显示图片框 Code Result

我已尽力解释我的问题,但如果我遗漏了什么,请告诉我我的错误。

【问题讨论】:

  • 作为记录,我正在手机上查看此内容,并且您的一半代码被遮盖了,因为您无缘无故地包含了一大堆前导空格。你应该尽你所能帮助我们帮助你。

标签: vb.net


【解决方案1】:

此代码可帮助您了解如何实现目标。因此,从这里开始并改进: (代码中的一些cmets)

    Try

        Const spacer As Integer = 5

        ' Here max elements x row
        Dim elementsXRow As Integer = 3

        Dim numberOfPics As Integer = 11 'images.Count

        Dim lastLeft As Integer = 15
        Dim lastTop As Integer = 25
        Dim w As Integer = 88
        Dim h As Integer = 138

        For parser As Integer = 0 To numberOfPics - 1

            ' If parser is like 3 + 3 + 3 you need to start from point 0 -> means lastLeft - > 15
            If parser > 0 AndAlso parser Mod elementsXRow = 0 Then
                ' so restart your coordinates
                lastLeft = 15
                lastTop += h + spacer
            End If


            'Dim tClient As WebClient = New WebClient
            'Dim tImage As Bitmap = Bitmap.FromStream(New MemoryStream(tClient.DownloadData(images.Item(parser).Groups(1).Value)))

            Dim PicBox As New PictureBox With {
                .Width = w,
                .Height = h,
                .Top = lastTop,
                .Left = lastLeft,
                .SizeMode = PictureBoxSizeMode.StretchImage,
                .BorderStyle = BorderStyle.FixedSingle,
                .Image = Nothing 'tImage
            }

            lastLeft = PicBox.Width + PicBox.Left + spacer

            Me.Panel1.Controls.Add(PicBox)



        Next

    Catch ex As Exception
        MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
    End Try

End Sub

要拥有 4 个元素 x 行,您只需更改 elementsXRow = 4

【讨论】:

  • 我做了和你在上面代码中提到的一样的事情,但是上面的代码只打印了 4 个这样的图片框link
  • 增加面板的高度。如果它们超过 4 个,那么它们就在某个地方是不可见的。玩得开心
猜你喜欢
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2020-11-21
  • 2012-11-11
相关资源
最近更新 更多