Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When timer exits i want to open Form2 but this will gives an error
"Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog."


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace timer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar2.Value < 100)
            {
                progressBar2.Value += 2;
            }
            else
            {
                Form2 f = new Form2();
                //f.ShowDialog();
                ShowDialog(new Form2());
            }
        }
    }
}
Posted
Comments
ridoy 17-Nov-12 14:42pm    
This occurs for this line..ShowDialog(new Form2());
Whats the problem of using f.ShowDialog() instead of it?
Member 9411249 17-Nov-12 14:50pm    
this gives me an error and not working means not open Form2

Try:
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace Timer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2();  // Create the form only once. The Tick event may be called thouthands of times!
        }

        private Form2 form2;

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar2.Value < 100)
            {
                progressBar2.Value += 2;
            }
            else
            {
                form2.ShowDialog();
                timer1.Stop();  // We did our job. Leave the Timer alone now. You should restart the timer only if needed.
            }
        }
    }
}
 
Share this answer
 
Comments
Member 9411249 18-Nov-12 2:57am    
Thank you so much its work for me...:)
Hi,
lets try this
C#
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace Timer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Form2 f2= new Form2();  //creates the object for Form2
        
       private void Form1_Load(object sender, EventArgs e)
        {
          timer1.Start();//calling the timer tick event
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar2.Value < 100)
            {
                progressBar2.Value += 2;
            }
            else
            {
                timer1.Stop(); // Before calling f2.ShowDialog(), stops the timer event
                f2.ShowDialog();
            }
        }
    }
}


i hope this will helps to you.
 
Share this answer
 
Comments
Member 9411249 18-Nov-12 2:58am    
Thank you so much for helping me... :)
Member 9411249 18-Nov-12 3:25am    
i want that when i close form2 so form1 close automatically its not closed when i closed Form2?? help me plz
thnx
anilkumar.6714 26-Nov-12 2:16am    
just write the this.Close(); after f2.ShowDialog(); In timer1_Tick event.
This looks like the following
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar2.Value < 100)
{
progressBar2.Value += 2;
}
else
{
timer1.Stop(); // Before calling f2.ShowDialog(), stops the timer event
f2.ShowDialog();
this.Close();
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900