System.Windows.Forms.Form.ShowDialog C# (CSharp) Method

ShowDialog() public method

public ShowDialog ( IWin32Window owner ) : DialogResult
owner IWin32Window
return DialogResult
		public DialogResult ShowDialog(IWin32Window owner) {
			Rectangle	area;
			bool		confined;
			IntPtr		capture_window;

			Form owner_to_be = null;

			if ((owner == null) && (Application.MWFThread.Current.Context != null)) {
				IntPtr active = XplatUI.GetActive ();
				if (active != IntPtr.Zero) {
					owner = Control.FromHandle (active) as Form;
				}
			}

			if (owner != null) {
				Control c = Control.FromHandle (owner.Handle);
				if (c != null)
					owner_to_be = c.TopLevelControl as Form;
			}

			if (owner_to_be == this) {
				throw new ArgumentException ("Forms cannot own themselves or their owners.", "owner");
			}

			if (is_modal) {
				throw new InvalidOperationException ("The form is already displayed as a modal dialog.");
			}

			if (Visible) {
				throw new InvalidOperationException ("Forms that are already "
					+ " visible cannot be displayed as a modal dialog. Set the"
					+ " form's visible property to false before calling"
					+ " ShowDialog.");
			}

			if (!Enabled) {
				throw new InvalidOperationException ("Forms that are not enabled"
					+ " cannot be displayed as a modal dialog. Set the form's"
					+ " enabled property to true before calling ShowDialog.");
			}

			if (TopLevelControl != this) {
				throw new InvalidOperationException ("Forms that are not top level"
					+ " forms cannot be displayed as a modal dialog. Remove the"
					+ " form from any parent form before calling ShowDialog.");
			}

			if (owner_to_be != null)
				this.owner = owner_to_be;
				
			// If our owner is topmost, we better be too, or else we'll show up under our owner
			if (this.owner != null && this.owner.TopMost)
				this.TopMost = true;
				
			#if broken
			// Can't do this, will screw us in the modal loop
			form_parent_window.Parent = this.owner;
			#endif

			// Release any captures
			XplatUI.GrabInfo(out capture_window, out confined, out area);
			if (capture_window != IntPtr.Zero) {
				XplatUI.UngrabWindow(capture_window);
			}

			foreach (Form form in Application.OpenForms)
			{
				if (form.Enabled == true)
				{
					disabled_by_showdialog.Add(form);
					form.Enabled = false;
				}
			}
			modal_dialogs.Add(this);

#if not
			// Commented out; we instead let the Visible=true inside the runloop create the control
			// otherwise setting DialogResult inside any of the events that are triggered by the
			// create will not actually cause the form to not be displayed.
			// Leaving this comment here in case there was an actual purpose to creating the control
			// in here.
			if (!IsHandleCreated) {
				CreateControl();
			}
#endif

			Application.RunLoop(true, new ApplicationContext(this));

			if (this.owner != null) {
				// Cannot use Activate(), it has a check for the current active window...
				XplatUI.Activate(this.owner.window.Handle);
			}
			
			if (IsHandleCreated) {
				DestroyHandle ();
			}

			if (DialogResult == DialogResult.None) {
				DialogResult = DialogResult.Cancel;
			}
			
			return DialogResult;
		}

Same methods

Form::ShowDialog ( ) : DialogResult

Usage Example

示例#1
0
        /// <summary>Displays Any String</summary>
        /// <param name="StringToDisplay">Object with Properties to edit</param>
        /// <param name="pDaddy">Parent Form Or Nothing (null)</param>
        /// <param name="strTitle">Window Title</param>
        public static void DisplayABigString(string StringToDisplay, System.Windows.Forms.Control pDaddy, string strTitle = "")
        {
            System.Windows.Forms.Form frmEO = new System.Windows.Forms.Form();
            TextBox T = new TextBox()
            {
                Multiline = true, ScrollBars = ScrollBars.Both, Dock = DockStyle.Fill, WordWrap = false
            };

            frmEO.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            frmEO.Text            = strTitle;
            frmEO.Controls.Add(T);
            T.Text            = StringToDisplay;
            T.Font            = CourierNew();
            T.SelectionStart  = 0;
            T.SelectionLength = 0;
            T.KeyDown        += TextBox_KeyDown;
            frmEO.Hide();
            frmEO.Height = 640;
            frmEO.Width  = 640;
            System.Windows.Forms.Application.DoEvents();
            if (pDaddy == null)
            {
                frmEO.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                frmEO.ShowDialog();
            }
            else
            {
                frmEO.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                frmEO.ShowDialog(pDaddy);
            }
            frmEO.Dispose();
        }
All Usage Examples Of System.Windows.Forms.Form::ShowDialog
Form