CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010
    Posts
    1

    Programmatically Close MsgBox/Form

    Hello,

    I have a MsgBox utility that basically creates a message box ontop of a windows form to ensure the message bis is the the topmost form. The code is:

    //***********************************************************************//
    //*******FUNCTION TO ENSURE DIALOG/MESSAGE BOXES ARE ALWAYS 'ON TOP'*****//
    //***********************************************************************//

    public static class MsgBox
    {
    static public DialogResult Show(string message, string title, MessageBoxButtons buttons, MessageBoxIcon icons)
    {
    // Create a host form that is a TopMost window which will be the parent of the MessageBox.
    System.Windows.Forms.Form topmostForm = new System.Windows.Forms.Form();

    topmostForm.Size = new System.Drawing.Size(40, 40);
    topmostForm.StartPosition = FormStartPosition.Manual;
    System.Drawing.Rectangle rect = SystemInformation.VirtualScreen;
    topmostForm.Location = new System.Drawing.Point(rect.Bottom + 12, rect.Right + 12);
    topmostForm.Show();

    // Make this form the active form and make it TopMost
    topmostForm.Focus();
    topmostForm.BringToFront();
    topmostForm.TopMost = true;

    // Finally show the MessageBox with the form just created as its owner
    Keyboard.Enabled = true;
    Mouse.Enabled = true;
    DialogResult result = MessageBox.Show(topmostForm, message, title, buttons, icons);
    topmostForm.Dispose(); // clean it up all the way
    return result;
    }
    }

    -------------------------------------------------------------------------------------------------------------------------

    The entire program is an automated test and purpose of the message box is to inform the user whether the test completed, failed execution, or was aborted by the user. It is called using the code below:

    //****Display Error Dialog if an Error Occurs or Success Dialog if Test Completes Without Errors****//
    if (error == -1)
    {
    CommonUtilLib.Util.MsgBox.Show("An Error Has Occurred, Test Aborted.","Test Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    Report.Error("AN ERROR HAS BEEN REPORTED WHICH WILL NOT ALLOW RANOREX TO CONTINUE EXECUTION. PLEASE EXAMINE YOUR LOG REPORT FOR MORE INFORMATION");
    }
    else if (error == 0)
    {
    CommonUtilLib.Util.MsgBox.Show("Test Successfully Executed","Test Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
    Report.Info("YOUR RANOREX TEST HAS EXECUTED SUCCESSFULLY. PLEASE EXAMINE YOUR LOG REPORT FOR MORE INFORMATION");
    }
    else if (error == 1)
    {
    CommonUtilLib.Util.MsgBox.Show("You Have Aborted the Test","Test Aborted", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    Report.Warn("YOU HAVE ABORTED THE TEST. THE REPORT LOG HAS STOPPED LOGGING DATA");
    }
    -------------------------------------------------------------------------------------------------------------------------

    what im really hoping to do is to display the message box when necessary, and if the user hasnt responded to it with 30 seconds then I want to set the result = "OK" and destroy the form

    Can anyone offer any direction or examples on how to do this? Thanks

  2. #2
    Join Date
    Nov 2007
    Posts
    35

    Re: Programmatically Close MsgBox/Form

    Easiest way may be to simulate a mouse click on the OK button after the time-out, or just send the Enter key if OK is the default button.

    For mouse click see PostMessage() and WM_LBUTTONDOWN and WM_LBUTTONUP. For sending Enter key check the help for a key sending function. Check MSDN docs for sending keystrokes to programs/windows.

    For the 30 second time-out you could use a timer or get the system tick count and calculate it yourself.
    Search Timer Timer Tick etc.. if you use the tick count I believe you have to check for rollover at midnight, unless they changed it. A Timer uses more resources but it should do everything for you.
    Last edited by MilesAhead; November 17th, 2010 at 04:26 PM.

  3. #3
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: Programmatically Close MsgBox/Form

    costamesakid:

    you should use code tags - it makes a big difference

    [ code ] code............... [ / code ]

    (without the spaces)

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Programmatically Close MsgBox/Form

    System.Windows.Forms.Form
    BTW, it's neither C++ nor WinAPI. Wrong forum...
    Best regards,
    Igor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured