C # form location Show and ShowDialog

Source: Internet
Author: User

The Centerparent form is centered in its parent form.
The Centerscreen form is centered in the current display window and its dimensions are specified in the form size.
The position of the Manual form is determined by the Location property.
The Windowsdefaultbounds form is positioned in the Windows default location, and its boundaries are determined by Windows by default.
The WindowsDefaultLocation form is positioned in the Windows default location, and its dimensions are specified in the form size.

That is, Centerscreen is not meant to be centered on the screen (relative), it is centered in the "Current display window", should be selected when using the show () method, Centerparent should be selected with the ShowDialog () method, This will allow the window to be displayed centered.

In SDI with the ShowDialog () method, and set the corresponding form of the startposition for the centerparent can make the form center, of course, you can also use the same effect as centerscreen, but the meaning is not the same.
In the MDI can only use Show (), if you use ShowDialog (), whether you choose Centerparent or Centerscreen will be wrong, said ShowDialog can only be used in the top-level window, such as meaning. You can center the form with show (), and set the startposition of the corresponding form to Centerscreen.


ShowDialog () Popup modal form

Show () Popup non-modal form

Mode form, you cannot switch to the main form until you close or hide it.

Non-modal form, transformation focus eliminates the need to close the form

Summary: Display important information, or use a modal form, such as deleting a file, to make sure that the user is really trying to delete the file

Non-modal, the order of form access is not known, it is more suitable to display some relevant information of the program.


Application.Run () is "begins running a standard application messageloop on the current thread, and makes the specified form V Isible. " The code can be expressed as:
while (GetMessage (&msg) >0)
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
Directed by Wm_quit, quit the application. Use the Form.show () method to exit the program immediately after the form is displayed. If the Form.ShowDialog () is a modal dialog box, it will not disappear immediately, but if you have other Windows, Form.ShowDialog () shows the modal window, only it exits, other windows can be displayed, but once you exit the entire program, Other windows will never get the chance to run, and with Application.Run () there is no such phenomenon.

The form displays in Form1. Show () and Form1. The difference between ShowDialog ()

Forms and dialog boxes are either modal or modeless. The Mode form or dialog box must be closed or hidden before you can continue to use the rest of the application.

The dialog box that displays important messages should always be modal. An example of a modal dialog box is the About dialog box in Visual Studio. The MessageBox is a modal form that you can use. When a modal dialog box is displayed, the form or dialog box that starts open can no longer get focus.

The modeless form lets you transform the focus between this form and another form without having to close the initial form. Users can continue to work in other locations in any application while the form is displayed. For example: The search function in the text editing software is a modeless one, because after the search dialog box comes out, you can also edit this article, that is, it does not affect other forms to get the focus.

Displays the form as a modal dialog box with Form1. ShowDialog () method. This method has an optional parameter, owner, which can be used to specify a parent-child relationship for the form. For example:

In the Form1 code snippet:

Form2 f2=new Form2 ();

F2. ShowDialog (this);//this represents Form1 current instance

This F2 instance establishes a parent-child relationship with the Form1 instance and can communicate with each other.

If F2 is not used. ShowDialog (this) and the direct use of the non-parametric, to define the parent-child relationship, you need a statement f2.owner=this;

The form is displayed as a modeless dialog box with Form1. Show () method.

NoteIf the form appears to be modal, the code after the ShowDialog method is not executed until the dialog box is closed. However, when the form is displayed as modeless, the code that follows the show method is executed immediately after the form is displayed.

The Show method of the form does not give any notification to the calling code, and using ShowDialog is a good choice if notification is required.
After the show method is called, the code after the Show method executes immediately after the call to the ShowDialog method, the calling code is paused, and the form relationship of the ShowDialog method is called until it is executed. And the form can return a DialogResult value that describes why the form was closed, such as Ok,cancel,yes,no. For the form to return a DialogResult, you must set the DialogResult value for the form, or set the DialogResult property on a button on the form.

Example:
The following is the subform code, which requires the phone to be entered and then returned to the parent form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;      
Namespace WindowsApplication1
{
Public partial class Phone:form
{
Public Phone ()
{
InitializeComponent ();
Btnok.dialogresult = DialogResult.OK;
Btnok.dialogresult = DialogResult.Cancel;
}
public string PhoneNumber
{
get {return textbox1.text;}
Set {TextBox1.Text = value;}
}
private void Phone_load (object sender, EventArgs e)
{
}
}
}
does not contain any code that handles button click events because To set the DialogResult property for each button, the form disappears after you click the OK or Cancel button. The following code shows the method that calls the phone dialog box in the parent form.


The difference between Form.show and Form.ShowDialog
The difference between the 1:showdialog is modal (exclusive user input), show is modeless.
Difference 2: According to 1,showdialog can only open a self, show can open multiple self.
Difference 3: According to 2, a form opened with the show method calls Dispose immediately to release the resource when it is closed. Will the ShowDialog release resources as soon as it shuts down? I did an experiment.

public partial class Form1:form
{
Private Form2 F2 = new Form2 ();

Public Form1 ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{
Try
{
F2. Show ();
}
catch (Exception ex)
{
MessageBox.Show (ex. Message);
}
}

private void Button2_Click (object sender, EventArgs e)
{
Try
{
F2. ShowDialog ();
}
catch (Exception ex)
{
MessageBox.Show (ex. Message);
}
}

private void Button3_Click (object sender, EventArgs e)
{
F2 = new Form2 ();
}
}


Continuous Click Button1 will prompt "Unable to access the freed object." Object name: Form2 ". and continuous click Button2 is otherwise, everything is normal. Continuing the investigation found that when using ShowDialog, the constructor Form2 () is called for the first time, and then Form2_load,form2_activated,form2_shown is called in turn. The second and later use of ShowDialog will only call Form2_load,form2_activated,form2_shown. It can be judged that the Form2 is only hidden, not deconstructed, when it is closed.

ShowDialog This design also makes sense, in the continuous call can save resources, but also beware of "The Last Life" affect "this life." If you want to release resources in a timely manner, add a form.close () after calling Form.ShowDialog (), and the Close method calls Dispose to deconstruct the form.

C # form location Show and ShowDialog (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.