0

I want to create a usercontrol that include 1 panel and 4 label

Here is what I try so far

Public Class OrderPanel
    Inherits Panel

    Public ItemName As String
    Public Quantity As Integer
    Public Price As Decimal
    Public DiscountType As Boolean
    Public DiscountAmount As Decimal
    Public Properties As String
    Public SubTotal As Decimal
End Class

This is the Designer File

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class OrderPanel
    Inherits System.Windows.Forms.UserControl

    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Font = New System.Drawing.Font("Microsoft YaHei", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(161, Byte), Integer), CType(CType(218, Byte), Integer))
        Me.Label1.Location = New System.Drawing.Point(3, 2)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(59, 21)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Label1"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Font = New System.Drawing.Font("Microsoft YaHei", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(161, Byte), Integer), CType(CType(218, Byte), Integer))
        Me.Label2.Location = New System.Drawing.Point(3, 23)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(59, 21)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "Label2"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Font = New System.Drawing.Font("Microsoft YaHei", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(161, Byte), Integer), CType(CType(218, Byte), Integer))
        Me.Label3.Location = New System.Drawing.Point(3, 44)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(59, 21)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Label3"
        '
        'Label4
        '
        Me.Label4.Font = New System.Drawing.Font("Microsoft YaHei", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label4.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(161, Byte), Integer), CType(CType(218, Byte), Integer))
        Me.Label4.Location = New System.Drawing.Point(257, 0)
        Me.Label4.Name = "Label4"
        Me.Label4.RightToLeft = System.Windows.Forms.RightToLeft.Yes
        Me.Label4.Size = New System.Drawing.Size(128, 23)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "Label4"
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'OrderPanel
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Name = "OrderPanel"
        Me.Size = New System.Drawing.Size(386, 66)
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents Label1 As Label
    Friend WithEvents Label2 As Label
    Friend WithEvents Label3 As Label
    Friend WithEvents Label4 As Label
End Class

When I try to add this usercontrol to my form i getting this error

"Failed to load toolbox item, It will be removed from Toolbox" error image

How can I solve the problem?

vbnewbie
  • 206
  • 6
  • 26
  • Look at the top of the designer file and your code file. There are two inherits: `UserControl` and `Panel` which is not allowed in VB .NET. Either remove the inheritance in the designer file and keep it the `Panel` in the code file or vice versa change the designer file to inherit `Panel` and remove it in the code file. – Alex B. Dec 16 '16 at 07:47
  • 1
    @AlexB. Looks like we came to similar answers at about the same time. Let me know if you want me to take mine down for yours to go up. – Andrew Mortimer Dec 16 '16 at 07:55
  • @AndrewMortimer Our answers are not exactly the same I guess. I recommend to change the inheritance to Panel instead of UserControl since this would remove all Panel specific properties in the OrderPanel class. See my answer if it makes sense. You can keep your´s for sure. – Alex B. Dec 16 '16 at 08:08

2 Answers2

2

Andrew's answer is fine but when you change the inheritance to UserControl you will lose all Panel specific properties (like AutoScaleDimensions) in the OrderPanel class.

So what I recommend is to keep the Panel inheritance but delete the generated one:

OrderPanel.designer.vb

Partial Class OrderPanel  
        'Inherits System.Windows.Forms.UserControl' <--- delete this
         Inherits Panel '<--- add this

OderPanel.vb

Public Class OrderPanel
    'Inherits Panel <--- delete this
Alex B.
  • 2,145
  • 1
  • 16
  • 24
1

Your OrderPanel class should be inheriting from UserControl

Public Class OrderPanel
    Inherits UserControl
Andrew Mortimer
  • 2,380
  • 7
  • 31
  • 33