2011-04-20 77 views
1

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 

末級

首先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 
    ' 
    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 

末級

First Decendent Designer

二後代

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 

末級

Second Descendant Designer

哪裏有汽笛按鈕哪裏去了?

繼承的類部分有效,因爲您可以通過代碼訪問它。

回答

2

嘗試從第二個子代調用MyBase.InitializeComponent()。您可能還必須更改其訪問級別。

編輯

這是竊聽我一整夜。事實證明,它是一個缺少構造函數的情況。如果使用反射器,則會看到Form1的構造函數調用Me.InitializeComponent(),即使Form1.vbForm1.Designer.vb中不存在該構造函數。

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

    ... 
End Class 

如果您創建一個C#WinForms應用程序的構造函數是可見的,所以這讓我覺得它是一個VB的東西來隱藏它。另外,如果您手動添加Sub NewForm1,它會爲您填充一些代碼,實質上是「取消隱藏它」。

我猜VS看着你的代碼,並意識到這是的System.Windows.Forms.Form後裔,但在技術上它是不正確完成,因爲它沒叫(因爲它不能因爲它不存在),因此它只是在猜測。它「知道」以其創建的形式添加對InitializeComponent()的調用,並且「知道」要爲您正在查看的表單執行該操作,但它並不會打擾表單的所有步驟,並且不會執行所有操作其中。錯誤?也許。

當您將MoreBellsAndWhistles設置爲啓動窗體時,您從未看到任何一個新按鈕,對不對?這就是你可以告訴它更多涉及VS技巧的方法。

無論如何,解決整個問題是它添加到兩個子類:

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

並添加這Form1

Public Sub New() 
    InitializeComponent() 
End Sub 
+0

如果是這樣,那麼爲什麼不第一後裔也需要它? – 2011-04-20 22:23:45

+0

@Adam Speight,看我更新的帖子。它基本上VS猜測。 – 2011-04-21 13:28:08

+0

謝謝你解決了這個問題。我想知道由vb.net的默認表單實例被VS Designer取代而導致的「真實」原因。 – 2011-04-21 16:14:12

相關問題