Click here to Skip to main content
15,910,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

There is one issue, in windows applicaiton, after implement the autologgedout facility.

Eg:
There is a report, and its shows message as "Record not found". And leave the application for some time idle. And application will autologgedout. And every thing will be hide except the "Record not Found" message box.


private void Logout()
{

this.Hide();
Login lg = new Login();
lg.Show();

}
Posted
Updated 1-Apr-14 23:22pm
v3
Comments
ZurdoDev 2-Apr-14 9:08am    
What is your question?
gggustafson 2-Apr-14 12:28pm    
I think OP placed it in the title of the question.
ZurdoDev 2-Apr-14 12:57pm    
Perhaps, but it still makes no sense.
gggustafson 2-Apr-14 13:03pm    
I believe that it made enough sense for me to provide a solution.

Regards


Use the Application.Exit[^] method to terminate the application. If specific clean up actions are required, include an event handler like:


C#
// ****************************************** Application_Exit

void Application_Exit ( object    sender,
                        EventArgs e )
    {

    // take clean up actions

    }


and in the application's constructor add the statement:


C#
Application.ApplicationExit +=
                new EventHandler (
                        Application_Exit );


Alternatively, if you have a handle to the MessageBox window, you could send the MessageBox window a WM_CLOSE message. (You can obtain the handle to the MessageBox window using FindWindow.)

 
Share this answer
 
v2
Comments
clintoaug 3-Apr-14 5:14am    
Hi,

Thanks for the reply.

I try to use "FindWindow" to close the MessageBox. and its working fine except the cases MessageBoxButtons.YesNo, MessageBoxButtons.AbortRetryIgnore


http://social.msdn.microsoft.com/Forums/windows/en-US/d3f89686-e4d0-4bb1-9052-31abef2a9d2a/detecting-message-boxes-and-close-them-automaticaly?forum=winforms

I need to findout a solution for the cases MessageBox like MessageBoxButtons.YesNo, MessageBoxButtons.AbortRetryIgnore
clintoaug 3-Apr-14 6:51am    
Hi
Using this one we can solve the MessageBox.YesNo type

http://stackoverflow.com/questions/16797370/click-a-messagebox-button-programmatically

gggustafson 3-Apr-14 15:10pm    
Did you try using an exit handler?
gggustafson 10-Apr-14 10:27am    
Goog. Glad you found a solution you liked.
For close active message, i refer these

MessageBox with Okhttp://stackoverflow.com/questions/12532812/how-to-suppress-a-dialog-box-displayed-by-code-that-i-cant-change


MessabeBoxWith Yes or No
http://stackoverflow.com/questions/16797370/click-a-messagebox-button-programmatically




#region MessageBox Closing Method

//// === Close MessageBox Programcaticaly, other than MessageBoxButton.yesNo type ===============
//// === Programaticaly click on MessageBox No button
//// ============================================================================================
private bool cancelled;
// P/Invoke declarations
private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
[DllImport("user32.dll")]
private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

////public class DialogCloser : IDisposable
////{
public void CloseActiveMessageBoxOnApplicationIdle()
{
//if (Application.OpenForms.Count == 0) throw new InvalidOperationException();
//Application.OpenForms[0].BeginInvoke(new Action(() =>
//{
// Enumerate windows to find dialogs
if (cancelled) return;
EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero);
GC.KeepAlive(callback);
//}));
}

public void Dispose()
{
cancelled = true;
}

private static bool checkWindow(IntPtr hWnd, IntPtr lp)
{
// Checks if <hwnd> is a Windows dialog
StringBuilder sb = new StringBuilder(260);
GetClassName(hWnd, sb, sb.Capacity);
if (sb.ToString() == "#32770")
{
// Programaticaly click on 'X' button on MessageBox
// Close it by sending WM_CLOSE to the window (MessageBox.OK, OkCancel, type)
SendMessage(hWnd, 0x0010, IntPtr.Zero, IntPtr.Zero);

// Programaticaly click on 'No' button on MessageBox
// Close MessageBox.YesNo type
EnumChildWindows(hWnd, EnumChildWindowsCallback, IntPtr.Zero);
}
return true;
}

//// === End Close MessageBox Programcaticaly, other than MessageBoxButton.yesNo type ===========
//// ============================================================================================



//// =========== Close MessageBox.YesNo type ====================================================
//// =========== Programcaticaly click on MessageBox No button. =================================
//// ============================================================================================
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

//[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
//private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)]
private static extern IntPtr GetWindowCaption(IntPtr hwnd, StringBuilder lpString, int maxCount);

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

// A delegate which is used by EnumChildWindows to execute a callback method.
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

//// This method accepts a string which represents the title name of the window you're looking for the controls on.
//public static void ClickButtonLabeledNo(string windowTitle)
//{
// try
// {
// // Find the main window's handle by the title.
// var windowHWnd = FindWindowByCaption(IntPtr.Zero, windowTitle);

// // Loop though the child windows, and execute the EnumChildWindowsCallback method
// EnumChildWindows(windowHWnd, EnumChildWindowsCallback, IntPtr.Zero);
// }
// catch (Exception e)
// {
// MessageBox.Show(e.ToString());
// }
//}

private static bool EnumChildWindowsCallback(IntPtr handle, IntPtr pointer)
{
const uint WM_LBUTTONDOWN = 0x0201;
const uint WM_LBUTTONUP = 0x0202;

var sb = new StringBuilder(256);
// Get the control's text.
GetWindowCaption(handle, sb, 256);
var text = sb.ToString();

// If the text on the control == &No send a left mouse click to the handle.
if (text == @"&No")
{
PostMessage(handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
PostMessage(handle, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}

return true;
}
//// =========== End Programcaticaly click on MessageBox No button. =============================
//// ============================================================================================

#endregion MessageBox Closing Method

and i will call the method CloseActiveMessageBoxOnApplicationIdle(), and its working fine. and i would like to know, is this a good method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900