【问题标题】:What is the correct declaration of a form in VB.NET?VB.NET 中正确的表单声明是什么?
【发布时间】:2012-10-16 17:53:54
【问题描述】:

我只是在 Microsoft 升级向导的帮助下将一个项目从 VB6 移植到 VB.NET。

一个表单自动升级到

Friend Partial Class frmAudFeedSentenceEdit
    Inherits System.Windows.Forms.Form

但是当我在 VB.NET 的新项目中添加一个新表单时,它被声明为

 Public Class frmAudFeedSentenceEdit

(没有继承 System.Windows.Forms.Form)。

请问哪一个是正确的?

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    当您使用 Visual Studio 制作表单时,它会将表单信息放入 2 个单独的文件中。

    你看到的部分:

    Public Class frmAudFeedSentenceEdit
    

    只是主文件中的声明,您通常会在其中工作。

    但是,创建了第二个文件 (frmAudFeedSentenceEdit.Designer.vb),其中包含:

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

    如您所见,它仍然是Partial Class,但Inherits 语句放置在设计器为您生成的文件中。

    如果您查看 Class View 窗口,浏览到您的表单,然后双击 InitializeComponent()(因为此 Sub 在设计器文件中定义),您可以看到这一点。

    【讨论】:

    • @MarkJ VB 这种方式很有趣 - 与 C# 不同,您可以有一个 Partial 类,其中只有一侧(设计器)被装饰为 Partial,所以它并不总是很明显它是一种形式 ;)
    • 很好的答案!非常感谢!
    • 但是当它升级我的VB6代码时为什么它说“朋友”?或者更准确地说:“朋友”关键字在表单中的作用是什么?
    • @tmighty 这是可访问性。默认情况下,当您升级时,它会成为朋友。现在默认情况下是“公共”的(因此您可以从其他程序集中使用它们)。见msdn.microsoft.com/en-us/library/08w05ey2.aspxmsdn.microsoft.com/en-us/library/76453kax.aspx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2019-05-09
    相关资源
    最近更新 更多