1

I was wondering how to close my form1 after a couple of seconds. Its essentially a loading form with some data. I desire to show the form1 for half a minute, and then I need to close form1 and open the form2 where I have got my windows form application using Visual C#.

Any coding help from any one!!! Please

leppie
  • 115,091
  • 17
  • 196
  • 297
  • Are you looking for a Splash Screen? http://stackoverflow.com/q/7955663/153923 –  Jul 17 '14 at 16:25
  • @jp2code i have custom messages and custom made picture to give in the form1. So i don't want a splash screen –  Jul 17 '14 at 16:47
  • Yes. Here is a simple one: [Splash screen in C# Winform](http://www.mindstick.com/Articles/e169c280-3754-41aa-b2dd-b048ecb13588/?Splash%20screen%20in%20C) –  Jul 17 '14 at 18:25
  • @jp2code brother that was a perfect splash screen. it is perfectly working for me. **But the problem is i need to close the splash screen then only it is going to my main form.** –  Jul 17 '14 at 20:38
  • Call `backgroundWorker1.CancelAsync();` when you are ready to close the splash screen. –  Jul 17 '14 at 21:00

3 Answers3

1

You can use the Timer for waiting the specified amount of time.

Try This:

System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=30000;
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();

private void timer1_Tick(object sender, EventArgs e)
{
   timer1.Stop();
   this.Hide();
   new Form2().Show();
}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • It opening and closing the form2. In 3 sec. i need to show the form 2 for like ever.. so that my application runs over there. –  Jul 17 '14 at 16:14
  • @user3846692: call `this.Hide()` instead of `this.Close()` and also stop the timer , please check my edited post. – Sudhakar Tillapudi Jul 17 '14 at 16:22
  • The form 2 is opening and closing, still –  Jul 17 '14 at 17:10
  • `namespace LocalDataBaseAp { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { this.Close(); new Form2().Show(); } } }` –  Jul 17 '14 at 17:44
0

You can use the ApplicationContext class to do this job:

    public class CustomApplicationContext : ApplicationContext
    {
        Form mainForm = null;
        Timer timer = new Timer();

        public CustomApplicationContext(Form mainForm,Form timed):base(timed)
        {
            this.mainForm = mainForm;

            timer.Tick += new EventHandler(SplashTimeUp);
            timer.Interval = 30000;
            timer.Enabled = true;
        }

        private void SplashTimeUp(object sender, EventArgs e)
        {
            timer.Enabled = false;
            timer.Dispose();

            base.MainForm.Close();
        }

        protected override void OnMainFormClosed(object sender, EventArgs e)
        {
            if (sender is Form1)
            {
                base.MainForm = this.mainForm;
                base.MainForm.Show();
            }
            else if (sender is Form2)
            {
                base.OnMainFormClosed(sender, e);
            }
        }


    }

Then in your Program.cs in Main():

[STAThread]
static void Main()
{
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     CustomApplicationContext app = new CustomApplicationContext(new Form2(),new Form1());
     Application.Run(app);

}

With this you dont need the timer in Form1,just the functionality you want it to have for the especified amount of time and it closes showing Form2 after.

terrybozzio
  • 4,424
  • 1
  • 19
  • 25
  • Okay please tell me where i need to give this code, in form1 or form2 and. I can see only one program.cs for both form1 and form2. In program.cs there is alraey some thing like `Application.Run(new Form1());` So do i need to change to what you said –  Jul 17 '14 at 17:09
  • Also what is this customApplicationContext, Its showing an error like directive or assembly mising –  Jul 17 '14 at 17:11
  • In your project(where you have form1 and form2),add a class file and paste this code(the customApplicationContext class) and set a reference to 'using System.Windows.Forms;'.then in your Program.cs just paste what i have up there(inside the static void Main method) and of course remove what you previously have and run it. – terrybozzio Jul 17 '14 at 17:17
  • ok i created a class called ApplicationContect.cs and i pasted your first big code. In that i am geting error `Error 'LocalDataBaseAp.ApplicationContext' does not contain a definition for 'MainForm'` ` –  Jul 17 '14 at 17:27
  • Ok now i am getting only one erro. Please see my updated coding as per what you said,. i am getting error like `LocalDataBaseAp.ApplicationContext.CustomApplicationContext.OnMainFormClosed(object, System.EventArgs)': no suitable method found to override` –  Jul 17 '14 at 17:41
  • are you there. I am getting only one error now. i am waiting for you reply. Error... `LocalDataBaseAp.ApplicationContext.CustomApplicationContext.OnMainFormClosed(ob‌​ject, System.EventArgs)': no suitable method found to override` What to so with this –  Jul 17 '14 at 17:51
  • you name your class CustomApplicationContext which DERIVES from ApplicationContext and thats all.again,create a class called CustomApplicationContext,after you created that file with this name paste the class above to that file and insert a using statement like this:using System.Windows.Forms;,and for this file your done.Next in your program.cs file replace the contents in your Main method with what i have above. – terrybozzio Jul 17 '14 at 18:16
0

Use the Below codes for splash screen and call in form1.

 Form2 _f2;
    public Form1()
    {
        InitializeComponent();
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
        _f2 = new Form2();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        if (backgroundWorker1.IsBusy != true)
        {
            _f2.Show();
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            // Start the asynchronous operation.
            backgroundWorker1.RunWorkerAsync();
        }
    }

    private void btnStart_Click(object sender, EventArgs e)
    {

    }

    private void btnCancle_Click(object sender, EventArgs e)
    {
        if (backgroundWorker1.WorkerSupportsCancellation == true)
        {

            // Cancel the asynchronous operation.
            backgroundWorker1.CancelAsync();
        }
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; i <= 100; i++)
        {
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                break;
            }
            else
            {
                // Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500);


            }
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        _f2.Close();
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = true;

    }

}
Milan phir
  • 74
  • 10