Example: Display a modal window

Best practices, code snippets for common functionality, examples, and guidelines.
User avatar
Posts: 41
Joined: Wed Jul 27, 2016 12:22 pm
Location: Hamburg

Example: Display a modal window

Post by Andymann » Tue Sep 18, 2018 1:38 pm

Hi everybody,

lately, I needed to show a modal dialog window within an automated testcase: The system was not completely automated yet and a user had to take a few Actions on another system before the test could proceed.

This is partially like this: pause-test-with-message-box-t12193.html but I needed a window which was always on foreground. I ran into some trouble creating a truely modal window but with some Inspiration from here https://www.codeproject.com/Articles/18 ... MessageBox I finally got it done.
My code is a quick hack; it opens a window, presents a text and automatically closes the window after a given time has passed or if the 'OK'-button is clicked.

Maybe this is helpful to you.

Let me know...

Code: Select all

/// <summary>
		/// <para/>Stellt ein Fenster dar. Es gibt einen Button 'OK'. 
		/// <para/>Das Fenster schließt sich alternativ nach 'pSeconds' Sekunden.
		/// <para/>
		/// <para/>30.08.2018	A.Fischer	Erste Version
		/// <para/>18.09.2018	A.Fischer	Fenster ist immer im Vordergrund
		/// </summary>
		public static void showWindow_Timer(String pText, int pSeconds){			
			WinForms.Form wndALibi = new WinForms.Form();
        	wndALibi.MinimizeBox = false;
        	wndALibi.MaximizeBox = false;
        	wndALibi.Size = new System.Drawing.Size(1, 1);
	        wndALibi.StartPosition = WinForms.FormStartPosition.Manual;
	        wndALibi.Location = new System.Drawing.Point(0  , 5000);
	        WinForms.Form frmThing = new WinForms.Form();
	        //Delegates: #FTW #HackThePlanet #MWarrrHarrrHarrrr!
			var timer_Tick = new Action<Object, EventArgs>((sender, e) => { frmThing.Close(); } );			
			System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
			EventHandler evtHndl_OK = (object sender, EventArgs args) => {
		    	Ranorex.Report.Info("Fenster per Klick auf Button 'OK' geschlossen.");
			WinForms.Button btnOK = new WinForms.Button();
			WinForms.Label lblText = new WinForms.Label();
			WinForms.Label lblInfo = new WinForms.Label();
			btnOK.Text = "OK";
			btnOK.Size = new System.Drawing.Size(80, 45);
			btnOK.Location = new System.Drawing.Point(380, 210);
			btnOK.Click += new System.EventHandler( evtHndl_OK );
			btnOK.Font = new Font(btnOK.Font.FontFamily, 14);
			lblText.Text = pText;
			lblText.Location = new System.Drawing.Point(20, 10);
			lblText.Size = new System.Drawing.Size(440, 180);
			lblText.Font = new Font(lblText.Font.FontFamily, 14);
			lblInfo.Text = "(Fenster wird nach " + pSeconds.ToString() + " Sekunden geschlossen.)";
			lblInfo.Location = new System.Drawing.Point(20, 230);
			lblInfo.Size = new System.Drawing.Size(440, 180);
			lblInfo.Font = new Font(lblText.Font.FontFamily, 12);
			frmThing.Text = "RNRX";
			frmThing.Size = new Size(480, 300);
			frmThing.FormBorderStyle = WinForms.FormBorderStyle.Fixed3D;
			frmThing.MaximizeBox = false;
			frmThing.Controls.Add( btnOK );
			frmThing.Controls.Add( lblText );
			frmThing.Controls.Add( lblInfo );
			frmThing.StartPosition = WinForms.FormStartPosition.CenterScreen;
			timer.Interval = pSeconds*1000;
  			timer.Tick += new EventHandler(timer_Tick);
	        wndALibi.TopMost = true;
			Ranorex.Report.Info("Anzeige Fenster mit Text \r\n'" + pText + "'");
			frmThing.ShowDialog( wndALibi);
	        wndALibi.Dispose(); // clean it up all the way	


Vorsprung durch Wahnsinn