Form1
Public Class Form1
Private Sub But_Bell_Click(sender As System.Object, e As System.EventArgs) Handles But_Bell.Click
MessageBox.Show("Ding a ling")
End Sub端级
第一斜角
Public Class BellsAndWhistles
Inherits Form1
Friend WithEvents But_Whistle As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.But_Whistle = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'But_Whistle
'
Me.But_Whistle.Location = New System.Drawing.Point(112, 38)
Me.But_Whistle.Name = "But_Whistle"
Me.But_Whistle.Size = New System.Drawing.Size(75, 23)
Me.But_Whistle.TabIndex = 1
Me.But_Whistle.Text = "Whistle"
Me.But_Whistle.UseVisualStyleBackColor = True
'
'BellsAndWhistles
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.But_Whistle)
Me.Name = "BellsAndWhistles"
Me.Text = "Bells & Whistles"
Me.Controls.SetChildIndex(Me.But_Whistle, 0)
Me.ResumeLayout(False)
End Sub
Private Sub But_Whistle_Click(sender As System.Object, e As System.EventArgs) Handles But_Whistle.Click
MessageBox.Show("Toot Toot")
End Sub端级

第二后裔
Public Class MoreBellsAndWhistles
Inherits BellsAndWhistles
Friend WithEvents MoreBells As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.MoreBells = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'MoreBells
'
Me.MoreBells.Location = New System.Drawing.Point(30, 145)
Me.MoreBells.Name = "MoreBells"
Me.MoreBells.Size = New System.Drawing.Size(75, 23)
Me.MoreBells.TabIndex = 1
Me.MoreBells.Text = "More Bells"
Me.MoreBells.UseVisualStyleBackColor = True
'
'MoreBellsAndWhistles
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.MoreBells)
Me.Name = "MoreBellsAndWhistles"
Me.Text = "MoreBellsAndWhistles"
Me.Controls.SetChildIndex(Me.MoreBells, 0)
Me.ResumeLayout(False)
End Sub
Private Sub MoreBells_Click(sender As System.Object, e As System.EventArgs) Handles MoreBells.Click
MessageBox.Show("Ting TIng")
End Sub
Private Sub MoreBellsAndWhistles_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub端级

哨子按钮在哪里?
继承的类部分可以工作,因为您可以通过代码访问它。
发布于 2011-04-20 21:35:39
尝试从第二个后代调用MyBase.InitializeComponent()。您可能也必须更改它的访问级别。
编辑
整晚都在烦我。事实证明,这是一个缺少构造函数的案例。如果使用反射器,您将看到Form1有一个调用Me.InitializeComponent()的构造函数,即使在Form1.vb或Form1.Designer.vb中都不存在这个构造函数。
<DesignerGenerated> _
Public Class Form1
Inherits Form
' Methods
Public Sub New()
Me.InitializeComponent
End Sub
...
End Class如果您创建了一个C# WinForms应用程序,构造函数是可见的,所以这让我觉得隐藏它是VB的事情。另外,如果您手动将一个Sub New添加到Form1中,它将为您填写一些代码,本质上是“取消隐藏”。
我猜VS看了一下您的代码,并意识到它是System.Windows.Forms.Form的后代,但从技术上讲,这是不正确的,因为它没有调用MyBase.New() (因为它不能,因为它不存在),所以它只是试图猜测。它“知道”在它创建的表单中添加对InitializeComponent()的调用,并且它“知道”为您正在查看的表单添加调用,但遍历表单链并为所有表单执行该操作并不费事。窃听器?也许吧。
当您将MoreBellsAndWhistles设置为启动形式时,您永远看不到任何一个新按钮,对吗?这就是你如何告诉它更多的VS欺骗所涉及的。
无论如何,整个问题的解决方案是将其添加到两个子类中:
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub并将其添加到Form1中
Public Sub New()
InitializeComponent()
End Subhttps://stackoverflow.com/questions/5736589
复制相似问题