Automatic closing of popup window

| August 9, 2010 | 0 Comments

So what you wish while saving data in database. You wish to show user some popup window while saving database. For this we want to show user some type of notification that the task of saving or writing data is going on. And finally when task completes we want to let our user know that he/she can do any other work with form. But we also wish automatic closing of popup window.

Popup in Window Forms

Popup in Window Forms

Pre-Requisites

As it is always usual in my each post, you should have:

  1. Visual Studio 2005 or more
  2. .NET 2.0 installed in your system.
  3. Some heavy task which allow to show user the popup window.

Concepts Covered

All is worst if we do not know what else we have gain with completion of our task. So here it is:

  • Controlling popup window opening and closing from main application
  • Use of BackgroundWorker component

Code Snippet

Since the project is not so much big. I am not attaching code for download.
There are two Forms in Project (Start & Downloader). Start form having three controls as:

  1. Button – Name property as btnSave
  2. RichTextBox – Name property as richTextBox1
  3. Label – Name property as label1 with Text as Main Form
  4. BackgroundWorker – Name property as hiddenRunner

Downloader formĀ  having two controls as:

  1. Label – Name property as label1 with Text as Please Wait… Writing data to file.
  2. PictureBox – Name property as pictureBox1 with image of progress circle.

We are trying to run a loop up to counter value 1000 and while it is writing data to richTextBox1 we are showing user the popup window as progress is going on. You can save the data in database instead of running loop. And as soon task completes we are closing the popup window by hiddenRunner’s RunWorkerCompleted event.

Check out code here.

Code for Start.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ee_PopupAutomaticClose
{
public partial class Start : Form
{
// Downloader form declaration
Downloader down;
public Start()
{
InitializeComponent();
InitializeBackgoundWorker();

// For avoiding IllegalCrossCheckException
Control.CheckForIllegalCrossThreadCalls = false;

}

// Setting events of BackgroundWorker hiddenRunner
private void InitializeBackgoundWorker()
{
hiddenRunner.DoWork += new DoWorkEventHandler(hiddenRunner_DoWork);
hiddenRunner.RunWorkerCompleted += new RunWorkerCompletedEventHandler(hiddenRunner_RunWorkerCompleted);
}

private void hiddenRunner_DoWork(object sender, DoWorkEventArgs e)
{
// Save data in database
for (int i = 0; i < 1000; i++)
{
richTextBox1.Text += i.ToString()+" |";
}
}

// Notify as work completes and so close the form
private void hiddenRunner_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (down != null)
{
down.Close();
}
}

private void btnSave_Click(object sender, EventArgs e)
{
// Calling BackgroundWorker DoWork
hiddenRunner.RunWorkerAsync();
using (down = new Downloader())
{
down.ShowDialog();
}
}
}
}

Code for Downloader.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ee_PopupAutomaticClose
{
public partial class Downloader : Form
{
public Downloader()
{
InitializeComponent();
}

// Setting image of picture box to show user that progress is going on
private void Downloader_Load(object sender, EventArgs e)
{
pictureBox1.Image=Image.FromFile(Environment.CurrentDirectory + "\\Progress.gif");
}
}
}

Category: .NET

Leave a Reply

%d bloggers like this: