Click here to Skip to main content
12,955,118 members (61,618 online)
Rate this:
 
Please Sign up or sign in to 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."


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 17-Nov-12 8:09am
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
            }
        }
    }
}
  Permalink  
Comments
Member 9411249 18-Nov-12 2:57am
   
Thank you so much its work for me...:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,
lets try this
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.
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100