首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为在VB.NET中动态创建的某些控件赋值

为在VB.NET中动态创建的某些控件赋值
EN

Stack Overflow用户
提问于 2013-06-27 17:49:20
回答 1查看 675关注 0票数 0

嗯,我在VB.NET上有一个动态创建的控件列表,我想给它赋值。

代码语言:javascript
运行
复制
        Dim widaco As Integer = 126 'width value

Dim value As String = File.ReadAllText(".\Test.ini")
Dim cuenta As Integer = Find_String_Occurrences(value, "2ç0k") - 1

Dim Array_Size As Integer = cuenta
            ReDim pcb_(Array_Size)

    For pcb_num = 0 To Array_Size
                    Application.DoEvents()
                    'deel = Math.Abs(Int(Panel1.AutoScrollPosition.Y.ToString)) \ altur + 2
                    pcb_(pcb_num) = New PictureBox
                    pcb_(pcb_num).BackColor = Color.FromArgb(255, pcb_num * 3, pcb_num * 2, pcb_num)
                    pcb_(pcb_num).Height = 77
                    pcb_(pcb_num).Width = widaco
                    pcb_(pcb_num).Left = 36
                    pcb_(pcb_num).Top = 85 * pcb_num + 15
                    pcb_(pcb_num).BackgroundImage = Image.FromFile(".\Art\im\" & pcb_num + 1 & ".png")
                    pcb_(pcb_num).Image = Image.FromFile(INI_Manager.Load_Value(".\Test.ini", "FuncImg-" & pcb_num))
                    pcb_(pcb_num).Tag = pcb_num
                    'pcb_(deel).Width = 200
                    Me.Controls.Add(pcb_(pcb_num))
                    pcb_(pcb_num).Parent = Panel1
                    AddHandler pcb_(pcb_num).Click, AddressOf pcb_Click
                Next

deel不工作,我想缩放卷轴的中心图像,但是我不能。:(我已经注释掉了这一行,因为如果不这样放,我会导致一个错误(卷轴的图像不收费)

我已经把它放在事件显示的表单上了,但是...这不管用。:P

EN

回答 1

Stack Overflow用户

发布于 2013-07-11 04:17:52

如果将deel变量的逻辑移到For...Next循环之外会怎样呢?我不确定altur变量是什么,但通过在循环中包含此逻辑,您很可能正在尝试调用一个尚不存在的PictureBox控件。此外,您可能需要在Panel1.Scroll事件中使用类似的逻辑。

下面是一个更加充实的示例(我移动了一些变量以使Panel1.Scroll事件与新创建的PictureBox控件一起工作,并更改了用于deel变量的逻辑以选取第三个可见的PictureBox控件。当然,根据实际显示的PictureBox控件的数量,您可能希望在Panel1.Scroll事件中更改此设置)。我已经测试了这段代码,它看起来做了我相信你想要做的事情:

代码语言:javascript
运行
复制
Public Class Form1
    Private pcb_() As PictureBox
    Private deel As Integer                    'Current PictureBox control zoomed
    Private altur As Integer                   'New PictureBox control to zoom
    Private Array_Size As Integer              'The number of PictureBox controls added to Panel container
    Private Const widaco As Integer = 126      'Default width for non-zoomed PictureBox controls

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim value As String = File.ReadAllText(".\Test.ini")
        Dim cuenta As Integer = Find_String_Occurrences(value, "2ç0k") - 1

        Array_Size = cuenta
        ReDim pcb_(Array_Size)

        For pcb_num = 0 To Array_Size
            Application.DoEvents()
            pcb_(pcb_num) = New PictureBox

            With pcb_(pcb_num)
                .BackColor = Color.FromArgb(255, pcb_num * 3, pcb_num * 2, pcb_num)
                .Height = 77
                .Width = widaco
                .Left = 36
                .Top = 85 * pcb_num + 15
                .BackgroundImage = Image.FromFile(".\Art\im\" & pcb_num + 1 & ".png")
                .Image = Image.FromFile(INI_Manager.Load_Value(".\Test.ini", "FuncImg-" & pcb_num))
                .Tag = pcb_num
                'Added the following line to make sure the image correctly fills the PictureBox control for the "zoom" effect
                .SizeMode = PictureBoxSizeMode.StretchImage
                Me.Controls.Add(pcb_(pcb_num))
                .Parent = Panel1
            End With

            AddHandler pcb_(pcb_num).Click, AddressOf pcb_Click
        Next

        deel = 2
        pcb_(deel).Width = 200
    End Sub

    Private Sub Panel1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles Panel1.Scroll
        For pcb_num = 0 To Array_Size
            'A 0 value for the Top property of each PictureBox control indicates it is at the top of Panel1.
            'As soon as the PictureBox scrolls above the top of Panel1, we want to zoom in on the next PictureBox.
            If pcb_(pcb_num).Top >= 0 Then
                altur = pcb_num + 2
                Exit For
            End If
        Next pcb_num

        pcb_(deel).Width = widaco
        pcb_(altur).width = 200
        deel = altur
    End Sub
End Class
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17339956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档