开发者

Inheritance works for first descendant but not next. Why?

开发者 https://www.devze.com 2023-02-27 04:56 出处:网络
Form1 Public Class Form1 Private Sub But_Bell_Click(sender As System.Object, e As System.EventArgs) Handles But_Bell.Click

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

End Class

First Decendant

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
    '
  开发者_StackOverflow中文版  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

End Class

Inheritance works for first descendant but not next. Why?

Second Descendant

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

End Class

Inheritance works for first descendant but not next. Why?

Where has the whistle button gone?

The class part of the inheritance has works because you can access it via code.


Try calling MyBase.InitializeComponent() from the second descendant. You'll probably have to change the access level of it, too.

EDIT

This was bugging me all night. It turns out that its a case of missing constructors. If you use reflector you'll see that Form1 has a constructor that calls Me.InitializeComponent() even though that doesn't exist in either Form1.vb or Form1.Designer.vb.

<DesignerGenerated> _
Public Class Form1
    Inherits Form
    ' Methods
    Public Sub New()
        Me.InitializeComponent
    End Sub

    ...
End Class

If you create a C# WinForms app the constructor is visible so this makes me think its a VB thing to hide it. Also, if you manually add a Sub New to Form1 it will fill in some code for you, essentially "unhiding it".

I'm guessing that VS looked at your code and realized that it was a descendant of System.Windows.Forms.Form but technically it was done improperly since it didn't call MyBase.New() (since it couldn't because it didn't exist) so it was just trying to guess. It "knows" to add a call to InitializeComponent() in the form that it created and it "knows" to do that for the form that you're looking at but it isn't bothering to walk the chain of forms and do it for all of them. Bug? Maybe.

When you set MoreBellsAndWhistles as a startup form you never see either of the new buttons, right? This is how you can tell its more of a VS trickery involved.

Anyway, the solution to the entire problem is to add this to both sub classes:

Public Sub New()
    MyBase.New()
    InitializeComponent()
End Sub

And add this to Form1:

Public Sub New()
    InitializeComponent()
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消