How To Close A Form

Table of contents:

How To Close A Form
How To Close A Form

Video: How To Close A Form

Video: How To Close A Form
Video: How To Open Form2 From Form1 And Hide/Close Form 1 In C# | C# Tutorial 2024, May
Anonim

The concept of forms as interface elements, encapsulating the functionality of windows and abstracting various aspects of working with them, is firmly rooted in the field of application programming. Various platforms and frameworks usually offer their own implementations of this concept. Accordingly, to control the form, for example, to close it, you need to use different methods.

How to close a form
How to close a form

Necessary

  • - development environment or text editor;
  • - the ability to change the program code.

Instructions

Step 1

Get a handle to the window, object, reference, or pointer to the object of the form that you want to close. As a rule, the descriptor may be needed only when programming under Windows without using any frameworks (using only the API). In other cases, actions with forms are usually performed through the corresponding objects. Since the form was previously opened, the corresponding object was also created. Use it. In the code of the methods of a form class, access to the methods and properties of its object is usually possible through identifiers such as this (C ++, C #), self (Delphi), Me (Visual Basic), or simply by name, since they are accessible from the current scope.

Step 2

Close the form created as a modeless dialog, knowing its handle. If you want the program to be able to perform some action on closing and possibly prevent it, send the window a WM_CLOSE message:

:: PostMessage (h, WM_CLOSE, 0, 0);

Otherwise, just destroy it by calling DestroyWindow:

:: DestroyWindow (h);

Here h is the handle to the window.

Step 3

To close a form in a program running the Microsoft. NET Framework, use the Close method of its object (this is an object of the Form class in the System. Windows. Forms namespace). For example, from a method of a form class, a call can be made like this:

this. Close ();

Then, if the form is part of an MDI application or has been displayed by calling ShowDialog, also call Dispose so that the garbage collector can free memory.

Step 4

In Delphi, use the Close method to close the form. Alternatively, for modal forms, you can set the ModalResult property to a value other than zero. The constants commonly used are mrOk, mrCancel, etc.

Step 5

Forms of Microsoft office applications in VBA scripts can be closed by calling the Hide method of their objects. For example, from a handler of any event of a form or its control, it can be done like this:

Me. Hide

Recommended: