VB.NET Me.Close() 不起作用,窗体没有关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/755643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:10:10  来源:igfitidea点击:

VB.NET Me.Close() doesn't work, the form doesn't close?

vb.net

提问by Scott

The form is an About Us form so has nothing on it only a text box and a OK button.

该表单是一个关于我们的表单,所以上面没有任何内容,只有一个文本框和一个确定按钮。

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

Here is how I'm opening the form:

这是我打开表格的方式:

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

Why won't the button close the form? I'm puzzled, I tried another button just in case with the same result.

为什么按钮不会关闭表单?我很困惑,为了防止同样的结果,我尝试了另一个按钮。

UPDATE: I set a break point on Me.Close() and it isn't reaching it when I click the button, I created a new button and the same thing happened.

更新:我在 Me.Close() 上设置了一个断点,但当我单击按钮时它没有到达它,我创建了一个新按钮并且发生了同样的事情。

Thanks

谢谢

回答by Jeff Olson

I am betting the event handler for the button1_click event has been inadvertently removed.

我打赌 button1_click 事件的事件处理程序已被无意中删除。

Try double-clicking on the button in design time and see if it pulls you back to that same exact piece of code - or a new event handler definition.

尝试在设计时双击按钮,看看它是否会将您拉回到完全相同的代码段 - 或新的事件处理程序定义。

If it's a new event handler definition - copy your code there and delete the first one.

如果它是一个新的事件处理程序定义 - 将您的代码复制到那里并删除第一个。

There are other ways to manually add the event handler in the designer's code-behind - but maybe that's for a later progression.

还有其他方法可以在设计器的代码隐藏中手动添加事件处理程序 - 但也许这是为了以后的进展。

From within VS click the "Show all files" button in solutions explorer. Grab us the code in .Designer.vb and paste it in here and we'll nail it down for you definitively.

在 VS 中,单击解决方案资源管理器中的“显示所有文件”按钮。将 .Designer.vb 中的代码交给我们并将其粘贴到此处,我们将明确为您确定。

Here's mine:

这是我的:


    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

回答by Anton Gogolev

From MSDN:

来自 MSDN:

Showing the control is equivalent to setting the Visible property to true. After the Show method is called, the Visible property returns a value of true until the Hide method is called.

显示控件等效于将 Visible 属性设置为 true。调用 Show 方法后,Visible 属性返回 true 值,直到调用 Hide 方法。

回答by Fredou

when formabout is open

当 formabout 打开时

click on pause(break all) button in visual studio

单击 Visual Studio 中的暂停(全部中断)按钮

click on step into in debug in visual studio

在 Visual Studio 中单击进入调试

click on the close button in formabout

单击 formabout 中的关闭按钮

you will see which code is executed, if any

您将看到执行了哪些代码(如果有)

* edit *

* 编辑 *

another question

另一个问题

is formabout.enabled property is true?

formabout.enabled 属性是真的吗?

回答by dbasnett

I tested the following

我测试了以下内容

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

and did not have problems. As was suggested, re-create your button and code.

并且没有问题。按照建议,重新创建您的按钮和代码。

回答by Kshitij

Add you Button Dynamically may solve the problem. Place the following code in the load event of about Form.

动态添加您的按钮可能会解决问题。将以下代码放在about Form的load事件中。

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

回答by Ravikumar

Simple.

简单的。

  1. Select Project Properties from Solution Explorer.
  2. Select Security tab to either uncheck "Enable ClickOnce..." or select "This is a full trust application".
  3. Save the properties settings.
  1. 从解决方案资源管理器中选择项目属性。
  2. 选择“安全”选项卡以取消选中“启用 ClickOnce...”或选择“这是一个完全信任的应用程序”。
  3. 保存属性设置。

Solved.

解决了。