Page 1 of 1

Programmatically Close MsgBox/Form

Posted: Tue Nov 09, 2010 8:15 pm
by costamesakid
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

Re: Programmatically Close MsgBox/Form

Posted: Tue Nov 09, 2010 8:22 pm
by Ciege
Rather than try to shoe-horn in a timer into a dialog box (do able but not straight forward) you can create a new form (in place of the dialog box) make it look at feel as much like a dialog box as you want but have a timer function on the form to close the form after X amount of seconds.

Look into using the Timer class...

Re: Programmatically Close MsgBox/Form

Posted: Wed Nov 10, 2010 1:07 pm
by Support Team
Hi,

It is not possible to use a timer function in a message box window, but as ciege suggested, write your own dialog control with identical behavior and a timer function included.

Regards,
Peter
Ranorex Team

Re: Programmatically Close MsgBox/Form

Posted: Tue Nov 16, 2010 12:56 am
by costamesakid
Turns out the time class was pretty simple;

// timer1
this.timer1.Tick += new System.EventHandler(timer1_Tick);
this.timer1.Enabled = true;
this.timer1.Interval = 60000;
this.timer1.Start();

//Close dialog after time has elapsed
void timer1_Tick(Object sender, EventArgs e)
{
this.timer1.Stop();
this.Close();
Application.Exit();
}


The above works. Eventually id like to have a counter display on the form that counts down from 60 but this works for what I need it to do