Bizarre bug: Can't close form

T

Tales Normando

Hi,

One day, when I was testing my application, I tried to use the
main form's close button to exit it, but it didn't work. I right-clicked the
taskbar's icon and used the close menu item, but it didn't work either. I
stopped execution and added an exit menu item (using Form.Close(); the
application is in the early stages of development), but it also didn't work
when I ran it again. What's wrong? I haven't done anything out of the
ordinary to prevent the form from closing!
Here is the form's code. I'm still using VB.NET 2002 & .NET 1.0.
Help!


' Declaração de importações

Imports System.Windows.Forms

Imports Normando.Focus.Controls

Public Class Main

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

' Associa os eventos da botão Serviços

AddHandler ServicesPage.Click, AddressOf Pages_Click

AddHandler ServicesPage.MouseEnter, AddressOf Pages_MouseEnter

AddHandler ServicesPage.MouseLeave, AddressOf Pages_MouseLeave

' Associa os eventos da botão Cadastros

AddHandler RecordsPage.Click, AddressOf Pages_Click

AddHandler RecordsPage.MouseEnter, AddressOf Pages_MouseEnter

AddHandler RecordsPage.MouseLeave, AddressOf Pages_MouseLeave

' Associa os eventos da botão Relatórios

AddHandler ReportsPage.Click, AddressOf Pages_Click

AddHandler ReportsPage.MouseEnter, AddressOf Pages_MouseEnter

AddHandler ReportsPage.MouseLeave, AddressOf Pages_MouseLeave

' Associa os eventos da botão Opções

AddHandler OptionsPage.Click, AddressOf Pages_Click

AddHandler OptionsPage.MouseEnter, AddressOf Pages_MouseEnter

AddHandler OptionsPage.MouseLeave, AddressOf Pages_MouseLeave

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

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.

Friend WithEvents Tray As System.Windows.Forms.NotifyIcon

Friend WithEvents MenuBar As System.Windows.Forms.MainMenu

Friend WithEvents Pages As System.Windows.Forms.Panel

Friend WithEvents OptionsPage As System.Windows.Forms.Label

Friend WithEvents ReportsPage As System.Windows.Forms.Label

Friend WithEvents RecordsPage As System.Windows.Forms.Label

Friend WithEvents ServicesPage As System.Windows.Forms.Label

Friend WithEvents Page As System.Windows.Forms.Panel

Friend WithEvents FileMenu As System.Windows.Forms.MenuItem

Friend WithEvents FileExit As System.Windows.Forms.MenuItem

Friend WithEvents HelpMenu As System.Windows.Forms.MenuItem

Friend WithEvents HelpContents As System.Windows.Forms.MenuItem

Friend WithEvents HelpSeparator As System.Windows.Forms.MenuItem

Friend WithEvents HelpAbout As System.Windows.Forms.MenuItem

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container()

Me.Tray = New System.Windows.Forms.NotifyIcon(Me.components)

Me.MenuBar = New System.Windows.Forms.MainMenu()

Me.FileMenu = New System.Windows.Forms.MenuItem()

Me.FileExit = New System.Windows.Forms.MenuItem()

Me.HelpMenu = New System.Windows.Forms.MenuItem()

Me.HelpContents = New System.Windows.Forms.MenuItem()

Me.HelpSeparator = New System.Windows.Forms.MenuItem()

Me.HelpAbout = New System.Windows.Forms.MenuItem()

Me.Pages = New System.Windows.Forms.Panel()

Me.OptionsPage = New System.Windows.Forms.Label()

Me.ReportsPage = New System.Windows.Forms.Label()

Me.RecordsPage = New System.Windows.Forms.Label()

Me.ServicesPage = New System.Windows.Forms.Label()

Me.Page = New System.Windows.Forms.Panel()

Me.Pages.SuspendLayout()

Me.SuspendLayout()

'

'Tray

'

Me.Tray.Text = ""

Me.Tray.Visible = True

'

'MenuBar

'

Me.MenuBar.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
{Me.FileMenu, Me.HelpMenu})

'

'FileMenu

'

Me.FileMenu.Index = 0

Me.FileMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
{Me.FileExit})

Me.FileMenu.Text = "&Arquivo"

'

'FileExit

'

Me.FileExit.Index = 0

Me.FileExit.Text = "Sai&r"

'

'HelpMenu

'

Me.HelpMenu.Index = 1

Me.HelpMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
{Me.HelpContents, Me.HelpSeparator, Me.HelpAbout})

Me.HelpMenu.Text = "A&juda"

'

'HelpContents

'

Me.HelpContents.Index = 0

Me.HelpContents.Text = "&Conteúdo"

'

'HelpSeparator

'

Me.HelpSeparator.Index = 1

Me.HelpSeparator.Text = "-"

'

'HelpAbout

'

Me.HelpAbout.Index = 2

Me.HelpAbout.Text = "&Sobre o Focus Café..."

'

'Pages

'

Me.Pages.BackColor = System.Drawing.Color.FromArgb(CType(64, Byte),
CType(64, Byte), CType(64, Byte))

Me.Pages.Controls.AddRange(New System.Windows.Forms.Control()
{Me.OptionsPage, Me.ReportsPage, Me.RecordsPage, Me.ServicesPage})

Me.Pages.Dock = System.Windows.Forms.DockStyle.Top

Me.Pages.Name = "Pages"

Me.Pages.Size = New System.Drawing.Size(488, 24)

Me.Pages.TabIndex = 6

'

'OptionsPage

'

Me.OptionsPage.BackColor = System.Drawing.Color.Transparent

Me.OptionsPage.ForeColor = System.Drawing.Color.LightGray

Me.OptionsPage.Location = New System.Drawing.Point(184, 2)

Me.OptionsPage.Name = "OptionsPage"

Me.OptionsPage.Size = New System.Drawing.Size(56, 20)

Me.OptionsPage.TabIndex = 3

Me.OptionsPage.Tag = "OrdersPage"

Me.OptionsPage.Text = "Opções"

Me.OptionsPage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'ReportsPage

'

Me.ReportsPage.BackColor = System.Drawing.Color.Transparent

Me.ReportsPage.ForeColor = System.Drawing.Color.LightGray

Me.ReportsPage.Location = New System.Drawing.Point(120, 2)

Me.ReportsPage.Name = "ReportsPage"

Me.ReportsPage.Size = New System.Drawing.Size(64, 20)

Me.ReportsPage.TabIndex = 2

Me.ReportsPage.Tag = "SuppliersPage"

Me.ReportsPage.Text = "Relatórios"

Me.ReportsPage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'RecordsPage

'

Me.RecordsPage.BackColor = System.Drawing.Color.Transparent

Me.RecordsPage.ForeColor = System.Drawing.Color.LightGray

Me.RecordsPage.Location = New System.Drawing.Point(56, 2)

Me.RecordsPage.Name = "RecordsPage"

Me.RecordsPage.Size = New System.Drawing.Size(64, 20)

Me.RecordsPage.TabIndex = 1

Me.RecordsPage.Tag = "ClientsPage"

Me.RecordsPage.Text = "Cadastros"

Me.RecordsPage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'ServicesPage

'

Me.ServicesPage.BackColor = System.Drawing.Color.Transparent

Me.ServicesPage.ForeColor = System.Drawing.Color.LightGray

Me.ServicesPage.Location = New System.Drawing.Point(0, 2)

Me.ServicesPage.Name = "ServicesPage"

Me.ServicesPage.Size = New System.Drawing.Size(56, 20)

Me.ServicesPage.TabIndex = 0

Me.ServicesPage.Tag = "StartPage"

Me.ServicesPage.Text = "Serviços"

Me.ServicesPage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'

'Page

'

Me.Page.Dock = System.Windows.Forms.DockStyle.Fill

Me.Page.Location = New System.Drawing.Point(0, 24)

Me.Page.Name = "Page"

Me.Page.Size = New System.Drawing.Size(488, 374)

Me.Page.TabIndex = 7

'

'Main

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.BackColor = System.Drawing.Color.FromArgb(CType(224, Byte), CType(224,
Byte), CType(224, Byte))

Me.ClientSize = New System.Drawing.Size(488, 398)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Page, Me.Pages})

Me.Menu = Me.MenuBar

Me.Name = "Main"

Me.Text = "Focus Café"

Me.Pages.ResumeLayout(False)

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Pages_MouseEnter(ByVal sender As Object, ByVal e As
System.EventArgs)

' Ilumina a etiqueta da página

Dim PageLabel As Label = DirectCast(sender, Label)

PageLabel.BackColor = Color.LightGray

PageLabel.ForeColor = Color.DimGray

End Sub

Private Sub Pages_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs)

' Escurece a etiqueta da página

Dim PageLabel As Label = DirectCast(sender, Label)

PageLabel.BackColor = Color.Transparent

PageLabel.ForeColor = Color.Gainsboro

End Sub

Private Sub Pages_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Obtém o botão pressionado

Dim Button As Label = DirectCast(sender, Label)

' Simplifica o acesso à coleção de controles

With Page

' Testa se a página ativa não é a mesma que tenta-se

' exibir

If Not .Controls(0).GetType().Name.ToUpper() = Button.Name.ToUpper() Then

' Remove a página anterior

..Controls.Clear()

' Exibe a nova página

..Controls.Add(Activator.CreateInstance(Type.GetType("Normando.Focus.Cafe.Ser
ver." + Button.Name)))

..Controls(0).Dock = DockStyle.Fill

End If

End With

End Sub

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Cria a página inicial

Dim StartPage As ServicesPage = New ServicesPage()

StartPage.Dock = DockStyle.Fill

' Adiciona a página inicial no container

Page.Controls.Add(StartPage)

End Sub

Private Sub FileExit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles FileExit.Click

' Sai do programa

Close()

End Sub

End Class
 
H

Herfried K. Wagner [MVP]

* "Tales Normando said:
One day, when I was testing my application, I tried to use the
main form's close button to exit it, but it didn't work. I right-clicked the
taskbar's icon and used the close menu item, but it didn't work either. I
stopped execution and added an exit menu item (using Form.Close(); the
application is in the early stages of development), but it also didn't work
when I ran it again. What's wrong? I haven't done anything out of the
ordinary to prevent the form from closing!

Maybe the bug is one of the bug listed here?

<http://www.jelovic.com/dotnetbugs/index.html>
 
E

Eric Cadwell

I had this happen to me as well. It sounds related to the first bug on the
list.

I got around it by calling Hide() after getting the WM_CLOSE msg.

protected override void WndProc(ref System.Windows.Forms.Message m)
{
const int WM_CLOSE = 0x0010;
switch(m.Msg)
{
case WM_CLOSE:
Hide();
break;
}
base.WndProc(ref m);
}


HTH;
Eric Cadwell
http://www.origincontrols.com
 
1

100

Hi Tales,
The problem is that you clear Controls collection of the form. This is well
know bug in the windows forms framework.
If you remove control which is currently focused the form fails to clear
some internal stuff and tries to validate this control. As a result of this,
Form.Closing event receives event args with Cancel property set to 'true',
which stops the form and the application from closing.
There are couple of workarrounds, but I suggest you to call
Form.OnControlRemoved along with the reference to the controls in the
Controls collection before calling Controls.Clear() method.

HTH
B\rgds
100
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top