以编程方式将子控件添加到用户控件

时间:2015-10-29 17:27:31

标签: vb.net winforms

首次创建自己的用户控件,我遇到了问题。我想以编程方式创建子控件,而不是手动设置~480个控件。但是,在我的一个表单中实现控件后,我以编程方式创建的控件不会出现,即使我调用Generate并且它成功运行。 (检查断点)

这就是我所拥有的:

Imports Syncfusion.Windows.Forms.Tools

Public Class InfoGrid
Private _Quantities As CheckBox()
Private _Names As Label()
Private _Toggle As ToggleButton() ' From SyncFusion WinForms Library
Private Beleren As Font = New Font("Beleren", 10, FontStyle.Regular)
Private IDInset As Insets = New Insets(5, 5, 5, 5)
Private QuantityInset As Insets = New Insets(5, 5, 5, 5)
Private NameInset As Insets = New Insets(0, 5, 0, 5)
Private ToggleInset As Insets = New Insets(0, 0, 0, 0)

Public Property Quantities As CheckBox()
    Get
        Return _Quantities
    End Get
    Set(value As CheckBox())
        _Quantities = value
    End Set
End Property

Public Property Names As Label()
    Get
        Return _Names
    End Get
    Set(value As Label())
        _Names = value
    End Set
End Property

Public Property Toggles As ToggleButton()
    Get
        Return _Toggle
    End Get
    Set(value As ToggleButton())
        _Toggle = value
    End Set
End Property

Public Sub Generate()
    SuspendLayout()
    For i% = 2 To 31
        AddNewCtrls(i, vbNullString, False, i, i - 1)
        AddNewCtrls(i + 30, vbNullString, False, i + 4, i - 1)
        AddNewCtrls(i + 60, vbNullString, False, i + 8, i - 1)
        AddNewCtrls(i + 90, vbNullString, False, i + 12, i - 1)
    Next
    ResumeLayout()
    Refresh()
    PerformLayout()
End Sub

Public Function AddNewCtrls(IDNum%, CtrlName$, CtrlEnabled As Boolean, GridX%, GridY%)
    Dim NewCtrlQuantity As New CheckBox, NewCtrlID, NewCtrlName As New Label, NewCtrlToggle As New ToggleButton

    Try
        NewCtrlID.Font = Beleren
        NewCtrlID.Text = IDNum
        NewCtrlID.Tag = IDNum
        NewCtrlID.Name = "lbl" & IDNum
        NewCtrlID.TextAlign = ContentAlignment.MiddleCenter
        NewCtrlID.AutoSize = True
        gblMain.SetConstraints(NewCtrlID, New GridBagConstraints(GridX, GridY, 1, 1, 0R, 0R, AnchorTypes.Center, FillType.None, IDInset, 0, 0, False))

        NewCtrlQuantity.Text = "400"
        NewCtrlQuantity.Name = "cbCount" & IDNum
        NewCtrlQuantity.Tag = IDNum
        NewCtrlQuantity.FlatStyle = FlatStyle.Flat
        NewCtrlQuantity.Appearance = Windows.Forms.Appearance.Button
        NewCtrlQuantity.MaximumSize = New Size(60, 25)
        NewCtrlQuantity.MinimumSize = New Size(60, 25)
        NewCtrlQuantity.AutoSize = True
        gblMain.SetConstraints(NewCtrlQuantity, New GridBagConstraints((GridX + 1), GridY, 1, 1, 0R, 0R, AnchorTypes.Center, FillType.None, QuantityInset, 0, 0, False))

        NewCtrlName.Text = CtrlName
        NewCtrlName.Name = "lblAssign" & IDNum
        NewCtrlName.Tag = IDNum
        NewCtrlName.MaximumSize = New Size(160, 25)
        NewCtrlName.AutoSize = True
        NewCtrlName.AutoEllipsis = True
        NewCtrlName.TextAlign = ContentAlignment.MiddleCenter
        gblMain.SetConstraints(NewCtrlName, New GridBagConstraints((GridX + 2), GridY, 1, 1, 0R, 0R, AnchorTypes.Center, FillType.None, NameInset, 0, 0, False))

        NewCtrlToggle.Text = ""
        NewCtrlToggle.Name = "cbMask" & IDNum
        NewCtrlToggle.Tag = IDNum
        NewCtrlToggle.MaximumSize = New Size(25, 25)
        NewCtrlToggle.MinimumSize = New Size(25, 25)
        gblMain.SetConstraints(NewCtrlToggle, New GridBagConstraints((GridX + 3), GridY, 1, 1, 0R, 0R, AnchorTypes.Center, FillType.None, ToggleInset, 0, 0, False))

        Controls.AddRange(New Control() {NewCtrlID, NewCtrlQuantity, NewCtrlName, NewCtrlToggle})
    Catch ex As Exception
        LogException(ex)
        Return False
    End Try

    Return True
End Function
End Class

0 个答案:

没有答案