簡體   English   中英

為在VB.NET中動態創建的某些控件分配值

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

好吧,我在VB.NET上有一個動態創建的控件列表,我想為其分配一個值。

        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

如果將deel變量的邏輯移到For...Next循環之外怎么辦? 我不確定altur變量是什么,但是很可能通過在循環中使用此邏輯來嘗試調用尚不存在的PictureBox控件。 另外,在Panel1.Scroll事件中可能還需要類似的邏輯。

這是我正在談論的更加充實的示例(我移動了一些變量以使Panel1.Scroll事件與新創建的PictureBox控件一起工作,並更改了deel變量使用的邏輯來拾取第三個可見的PictureBox控件。當然,您可能需要在Panel1.Scroll事件中更改它,具體取決於您實際顯示的PictureBox控件的數量。 我已經測試了這段代碼,它似乎可以滿足我的要求:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM