Lanceli

Hi there,

I have a MainForm opens a child form as dialog box (eg: ShowDialog()). If user click anywhere outside the dialog box, I would like to catch that event and close the dialog box. Anyone knows how to do it Your help will be very appreciated.

BR,

L



Re: Windows Forms Designer How to catch the event when user clicking outside of a dialog box

PhilipDaniels

This should help, pertinent things are mouse events and setting of Capture property.

bon fortune - phild


Code Snippet


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using xGridCore;

namespace xGridExtensions
{
public partial class xgeTextLineDialog : Form
{
public xgeTextLineDialog()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs _eventData)
{
this.zTextLine.MouseDown += new MouseEventHandler(this.zTextLine_MouseDown);
this.zTextLine.MouseEnter += new EventHandler(this.zTextLine_DefaultMouseEvent);
this.zTextLine.MouseHover += new EventHandler(this.zTextLine_DefaultMouseEvent);
this.zTextLine.MouseLeave += new EventHandler(this.zTextLine_DefaultMouseEvent);
this.zTextLine.MouseMove += new MouseEventHandler(this.zTextLine_CommonMouseEvent);
this.zTextLine.MouseUp += new MouseEventHandler(this.zTextLine_CommonMouseEvent);
this.zTextLine.MouseWheel += new MouseEventHandler(this.zTextLine_CommonMouseEvent);
this.Height = this.zTextLine.Height;
this.zTextLine.Capture = true;
this.zTextLine.SetUserCancel = delegate(bool _value) { this.DialogResult = (_value) DialogResult.Cancel : DialogResult.OK; };
base.OnLoad(_eventData);
}
protected override void OnFormClosing(FormClosingEventArgs _eventData)
{
if (_eventData.CloseReason != CloseReason.TaskManagerClosing && this.DialogResult != DialogResult.Cancel)
{
if (this.ValidateChildren(ValidationConstraints.None) == false)
_eventData.Cancel = true;
}
base.OnFormClosing(_eventData);
}
void zTextLine_MouseDown(object _sender, MouseEventArgs _eventData)
{
if (this.zTextLine.Bounds.Contains(_eventData.Location) == false)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
this.zTextLine_DefaultMouseEvent(_sender, EventArgs.Empty);
}
void zTextLine_CommonMouseEvent(object _sender, MouseEventArgs _eventData)
{
this.zTextLine_DefaultMouseEvent(_sender, EventArgs.Empty);
}
void zTextLine_DefaultMouseEvent(object _sender, EventArgs _eventData)
{
this.zTextLine.Capture = true;
}
}
}


==============================================

namespace xGridExtensions
{
partial class xgeTextLineDialog
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.zTextLine = new xUserInterface.xuiTextLine();
this.SuspendLayout();
//
// zTextLine
//
this.zTextLine.BackColor = System.Drawing.Color.Linen;
this.zTextLine.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.zTextLine.Dock = System.Windows.Forms.DockStyle.Fill;
this.zTextLine.ForeColor = System.Drawing.Color.Maroon;
this.zTextLine.Location = new System.Drawing.Point(0, 0);
this.zTextLine.Margin = new System.Windows.Forms.Padding(0);
this.zTextLine.Name = "zTextLine";
this.zTextLine.SetUserCancel = null;
this.zTextLine.Size = new System.Drawing.Size(100, 13);
this.zTextLine.TabIndex = 0;
//
// xgeTextLineDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
this.ClientSize = new System.Drawing.Size(100, 13);
this.ControlBox = false;
this.Controls.Add(this.zTextLine);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "xgeTextLineDialog";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.TopMost = true;
this.ResumeLayout(false);
this.PerformLayout();
}

#endregion

internal xUserInterface.xuiTextLine zTextLine;
}
}













Re: Windows Forms Designer How to catch the event when user clicking outside of a dialog box

Zhi-Xin Ye - MSFT

Why not use Show() method instead of ShowDialog() method to open the form Using Show() method, you can handle Form.LostFocus event to close the form as:

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

Form f = new Form();

f.LostFocus +=new EventHandler(f_LostFocus);

f.Show();

}

void f_LostFocus(object sender, EventArgs e)

{

Form f = sender as Form;

f.Close();

f.Dispose();

}






Re: Windows Forms Designer How to catch the event when user clicking outside of a dialog box

PhilipDaniels

Zhi-Xin Ye

Yes you could do that, but you won't get the same behaviour, with your method I think that as soon as the mouse leaves the dialog box rectangle, the dialog box will terminate - I think the originator wanted to terminate if the user 'clicked' outside the dialog box.

Also the "terminate if user clicks outside dialog" behaviour ought to be controlled by a property of the DialogBox; which the calling application can set prior to the ShowDialog invokation, the original question prompted me to put that property into my dialogs that support the feature.

I don't think that the calling application should be aware of what caused the DialogBox to terminate, only that it did terminate. The DialogResult is the only thing that the caller should need to be aware of on return from ShowDialog.

Rgds PhilD




Re: Windows Forms Designer How to catch the event when user clicking outside of a dialog box

Lanceli

Hi,

Just by slight change Zhi-Xin's solution, I made a walk around which gives me the behavior I want. But PhilD is right, what I'm seeking for is the event to detect the user click out side a dialogbox not just a form. Any suggestion is welcome!

Kr,

-L





Re: Windows Forms Designer How to catch the event when user clicking outside of a dialog box

PhilipDaniels

Also, it should be remembered that Winda's can mimic the behaviour of X, that is when the mouse hovers over a window then that window becomes the active window (ie it gets the focus) - being an old (in all senses of the word) X man I run my system this way.