2009-04-16 39 views
1

該表格是一個關於我們的表單,因此它只有一個文本框和一個OK按鈕。VB.NET Me.Close()不起作用,窗體不關閉?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Me.Close() 
End Sub 

這裏是如何我打開表單:

Private Sub AboutAppStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutAppStripMenuItem.Click 
    Dim formAbout As New FormAbout() 
    formAbout.Show() 
End Sub 

爲什麼不會在按鈕關閉的形式?我很困惑,爲了防止相同的結果,我嘗試了另一個按鈕。

更新:我在Me.Close()上設置了一個斷點,當它單擊按鈕時我沒有觸及它,我創建了一個新按鈕併發生了同樣的事情。

感謝

+0

formabout.show後面的代碼是什麼? – Fredou 2009-04-16 11:19:10

+0

編輯顯示formabout.show後沒有代碼。 – Scott 2009-04-16 11:21:54

+0

你有沒有改變button1的名字?這將打破事件佈線... – 2009-04-16 11:28:52

回答

0

從MSDN:

顯示控制相當於Visible屬性設置爲true。 Show方法被調用後,Visible屬性返回值爲true,直到調用Hide方法。

2

我打賭button1_click事件的事件處理程序已被無意中刪除。

嘗試在設計時間內雙擊按鈕,看看它是否將您拉回到相同的確切代碼段 - 或新的事件處理程序定義。

如果是新的事件處理程序定義 - 將代碼複製到那裏並刪除第一個。

還有其他方法可以在設計器的代碼隱藏中手動添加事件處理程序 - 但也許這是爲了以後的進程。

從VS中單擊解決方案資源管理器中的「顯示所有文件」按鈕。在.Designer.vb中抓取代碼並將其粘貼到此處,我們將爲您明確指定它。

這裏是我的:


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

    'Form overrides dispose to clean up the component list. 
    _ 
    Protected Overrides Sub Dispose(ByVal disposing As Boolean) 
     Try 
      If disposing AndAlso components IsNot Nothing Then 
       components.Dispose() 
      End If 
     Finally 
      MyBase.Dispose(disposing) 
     End Try 
    End Sub 

    'Required by the Windows Form Designer 
    Private components As System.ComponentModel.IContainer 

    'NOTE: The following procedure is required by the Windows Form Designer 
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor. 
    _ 
    Private Sub InitializeComponent() 
     Me.Button1 = New System.Windows.Forms.Button 
     Me.SuspendLayout() 
     ' 
     'Button1 
     ' 
     Me.Button1.Location = New System.Drawing.Point(131, 91) 
     Me.Button1.Name = "Button1" 
     Me.Button1.Size = New System.Drawing.Size(133, 50) 
     Me.Button1.TabIndex = 0 
     Me.Button1.Text = "Button1" 
     Me.Button1.UseVisualStyleBackColor = True 
     ' 
     'Form1 
     ' 
     Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) 
     Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font 
     Me.ClientSize = New System.Drawing.Size(292, 266) 
     Me.Controls.Add(Me.Button1) 
     Me.Name = "Form1" 
     Me.Text = "Form1" 
     Me.ResumeLayout(False) 

    End Sub 
    Friend WithEvents Button1 As System.Windows.Forms.Button 

End Class 
0

時formabout是開放

點擊暫停(打破所有)按鈕在Visual Studio

點擊一步進入調試在Visual Studio

點擊關閉按鈕,形成關於

y OU將看到的是執行其代碼,如果任何

*編輯*

另一個問題

是formabout.enabled屬性爲true?

0

我測試了以下

Public Class Form1 
Private Sub Button1_Click(ByVal sender As System.Object, _ 
          ByVal e As System.EventArgs) _ 
          Handles Button1.Click 
    Dim f As New Form2 
    f.Show() 
End Sub 
End Class 


Public Class Form2 
Private Sub Button1_Click(ByVal sender As System.Object, _ 
          ByVal e As System.EventArgs) _ 
          Handles Button1.Click 
    Me.Close() 
End Sub 
End Class 

,並沒有問題。如所建議的那樣,重新創建您的按鈕和代碼。

0

Add you Button動態地可以解決問題。 將下面的代碼放在關於Form的加載事件中。

Public Sub FormAbout_Load(ByVal sender as object,ByVal e as System.EventArgs)Handles Me.Load 

Dim btn as new Button() 
AddHandler btn.Click ,AddressOf _ClickToClose 

End Sub 

Private Sub _ClickToClose(ByVal sender as object,ByVal e as System.EventArgs) 
    Me.Close() 
End Sub 
0

很簡單。

  1. 從解決方案資源管理器中選擇項目屬性。
  2. 選擇安全選項卡以取消選中「啓用ClickOnce ...」或選擇「這是完全信任的應用程序」。
  3. 保存屬性設置。

已解決。