Why set up standard dialogs in a form?

I have legacy code in C # .Net 2.0 where someone dragged OpenFileDialog and SaveFileDialaog from the toolbar to the form. This leads to the creation of openable and persistent dialogs when their parent dialog is created and remains until the parent dialog is closed. Open / saved forms are really used only in one place for the support file, which is not always open / saved during the life of the parent dialog; it seems to me ineffective. I would do the following:

using (OpenFileDialog openDlg = new OpenFileDialog() ) { //initialize openDlg... if (openDlg.ShowDialog() != DialogResult.OK) { //handle OK result } } 

This way, OpenFileDialog will be deleted when the using statement is out of scope. If I don’t miss something, then the value of quick utilization (and faster garbage collection) of a resource outweighs the small savings in that you do not need to create Open / Save File dialogs when they are needed. Even if the open / save dialogs were used every time their parent dialog is used, I don’t see much importance to constantly find them. My question is: am I missing something? Is there any greater efficiency for permanently storing open / save dialogs? I assume that the answer will be the same for all other standard dialogs: ColorDialog, FolderBrowserDialog and FontDialog.

+4
source share
7 answers

Not necessary. These dialogs have a Dispose () method only because they inherit one from the Component class. They do not redefine the method, since in reality they have nothing to dispose of. All system resources that they use are freed when the dialog box is closed. The implementation of Component.Dispose () also does not make any order; it simply ensures that the component will be removed from the IContainer collection.

This is otherwise an implementation detail of these dialog wrappers. Other components commonly used for Dispose (), ToolTip is a good example. The automatically created Dispose method for the form ensures that this happens; check the "components" field in the Designer.cs source code file. That's why the component has a Dispose method, which by default does nothing important.

There is one interesting point, to avoid removing a component in the form, it allows the .NET wrapper class to collect garbage for a dialog. But a kilobyte or so that saves is micro-optimization.

Take this at face value: if the early use of these class objects were important, Microsoft would not have made their components.

+4
source

I think that the "on form" option should be easier for a beginner, since the work is mainly in the editor, not in the code.

+2
source

It’s good practice to do what you are doing, as you have stated that these dialogs do not need to sit in memory for very long, but if you need to use them many times, it is a good idea to leave it around and dispose of it properly when the application closes just depends on the situation.

Given the current situation, it sounds like your good move anyway, and I would not lose a lot of sleep over it, but regardless of whether it does anything useful in this method of disposal, it is recommended to use it, its just a good practice .

+1
source

The advantage is that you can customize the dialog box that you need (multi-selection, start directory, extensions), etc. in the form designer.

In addition, the form designer makes it easy to drag and drop OpenFileDialog into your form, which many developers simply do. I can’t blame them.

There is no significant advantage or disadvantage to any approach; I doubt that you will find any tangible difference in performance.

0
source

I usually create form dialog boxes when I want to save certain settings, such as filters, without having to set them in every place. I am using dialogue. If the dialog is used in only one place, I use the using statement, as you already mentioned above.

0
source

I always wondered about this approach, and also prefer the use pattern in your snippet. The only possible reason I could ever come up with is that by creating their components, their properties can be configured in the designer.

Not a very convincing reason (IMHO).

0
source

I generally agree. But there may be a slight increase in performance if the dialog box has a lot of these default properties. Consider opening a file with 50 file type filters.

0
source

All Articles