2016-12-16 79 views
0

我想创建一个用户控件,其中包括1个面板和4个标签如何创建自己的面板里面有四个标签?

这是我尝试到目前为止

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 

这是设计师文件

<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 

当我尝试添加此usercontrol到我的形式我得到这个错误

“无法加载工具箱项目,它将被从工具箱中删除” error image

我该如何解决问题?

+0

看在设计器文件和代码文件的顶部。有两个继承:'UserControl'和'Panel',这在VB .NET中是不允许的。要么删除设计器文件中的继承,并将其保存在代码文件中的Panel中,反之亦然,将设计器文件更改为继承'Panel'并将其在代码文件中删除。 –

+1

@AlexB。看起来大约在同一时间,我们遇到了类似的答案。让我知道你是否想让我为你的目标而上升。 –

+0

@AndrewMortimer我想我们的答案并不完全一样。我建议将继承更改为Panel而不是UserControl,因为这将删除OrderPanel类中的所有面板特定属性。看到我的答案,如果它是有道理的。你可以保持你的确定。 –

回答

2

被继承安德鲁的回答是很好,但是当你改变继承UserControl您将在OrderPanel类失去所有Panel特定的属性(如AutoScaleDimensions)。

所以我的建议是保持Panel继承,但删除生成的一个:

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 
1

你OrderPanel类应该从用户控件

Public Class OrderPanel 
    Inherits UserControl