未定义类型'usercontrol'(Visual Studio 2015)

时间:2016-10-01 20:24:40

标签: vb.net winforms visual-studio-2015 user-controls

我在VS2015中添加了一个自定义的winforms用户控件。当我将控件拖到窗体并编译时,我得到错误类型'xxx'未定义。如果我从控件名称中删除命名空间,我可以修复,但对用户控件的任何更改都会覆盖我的更改。 有没有办法防止这种情况发生?

这是自动生成的控件(称为“ctrlTestStatus”):

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class ctrlTestStatus
    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.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
        Me.prgLocalDatabase = New System.Windows.Forms.ProgressBar()
        Me.prgRemoteDatabase = New System.Windows.Forms.ProgressBar()
        Me.lblLocalDatabaseStatus = New System.Windows.Forms.Label()
        Me.lblRemoteDatabaseStatus = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.prgAmazon = New System.Windows.Forms.ProgressBar()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.lblAmazonStatus = New System.Windows.Forms.Label()
        Me.prgEbay = New System.Windows.Forms.ProgressBar()
        Me.lblEbayStatus = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.lbStatus = New System.Windows.Forms.ListBox()
        Me.TableLayoutPanel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'TableLayoutPanel1
        '
        Me.TableLayoutPanel1.BackColor = System.Drawing.SystemColors.Window
        Me.TableLayoutPanel1.ColumnCount = 3
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25.30766!))
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 58.95527!))
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15.73707!))
        Me.TableLayoutPanel1.Controls.Add(Me.prgLocalDatabase, 1, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.prgRemoteDatabase, 1, 1)
        Me.TableLayoutPanel1.Controls.Add(Me.lblLocalDatabaseStatus, 2, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.lblRemoteDatabaseStatus, 2, 1)
        Me.TableLayoutPanel1.Controls.Add(Me.Label4, 0, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.Label5, 0, 1)
        Me.TableLayoutPanel1.Controls.Add(Me.prgAmazon, 1, 2)
        Me.TableLayoutPanel1.Controls.Add(Me.Label6, 0, 2)
        Me.TableLayoutPanel1.Controls.Add(Me.lblAmazonStatus, 2, 2)
        Me.TableLayoutPanel1.Controls.Add(Me.prgEbay, 1, 3)
        Me.TableLayoutPanel1.Controls.Add(Me.lblEbayStatus, 2, 3)
        Me.TableLayoutPanel1.Controls.Add(Me.Label1, 0, 3)
        Me.TableLayoutPanel1.Location = New System.Drawing.Point(8, 7)
        Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
        Me.TableLayoutPanel1.RowCount = 4
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
        Me.TableLayoutPanel1.Size = New System.Drawing.Size(425, 107)
        Me.TableLayoutPanel1.TabIndex = 8
        '
        'prgLocalDatabase
        '
        Me.prgLocalDatabase.Location = New System.Drawing.Point(110, 3)
        Me.prgLocalDatabase.Name = "prgLocalDatabase"
        Me.prgLocalDatabase.Size = New System.Drawing.Size(244, 20)
        Me.prgLocalDatabase.TabIndex = 0
        '
        'prgRemoteDatabase
        '
        Me.prgRemoteDatabase.Location = New System.Drawing.Point(110, 29)
        Me.prgRemoteDatabase.Name = "prgRemoteDatabase"
        Me.prgRemoteDatabase.Size = New System.Drawing.Size(244, 20)
        Me.prgRemoteDatabase.TabIndex = 1
        '
        'lblLocalDatabaseStatus
        '
        Me.lblLocalDatabaseStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lblLocalDatabaseStatus.AutoSize = True
        Me.lblLocalDatabaseStatus.Location = New System.Drawing.Point(360, 6)
        Me.lblLocalDatabaseStatus.MinimumSize = New System.Drawing.Size(40, 13)
        Me.lblLocalDatabaseStatus.Name = "lblLocalDatabaseStatus"
        Me.lblLocalDatabaseStatus.Size = New System.Drawing.Size(40, 13)
        Me.lblLocalDatabaseStatus.TabIndex = 3
        '
        'lblRemoteDatabaseStatus
        '
        Me.lblRemoteDatabaseStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lblRemoteDatabaseStatus.AutoSize = True
        Me.lblRemoteDatabaseStatus.Location = New System.Drawing.Point(360, 32)
        Me.lblRemoteDatabaseStatus.MinimumSize = New System.Drawing.Size(40, 13)
        Me.lblRemoteDatabaseStatus.Name = "lblRemoteDatabaseStatus"
        Me.lblRemoteDatabaseStatus.Size = New System.Drawing.Size(40, 13)
        Me.lblRemoteDatabaseStatus.TabIndex = 4
        '
        'Label4
        '
        Me.Label4.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(4, 6)
        Me.Label4.MinimumSize = New System.Drawing.Size(100, 13)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(100, 13)
        Me.Label4.TabIndex = 6
        Me.Label4.Text = "Local Database:"
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'Label5
        '
        Me.Label5.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(4, 32)
        Me.Label5.MinimumSize = New System.Drawing.Size(100, 13)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(100, 13)
        Me.Label5.TabIndex = 7
        Me.Label5.Text = "Remote Database:"
        Me.Label5.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'prgAmazon
        '
        Me.prgAmazon.Location = New System.Drawing.Point(110, 55)
        Me.prgAmazon.Name = "prgAmazon"
        Me.prgAmazon.Size = New System.Drawing.Size(244, 20)
        Me.prgAmazon.TabIndex = 9
        '
        'Label6
        '
        Me.Label6.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.Label6.AutoSize = True
        Me.Label6.Location = New System.Drawing.Point(4, 58)
        Me.Label6.MinimumSize = New System.Drawing.Size(100, 13)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(100, 13)
        Me.Label6.TabIndex = 8
        Me.Label6.Text = "Amazon:"
        Me.Label6.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lblAmazonStatus
        '
        Me.lblAmazonStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lblAmazonStatus.AutoSize = True
        Me.lblAmazonStatus.Location = New System.Drawing.Point(360, 58)
        Me.lblAmazonStatus.MinimumSize = New System.Drawing.Size(40, 13)
        Me.lblAmazonStatus.Name = "lblAmazonStatus"
        Me.lblAmazonStatus.Size = New System.Drawing.Size(40, 13)
        Me.lblAmazonStatus.TabIndex = 5
        '
        'prgEbay
        '
        Me.prgEbay.Location = New System.Drawing.Point(110, 81)
        Me.prgEbay.Name = "prgEbay"
        Me.prgEbay.Size = New System.Drawing.Size(244, 20)
        Me.prgEbay.TabIndex = 11
        '
        'lblEbayStatus
        '
        Me.lblEbayStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.lblEbayStatus.AutoSize = True
        Me.lblEbayStatus.Location = New System.Drawing.Point(360, 86)
        Me.lblEbayStatus.MinimumSize = New System.Drawing.Size(40, 13)
        Me.lblEbayStatus.Name = "lblEbayStatus"
        Me.lblEbayStatus.Size = New System.Drawing.Size(40, 13)
        Me.lblEbayStatus.TabIndex = 12
        '
        'Label1
        '
        Me.Label1.Anchor = System.Windows.Forms.AnchorStyles.Right
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(4, 86)
        Me.Label1.MinimumSize = New System.Drawing.Size(100, 13)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(100, 13)
        Me.Label1.TabIndex = 10
        Me.Label1.Text = "Ebay:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lbStatus
        '
        Me.lbStatus.FormattingEnabled = True
        Me.lbStatus.Location = New System.Drawing.Point(7, 120)
        Me.lbStatus.Name = "lbStatus"
        Me.lbStatus.Size = New System.Drawing.Size(425, 56)
        Me.lbStatus.TabIndex = 9
        '
        'ctrlTestStatus
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.TableLayoutPanel1)
        Me.Controls.Add(Me.lbStatus)
        Me.Name = "ctrlTestStatus"
        Me.Size = New System.Drawing.Size(440, 185)
        Me.TableLayoutPanel1.ResumeLayout(False)
        Me.TableLayoutPanel1.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents TableLayoutPanel1 As TableLayoutPanel
    Friend WithEvents prgLocalDatabase As ProgressBar
    Friend WithEvents prgRemoteDatabase As ProgressBar
    Friend WithEvents lblLocalDatabaseStatus As Label
    Friend WithEvents lblRemoteDatabaseStatus As Label
    Friend WithEvents Label4 As Label
    Friend WithEvents Label5 As Label
    Friend WithEvents prgAmazon As ProgressBar
    Friend WithEvents Label6 As Label
    Friend WithEvents lblAmazonStatus As Label
    Friend WithEvents prgEbay As ProgressBar
    Friend WithEvents lblEbayStatus As Label
    Friend WithEvents Label1 As Label
    Friend WithEvents lbStatus As ListBox
End Class

和表单的设计器代码我将控件放到(在我放入之前):

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class frmTestStart
    Inherits System.Windows.Forms.Form

    'Form 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.SuspendLayout()
        '
        'frmTestStart
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(468, 206)
        Me.Name = "frmTestStart"
        Me.Text = "Test Connections"
        Me.ResumeLayout(False)

    End Sub

End Class

设计师在拖动控件并保存后生成的内容:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class frmTestStart
    Inherits System.Windows.Forms.Form

    'Form 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.CtrlTestStatus1 = New QBI.ctrlTestStatus()
        Me.SuspendLayout()
        '
        'CtrlTestStatus1
        '
        Me.CtrlTestStatus1.Location = New System.Drawing.Point(13, 13)
        Me.CtrlTestStatus1.Name = "CtrlTestStatus1"
        Me.CtrlTestStatus1.Size = New System.Drawing.Size(440, 185)
        Me.CtrlTestStatus1.TabIndex = 0
        '
        'frmTestStart
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(468, 206)
        Me.Controls.Add(Me.CtrlTestStatus1)
        Me.Name = "frmTestStart"
        Me.Text = "Test Connections"
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents CtrlTestStatus1 As QBI.ctrlTestStatus
End Class

我在QBI.ctrlTestStatus()下得到了波浪线,如果我尝试编译它会产生错误。我认为这是设计器中的命名空间错误,所以我立即将用户控件包装起来(没有欢乐)。项目命名空间是QBI(据我所知,它不是保留字)。

Namespace QBI
    Public Class ctrlTestStatus
        Private Sub ctrlTestStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub
    End Class

End Namespace

不幸的是,这会产生更多问题,并且CtrlTestStatus1实例的属性和事件不可用(我得到了波浪线)。

我发现MS的一个问题是将编译设置为任何CPU(项目构建始终设置为此)由于64位/ 32位问题(VS是STILL 32位!),但也没有任何乐趣。

关于如何解决这个问题的任何想法(设置,我做错了)?

1 个答案:

答案 0 :(得分:1)

旧帖子,但对于其他遇到此问题的人...

我在一个项目中发生了这种情况,在该项目中我定义了一个与我的项目(根名称空间)同名的子命名空间(在OP中为“ QBI”)。当您的项目中的代码如下所示时,就会发生这种情况:

Namespace [Name of your Project/Root Namespace] [...] End Namespace

其结果是您的项目同时包含“ QBI”命名空间和“ QBI.QBI”命名空间(如果您查看项目的对象浏览器,则可以自己看到)。这种情况会在设计器文件中引起解析冲突,从而使它在自定义控件的错误名称空间下出现。

几种解决方法:

  1. 在声明了子命名空间的地方(如上面的示例代码所示),请完全删除命名空间声明。这会将声明中的所有代码移动到您的根“ QBI”命名空间。如果不需要在子命名空间中使用该代码,则可以执行此操作。
  2. 在声明子命名空间的位置,右键单击“命名空间”名称,并将其重命名为除“根命名空间”名称之外的其他名称。
  3. 如果要保留这样的子命名空间,可以改为从“我的项目”->“应用程序”->“根”命名空间中将根命名空间重命名为唯一的名称。

选项1是最简单的选项,应立即解决该错误。

选项2是下一个最简单的方法,但是,例如,如果将子命名空间重命名为“ NewName”,则Designer文件中所有对“ ctrlTestStatus”的引用都将错误地重命名为“ NewName.ctrlTestStatus”,并且您必须将其修复为“ QBI.ctrlTestStatus”。

选项3最为繁琐,因为您必须使用“查找和替换”来解析所有对Root名称空间的Designer文件引用,请注意不要错误地替换对子QBI名称空间的合法引用。