9 Replies - 5495 Views - Last Post: 02 June 2011 - 01:11 PM Rate Topic: -----

#1 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Form closing event override

Posted 01 June 2011 - 05:26 PM

I have this code that's suppose to override the form closing event, It fires off sucessfully however doesn't prevent the form from closing. Anybody see what I'm missing?

Private Sub Form_Close(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Me.Hide()
        e.Cancel = True
    End Sub


I referenced this site for help:

Is This A Good Question/Topic? 0
  • +

Replies To: Form closing event override

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8393
  • View blog
  • Posts: 31,213
  • Joined: 12-June 08

Re: Form closing event override

Posted 01 June 2011 - 05:30 PM

Try "me.closing" and not "mybase.closing".
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Form closing event override

Posted 01 June 2011 - 05:32 PM

Put the e.Cancel first before hide(). I used it and it does work
Was This Post Helpful? 0
  • +
  • -

#4 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Form closing event override

Posted 01 June 2011 - 05:37 PM

No still same thing unfortunately
Was This Post Helpful? 0
  • +
  • -

#5 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 310
  • Joined: 12-January 11

Re: Form closing event override

Posted 02 June 2011 - 09:38 AM

I have a program that uses a very similar block of code and it works fine. I will add the code below in case I have something you don't:

Private Sub testSelection_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        'if the user is trying to close the window
        If e.CloseReason = CloseReason.UserClosing Then
            'cancel the close
            e.Cancel = True
            'enable the main window
            formOwner.Enabled = True
            'update the main window values
            formOwner.updateVals()
            'hide this window
            Me.Hide()
        End If

    End Sub



Edit: I dont know if this will fix your problem, but I noticed that the event args used in my code are different than yours. Maybe try out the FormClosingEventsArgs instead.

This post has been edited by Psyguy: 02 June 2011 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

#6 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: Form closing event override

Posted 02 June 2011 - 10:05 AM

Public Class Form1

    'close Twice to close
    Dim once As Boolean = False
    Private Sub Form1_FormClosing(sender As Object, _
                                  e As System.Windows.Forms.FormClosingEventArgs) _
                              Handles Me.FormClosing
        Debug.WriteLine(once)
        If Not once Then
            once = True
            e.Cancel = True
            Me.Hide()
            Timer1.Interval = 5000
            Timer1.Start()
        End If
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, _
                            e As System.EventArgs) _
                        Handles Timer1.Tick
        Me.Visible = True
        Timer1.Stop()
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8393
  • View blog
  • Posts: 31,213
  • Joined: 12-June 08

Re: Form closing event override

Posted 02 June 2011 - 10:45 AM

@dbasnett - were there supposed to be words that went with your dump of code?
Was This Post Helpful? 0
  • +
  • -

#8 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 310
  • Joined: 12-January 11

Re: Form closing event override

Posted 02 June 2011 - 10:48 AM

While I am not willing to change my code to confirm it, I am fairly convinced his problem will go away if he changes the
System.ComponentModel.CancelEventArgs
to
System.Windows.Forms.FormClosingEventArgs


...of course, I don't know exactly how he is calling the close. Are you using a "Me.close" call?

This post has been edited by Psyguy: 02 June 2011 - 10:50 AM

Was This Post Helpful? 1
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8393
  • View blog
  • Posts: 31,213
  • Joined: 12-June 08

Re: Form closing event override

Posted 02 June 2011 - 11:01 AM

True that Psyguy. The default form closing uses "System.Windows.Forms.FormClosingEventArgs". Why the OP used something else is beyond me.
Was This Post Helpful? 0
  • +
  • -

#10 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Form closing event override

Posted 02 June 2011 - 01:11 PM

I think my code has more issues than this...I'm gonna redesign it...btw I think OP used that because they were doing MyBase.Closing not Me.FormClosing
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1