简体   繁体   中英

Assign a value to certain control dynamically created in VB.NET

Well, I have a list of dynamically created controls on VB.NET, and I want to assign it a value.

        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

And well, deel doesn't work, I want to zoom the central image of the scroll, but I can't. :( I have commented out this line, because If not put it like that, I will cause an error (the images of the scroll doesn't charge)

I have put it on the Form Shown event, but... This doesn't work. :P

What if you move the logic for the deel variable outside of the For...Next loop? I'm not sure what the altur variable is, but it's very possible that, by having this logic in the loop, you're trying to call on a PictureBox control that doesn't yet exist. Also, you'll probably need similar logic in the Panel1.Scroll event.

Here's an example of what I'm talking about a little more fleshed-out (I moved some of the variables around to make the Panel1.Scroll event work with the newly created PictureBox controls, and changed the logic you were using for the deel variable to pick up the third PictureBox control that's visible. Of course, you may want to change this in the Panel1.Scroll event, depending on the number of PictureBox controls you are actually displaying). I've tested this code, and it appears to do what I believe you're looking for:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM