I have a problem with a timer on a form. I've enabled it from the properties and set the interval value 5000. On the tick event, I want to close the current form and open form1, but it's not working. The current form closes and form1 opens every 5 seconds, not only once. What should I do? Thank you in advance!
This is the tick event:
private void timer1_Tick(object sender, EventArgs e)
{
this.Hide();
Form1 frm = new Form1();
frm.ShowDialog();
timer1.Enabled = false;
}
frm.ShowDialog(); is a blocking call, therefore the next line won't get executed until the new form is closed. Make sure you start by disabling the timer:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
this.Hide();
Form1 frm = new Form1();
frm.ShowDialog();
}
You need to disable the timer before calling theShowDialog, so move the timer1.Enabled = false; to the first line. Also I suggest that you add the frm.Closed event so that your main form will close after you closed the second form: This is what you want:
timer1.Enabled = false;
Hide();
Form1 frm = new Form1();
frm.Closed += (s, args) => Close();
frm.ShowDialog();
Related
I need your help please
I have created a form and insert a timer & progress bar into it
when the value of progress bar reach to 100% I want to close this form and open the main form of my program
I write this code but when I Run the program it show this error :
( Form that is already displayed modally cannot be displayed as a modal dialog box. Close the form before calling showDialog.)
How I can resolve this problem
Form1 MainForm = new Form1();
public Welcome_window()
{
InitializeComponent();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Increment(10);
if (progressBar1.Value == 100)
{
this.Visible = false;
MainForm.Visible = false;
MainForm.ShowDialog();
this.Close();
}
}
}
I think the problem is that you don't stop the timer, so the tick event will be fired even if the progress already reached 100%.
Form1 MainForm = new Form1();
public Welcome_window()
{
InitializeComponent();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Increment(10);
if (progressBar1.Value == 100)
{
timer1.Stop();
this.Visible = false;
MainForm.ShowDialog();
this.Close();
}
}
I want to disable a button (button3) on a primary form when a second, modeless form (Form2) is loaded, and then re-enable the button when the modeless form is closed.
Here is what I've tried:
private void button3_Click(object sender, EventArgs e)
{
Form2 p = new Form2(label1.Text);
p.Show();
if (p.Shown)
this.button3.Click += new System.EventHandler(this.button3_Click);
else
this.button3.Click -= new System.EventHandler(this.button3_Click);
}
The best method for achieving this would be to disable button3 prior to showing Form2, and using the FormClosed event on Form2 to re-enable button3 once the form is closed:
public partial class Form1 : Form
{
...
private void button3_Click(object sender, EventArgs e)
{
// Instantiate the form and assign the FormClosed event
var form = new Form2(label1.Text);
form.FormClosed += Form2_FormClosed;
// Disable button3
button3.Enabled = false;
// Show the form
form.Show();
}
// Occurs when Form2 is closed
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
// Re-enable button3
button3.Enabled = true;
}
}
An alternative method, that assigns a lambda expression to the FormClosed event:
private void button3_Click(object sender, EventArgs e)
{
// Instantiate the form
var form = new Form2(label1.Text);
// Assign a lambda method to the FormClosed event to re-enable button3
form.FormClosed += (s, a) => button3.Enabled = true;
// Disable button3
button3.Enabled = false;
// Show the form
form.Show();
}
I did a similar thing, i m not sure to understand what you want to do but maybe it will help you.
public partial class Form2 : Form
{
private void button3_Click(object sender, EventArgs e)
{
Button3.Enabled = false;
Form2 p = new Form2(label1.Text);
p.ShowDialog();
//the code will stop here until you finish your work on form2
Button3.Enabled=true;
}
it works with me.
But if your form3 is just a short label use:
Button3.Enabled= false;
MessageBox.show ("blabla");
Button3.Enabled=true;
I have Form1 with 2 radio buttons (rb1 and rb2) and one ordinary button (btn). When I click on btn I should open Form2, as MDI child of Form1 if rb1 is checked, or as ordinary Dialog if rb2 is checked. Also, there can only be one Form2 opened at any moment.
This is my code:
public partial class Form1 : Form
{
Form2 f2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (f2 != null)
{
MessageBox.Show("Close form!");
return;
}
f2 = new Form2();
if (radioButton1.Checked == true)
{
this.IsMdiContainer = true;
f2.MdiParent = this;
f2.Show();
}
else
{
f2.Show();
}
f2.FormClosed += f2_FormClosed;
}
void f2_FormClosed(object sender, FormClosedEventArgs e)
{
this.IsMdiContainer = false;
f2 = null;
}
}
Everything works as it should except when I maximize Form2 as MDI child and then close it. After that screen stays the same (as I even didn't closed Form2) but I am able to open new Form2, and then Form1's title is "Form1 - [Form2]", and if I repeat the process it would be "Form1 - [Form2] - [Form2]", etc.
I figured out that I my f2_FormClosed method should be
void f2_FormClosed(object sender, FormClosedEventArgs e)
{
f2.Hide(); // <<<<<<<<-----------NEW
this.IsMdiContainer = false;
f2 = null;
}
but I don't know why; Form2 should be closed, I don't know why should I have to hide it?!
Thanks!
I agree with Hans, switching IsMdiContainer at run-time is wonky and is likely to produce other side-effects you haven't seen yet.
Seriously consider a different design for your app.
With that in mind, here's probably the stupidest hack I'll post all day:
public partial class Form1 : Form
{
Form2 f2;
System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
tmr.Interval = 100;
tmr.Enabled = false;
tmr.Tick += delegate (object sender, EventArgs e) {
tmr.Stop();
this.IsMdiContainer = false;
};
}
private void button1_Click(object sender, EventArgs e)
{
if (f2 != null)
{
MessageBox.Show("Close form!");
return;
}
f2 = new Form2();
f2.FormClosed += delegate(object sender2, FormClosedEventArgs e2) {
f2 = null;
};
if (radioButton1.Checked == true)
{
this.IsMdiContainer = true;
f2.FormClosed += delegate(object sender3, FormClosedEventArgs e3) {
tmr.Start();
};
f2.MdiParent = this;
}
f2.Show();
}
}
*I originally tried Invoking the call to change IsMdiContainer but that didn't work, so I switched to the Timer. Stupidity that works. Use this solution with caution...
I a form than I can open it also by pressing F1 and clicking so in this code first I check if the form has been opened or not.if was opened I just want to bring it to front. my problem is it wont bring in front by pressing F1 or clicking ToolStripMenuItem if i open it befor.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Form2 form2;
form2 = new Form2();
if (e.KeyCode.ToString() == "F1" && Application.OpenForms.OfType<Form2>().Count() <= 0)
// the user pressed the F1 key
form2.Show();
else
{
form2.TopMost = true;
form2.BringToFront();
form2.Focus();
form2.TopMost = false;
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2;
form2 = new Form2();
if (Application.OpenForms.OfType<Form2>().Count() <= 0)
form2.Show();
else
{
form2.TopMost = true;
form2.BringToFront();
form2.Focus();
form2.TopMost = false;
}
}
It doesn't work because you forgot to call form2.Show() in the else clause. The code is wrong, you don't want to create a new instance of Form2 if one already exists. Also, there's a bug in Winforms that makes Application.OpenForms lose track of form instances.
Best thing to do is to explicitly keep track of the lifetime of the form with its FormClosed event handler so you don't have to find it back later:
Form2 form2;
private void showForm2() {
if (form2 == null) {
form2 = new Form2();
form2.FormClosed += delegate { form2 = null; };
}
form2.Show();
form2.Focus();
}
Call showForm2 from your event handlers.
Just call form2.Show() and BringToFront. You should declare the `new Form2()' once though.
private Form2 form2;
private void ShowForm2()
{
if (form2 == null)
{
form2 = new Form2();
form2.FormClosed += delegate { form2 = null; };
}
form2.Show();
form2.BringToFront();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
ShowForm2();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowForm2();
}
From Hans' code, I added the delegate to handle null problem on closing the form and opening it again.
i have created an application in which a menustrip is present in which 2 buttons are there, one for ADD, and another for UPDATE & both controls are in a single form, means a button of add & update is there in a single form, whenever i press add button in menustrip update button will be disabled, and when i press update on menustrip the add button will disable. how to do this? i m doing this by show method but that form is opening multiple times using show().
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button1.Enabled = true;
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
you simply have to use a single form in this case. try using the singleton approach -
http://hashfactor.wordpress.com/2009/03/31/c-winforms-create-a-single-instance-form/
try using .ShowDialog() instead .Show() and no other form will be able to be clicked on until that one closes.
To do that you'll need to have an instance of that Form outside of those methods that you dismply show if the Form has already been created, or create and show it if it has not (this is the singleton pattern). Here's an example:
Form1 f2 = null;
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2 = new Form1();
f2.MdiParent = this;
f2.button1.Enabled = true;
}
f2.Show();
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2.MdiParent = this;
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
f2.Show();
}
One question on your disabling of the menu items though, how do you plan on re-enabling them after they have been disabled?
just try to check that form is already opened or not by using its Text Property.. if it is opened just focus on that form other wise show that form as normally
private void button1_Click(object sender, EventArgs e)
{
bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
if (f.Text == "Form1")
{
IsOpen = true;
f.Focus();
break;
}
}
if (IsOpen == false)
{
Form f1 = new Form1();
f1.Show();
}
}
Try This Guys Its Simple