473,729 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find.Closing Event & CancelEventArgs

In VB 6, the Form_QueryUnloa d event had an UnloadMode parameter that let me
find out *why* a form is unloading, and then conditionally cancel the event.

In VB.NET, the Closing event passes a CancelEventArgs that lets me cancel
the Close() operation, but is there still any way to find out *why* a form
is closing?

This app as a form that needs to be loaded at startup, closed only at
shutdown, and then Show() / Hide() for the user. If the user clicks the [X]
button on the form's title bar, I want to intercept this and Hide() it
instead.

--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
Nov 20 '05 #1
5 3017
Nevermind. I finally found my answer after a whole bunch of full-text
searching:

"In Visual Basic 6.0, QueryUnload took two arguments, Cancel and UnloadMode.
In Visual Basic .NET, Cancel can be replaced by using
System.Componen tModel.CancelEv entArgs.Cancel. There is no replacement for
UnloadMode."

That sucks.

--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
Nov 20 '05 #2
* "Mike Labosh" <ml************ *****@vbsensei. com> scripsit:
Nevermind. I finally found my answer after a whole bunch of full-text
searching:

"In Visual Basic 6.0, QueryUnload took two arguments, Cancel and UnloadMode.
In Visual Basic .NET, Cancel can be replaced by using
System.Componen tModel.CancelEv entArgs.Cancel. There is no replacement for
UnloadMode."


Determine a Form's UnloadMode
<http://www.fawcette.co m/archives/premier/mgznarch/vbpj/2001/11nov01/qa0111/qa0111.asp>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
> Determine a Form's UnloadMode
<http://www.fawcette.com/archives/pre...1nov01/qa0111/
qa0111.asp>

Cool Tip!

OK, that has some pretty cool hack value, but I was looking for the
politically correct .NET way of doing it. See, there is a reason they
ripped out the UnloadMode feature, so I'm happy with that. Although
subclassing Form to add the functionality would work, subclassing should
never ever be done arbitrarily like that, because it leads to bad design and
maintenance problems [see Design Patterns by GoF]

For the particular problem I am trying to solve, I think I found a more
elegant way. I have this dialog with some functionality in it that I wanted
to be able to use inside the dialog, as well as silently in the application.
If I move the functionality back to the main application, then the main
application can Show() and Close() the dialog at will instead of being a
memory hog, use the functionality silently, and for the sake of the user
using the dialog to do it, the main app can pass either an Interface or a
Delegate to the dialog's constructor. I think this is the better way in
this case, because it has a more flexible and robust design.
--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:br******** *****@ID-208219.news.uni-berlin.de... * "Mike Labosh" <ml************ *****@vbsensei. com> scripsit:
Nevermind. I finally found my answer after a whole bunch of full-text
searching:

"In Visual Basic 6.0, QueryUnload took two arguments, Cancel and UnloadMode. In Visual Basic .NET, Cancel can be replaced by using
System.Componen tModel.CancelEv entArgs.Cancel. There is no replacement for UnloadMode."

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #4
On Tue, 9 Dec 2003 17:25:30 -0500, Mike Labosh wrote:
OK, that has some pretty cool hack value, but I was looking for the
politically correct .NET way of doing it. See, there is a reason they
I'm not sure I agree that this is a hack. All that article is doing is
overriding a base method of the form class. It seems very OO to me.
subclassing Form to add the functionality would work, subclassing should
never ever be done arbitrarily like that, because it leads to bad design and
Just by creating any form you are subclassing the base form and creating
your own class. Why is that bad design? Just curious on your thoughts on
this question.

For the particular problem I am trying to solve, I think I found a more
elegant way. I have this dialog with some functionality in it that I wanted


I'm glad you found a good solution.

Cheers

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #5
First, this turned out to be way longer than I had intended. But that's for
my benefit as well as anyone else that is also from a classic VB (non-OO)
background. Composing this reply has really focused my mind on design
issues. :)
OK, that has some pretty cool hack value, but I was looking for the
politically correct .NET way of doing it. See, there is a reason they
I'm not sure I agree that this is a hack. All that article is doing is
overriding a base method of the form class. It seems very OO to me.


I wouldn't quite call it a hack either. I said it had "hack value", as in
it's an interesting thing to read and experiment with.
subclassing Form to add the functionality would work, subclassing should
never ever be done arbitrarily like that, because it leads to bad design

and
Just by creating any form you are subclassing the base form and creating
your own class. Why is that bad design? Just curious on your thoughts on
this question.

Subclassing is not bad design. Subclassing arbitrarily to add or change one
feature *can lead to* bad design. For example, suppose I design a subclass
of Windows.Forms.F orm that adds this single feature. I would likely wind up
using it as a base from which to build lots of different forms in different
applications:

QueryUnloadForm Inherits Windows.Forms.F orm

Then, let's say I want to add another embellishment or feature later on that
I can use independently, and does not make use of this QueryUnload thing.
That would be another subclass of Windows.Forms.F orm that I would likely
wind up using as a base from which to derive many other forms in different
applications.

EmbellishedForm Inherits Windows.Forms.F orm

Sooner or later, I am going to want to use one of those that ALSO has the
QueryUnload feature. Since we don't have multiple inheritance, I couldn't
make a form deriving from both:

EmbellishedQuer yUnloadForm Inherits Windows.Forms.F orm

As you can see, as the number of customizations increases, there is an
explosion of subclasses of Form, because of the presumed need to have a Form
subclass for each possible combination of customizations.

Although code reuse is a benefit of inheritance, it is not the true power.
The true power of inheritance is that a client can treat subclasses
uniformly, so that application code is less cumbersome, less
"straight-down", and less brute-force, and therefore, easier to maintain.
Plus, the sibling subclasses get to share common parent implementation code.
This type of polymorphism is vastly superior to that filthy hack that VB5 &
VB6 called Interface Polymorphism.

The context here is a Notepad application as a real world design /
implementation sample case study for students. The project is highly
focused on best practices, not just banging out an app, and it is intended
to be of good design, not just simply functional.

The problem at hand was how to implement the Edit->Find functionality in a
way that yields great design. See, if I put the implementation of the text
searching inside the find dialog, then how should the application implement
the Edit ->FindNext menu command, which invokes the find feature silently
(without displaying the dialog) to find the next occurance of something the
user already searched for? My first thought was to put the searching
algorithm inside the Find class (the dialog). If the user needs to display
the form in Edit-> Find and the application needs to borrow it from
Edit->FindNext, I need a uniform way of treating the dialog. My next
thought was to have the Notebook class (main form) hold a private instance
of Find, passing its constructor a reference to the TextBox on the Notebook
form:

Private _find As New Find(txt)

At first glance, this would free me to have Find implement the QueryUnload /
UnloadMode thing. But that's not clean enough, because now the Find class
is wired to the Notebook class by the control it's hooked up to. The Find
class is now no longer portable between other apps, and it still doesn't
lend itself to be flexible enough to Find other things besides text.

So here is the solution that offers a flexible, less tighty coupled, more
portable, more maintainable design. The Find class defines a delegate for
generic searching. FindNExtHandler 's interface allows for text-search,
database search, or whatever. Below that, the Notebook class (main
application) implements the specific searching algorithm that is appropriate
for the nature of itself as the application. When the application needs to
do a search, it passes a Delegate (typesafe function pointer) of it's own
FindNext implementation to the Find dialog's Constructor. The find dialog
then invokes the delegate when the user clicks a button. If the user then
clicks Edit->FindNext, The application can call its own method directly.

If I had used inheritance to solve this problem, I easily could have ended
up with a zoo of subclasses: SearchableForm, FindForm, QueryUnloadable Form,
SearchableQuery UnloadableForm, etc...

Here, I have two classes, not a fistful of them. Inheritance is an "OO"
technique, but just because an application uses lots of subclassing, it does
not, however, imply that the application has good design. When I mentioned
in the previous post that arbitrary subclassing can turn into problems, this
what I was on about. Inheritance heirarchies need to be carefully and
thoughtfully planned so that features can be added or customized after the
fact, without the need to disturb existing code, and to not have side
effects.

The resulting advantages:

Find.vb
Is portable because it is not married to this application.
Is flexible because it is not married to this search implementation
Doesn't care who is using it or what is being searched.
Doesn't have to expose unnecessary (sp?) stuff

Notebook.vb
Can supply whatever search it wants and pass a function pointer.
Doesn't care what the Find box looks like or how it works.
Doesn't have to share any unnecessary (sp?) stuff

Public Class Find
Inherits Windows.Forms.F orm

Public Delegate Sub FindNextHandler ( _
ByVal startPos As Integer, _
ByVal searchFor As String, _
ByVal matchCase As Boolean, _
ByVal down As Boolean)

Private _findNext As FindNextHandler
Private _caretPos As Integer

Sub New(ByVal findNext As FindNextHandler , caretPos As Integer)

Me.New()

_findNext = findNext
_caretPos = caretPos

End Sub

Private Sub btnCancel_Click (...) Handles btnCancel.Click
Close()
End Sub

Private Sub btnFindNext_Cli ck(...) Handles btnFindNext.Cli ck

_findNext.Invok e(_caretPos, txtFind.Text, _
chkMatchCase.Ch ecked, radDown.Checked )

End Sub
....
End Class

Public Class Notebook

Private _findText As String
Private _findCase As Boolean
Private _findDown As Boolean
Private _findPos As Integer

Friend Sub FindNext( _
ByVal startPos As Integer, _
ByVal searchFor As String, _
ByVal matchCase As Boolean, _
ByVal down As Boolean)

'Actual implementation of the Text Search

End Sub

Private Sub mnuEditFind_Cli ck(...) Handles mnuEditFind.Cli ck

'Display the Find dialog, passing it a pointer to FindNext()
'and the current location of the caret within the text.

Dim start As Integer = Math.Max(txt.Se lectionStart, 1)
Dim f As New Find(AddressOf FindNext, start)

f.Show()

End Sub

Private Sub mnuEditFindNext _Click(...) Handles mnuEditFindNext .Click
FindNext(_findP os, _findText, _findCase, _findDown)
End Sub
....
End Class

--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2259
by: Ron L | last post by:
I have an MDI application with a number of child windows. In each child window I am catching the Closing event and having the child window decide if it should set cancel to true. The intent here is to ensure that no child window can close while it is in a state where user entered information can be lost. I have just noticed that while the Closing event is caught if I click the X on the child window, it is not caught if I click the X on...
4
3594
by: Gas | last post by:
Hi, I am a totally C# newbie from VB6. In my App, I don't know why the form_closing event is never being raised when I click on the "x" button at the corner my App, Can anyone give me some hints? Also I want to know how can I get a list of events for an object( say form) just like the combo box we use in VB6 editor (at the top)?
1
28353
by: MuZZy | last post by:
HI, I have a user control, say with one textBox. I need the following: when the user closes form (either calling Form.Close, or pressing "X" or anyhow), UserControl copies a file from a predefined folder to a folder in the textBox. UserControl doesn't have a "Closing" event, so i tried to override UserControl.Dispose() : // ========================================================== protected override void Dispose( bool disposing )
6
5663
by: Al the programmer | last post by:
I want to catch the Closing event for my form. I created a test windows app using the wizard. I then create the Closing event by clicking the lightning bolt on the properties pane. The code is added to the app but my app won't compile. It generates a "Test.Form1.Closing() denotes a 'method' which is not valid in the given context" error.
7
5756
by: Geoff Olding | last post by:
In VB.Net, What events (if any), does an MDI child form get when the application closes, which I can use to cancel the close? I am relying on the Closing event, which fires when the form itself closes, however this doesn't fire when the application closes. I can't find any alternative.
4
1275
by: Amalorpavanathan Y \(Amal\) | last post by:
Hi All, In Winform Application, MDI form is having Save button. When i close the application, it will ask Save Changes? YES or NO or CANCEL. after i select CANCEL button, i have set e.Cancel = True So application will work continously, But Save button Event is working. Can anybody give me the solution for this situation? Regards,
2
2475
by: chris in grimsby | last post by:
MDIChild Window Closing event not raised when MDI Parent is in a class library! Intructions to recreate problem: 1. Create a ClassLibrary project 2. Add an MDIParent form and a form that will be an MDIChild form 3. In the Closing event of both forms add debug code so that you can see when they are called <MDI PARENT FORM CODE> Private Sub MDIParent_Closing(ByVal sender As Object, ByVal e As
10
4025
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
4
6448
by: RSH | last post by:
I am trying to figure out how I can trap the Closing Event that occurs when a user attempts to close the window by clicking on the red"X". I tried this code which I found online but nothing happens. What do I need to do to trap and give the ability to save changes before the window closes? Thanks, Ron
0
8763
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9202
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.