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



Question:

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.

Thanks


Solution:1

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

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.

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  


Solution:2

From 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.


Solution:3

when formabout is open

click on pause(break all) button in visual studio

click on step into in debug in visual studio

click on the close button in formabout

you will see which code is executed, if any

* edit *

another question

is formabout.enabled property is true?


Solution:4

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.


Solution:5

Add you Button Dynamically may solve the problem. Place the following code in the load event of about 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  


Solution:6

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.

Solved.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »