简体   繁体   English

MyProject.MyClass-vb.NET自定义控件

[英]MyProject.MyClass - vb.NET custom controls

In a Visual Basic project, I created a homemade TabControl in order to fix a visual bug. 在Visual Basic项目中,我创建了一个自制的TabControl,以修复视觉错误。 The control works properly, however whenever I modify the form using my tab, Visual Studio adds MyProject in front of the control in its declaration: 该控件正常工作,但是每当我使用选项卡修改表单时,Visual Studio都会在其声明的控件前面添加MyProject:

Me.tabMenu = New MyProject.MyClass 'Gives a BC30002 compile error

If I remove the MyProject. 如果删除MyProject. , the project compiles properly. ,项目会正确编译。

MyClass is in a separate file MyClass.vb and looks mostly like this: MyClass在一个单独的文件MyClass.vb中,看起来像这样:

Public Class MyClass
Inherits System.Windows.Forms.TabControl

Public Sub New()
    InitializeComponent()
    MyBase.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed
End Sub

Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
    //OnDrawItem code
End Sub

Private Sub My_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
    //My_DrawItem code
End Sub
End Class

I tried removing the file and adding it again, copy and pasting the class inside MyForm.designer.vb , adding MyProject. 我尝试删除文件并再次添加它,将类复制并粘贴到MyForm.designer.vb ,添加MyProject. to the class name, but nothing stopped Visual Studio from adding this so-hated MyProject . 到类名,但是没有什么阻止Visual Studio添加这个讨厌的MyProject

Edit regarding this answer : 编辑有关此答案

I understand the thing about the namespace, however my problem is mostly that the compiler does not recognize the class with the project name appended but still adds it everytime. 我了解有关名称空间的知识,但是我的问题主要是编译器无法识别附加了项目名称的类,但仍然每次都添加它。

What is the actual compile error you are getting? 您得到的实际编译错误是什么? Is it possable that the VB compiler is interpreting MyProject as something other than a namespace identifier? VB编译器是否可能将MyProject解释为名称空间标识符以外的其他内容? You could also try changing the default namespace for the project, then see what it does, it might give you a hint as to what the actual problem is. 您还可以尝试更改项目的默认名称空间,然后查看其作用,这可能会提示您实际的问题是什么。 You could also try changing the offending line to 您也可以尝试将违规行更改为

Me.tabMenu = New Global.MyProject.MyClass

then let us know what the results are. 然后让我们知道结果如何。

By default, Visual Basic .NET assigned a default namespace to your project. 默认情况下,Visual Basic .NET为您的项目分配了默认名称空间。 (I believe the default is, in fact, MyProject .) (我相信默认值实际上是MyProject 。)

This is what's being prepended, and it's being done to explicitly identify your class in the designer. 这就是前提,并且正在完成以在设计器中明确标识您的类。

No matter what your default namespace is for your project, the WinForms designer will add the namespace name to the .designer.vb file. 无论您的项目使用的默认名称空间是什么,WinForms设计器都会将名称空间名称添加到.designer.vb文件中。

To change the default namespace, go to your project properties; 要更改默认名称空间,请转至项目属性。 it should appear on the first tab. 它应该出现在第一个标签上。

Also, generally, don't modify the .designer.vb files if you can avoid it. 同样,通常,如果可以避免的话,请勿修改.designer.vb文件。 Those files get completely blown away and rebuilt by Visual Studio often , so your changes will more likely than not be eliminated. 这些文件经常被Visual Studio完全吹走并重建,因此您的更改很有可能被消除。

I've seen this before when you have a public module with the same name as your default namespace (project name). 当您拥有一个与默认名称空间(项目名称)同名的公共模块时,我就已经看到了。 If that's the case, either rename the module or the default namespace and the problem should go away,. 如果是这种情况,请重命名模块或默认名称空间,问题应该会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM