Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 at 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 at 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 at 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 at 18-Nov-12 2:58am
   
Thank you so much for helping me... :)
Member 9411249 at 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 at 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
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 319
3 Sergey Alexandrovich Kryukov 241
4 Gihan Liyanage 194
0 OriginalGriff 737
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 505
3 Sergey Alexandrovich Kryukov 372
4 thatraja 370


Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2014
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