Если я правильно понимаю, вы хотите контролировать видимость одной формы из другой формы.Для этого используйте атрибут .Visible
формы.Например:
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
Form2 TheForm2 = new Form2();
TheForm2.ShowDialog();
TheForm2.Visible = false;
}
Существуют и другие проблемы, связанные с тем, как вы это делаете, но я полагаю, что вы со временем разберетесь с ними или зададите другие вопросы:)
Изменить: ОК, я изменил ваш код, чтобы продемонстрировать, как заставить это работать.Код компилируется и запускается для меня и показывает, как сделать форму 2 изначально невидимой, а затем сделать ее видимой через 10 секунд.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TwoForms
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// If you do it this way you'll have to stop the application yourself
// Form1 TheForm = new Form1();
// Application.Run();
Application.Run(new Form1()); // When Form1 is closed, the application will exit.
}
}
}
Форма 1:
using System;
using System.Windows.Forms;
namespace TwoForms
{
public partial class Form1 : Form
{
private Timer t = new Timer();
public static int counter = 60;
public Form TheForm2;
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
this.Show(); // show Form1 just so we know it's really there
TheForm2 = new Form2();
// TheForm2.ShowDialog(); // Don't do this unless you really want a modal dialog
TheForm2.Show();
TheForm2.Visible = false; // A timer tick will later set visible true
}
void Timer_Tick(object sender, EventArgs e)
{
counter -= 1;
if (counter == 50)
TheForm2.Visible = true;
if (counter == 40)
MessageBox.Show("Time remaining " + counter.ToString());
}
}
}
Форма 2:
using System;
using System.Windows.Forms;
namespace TwoForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int userVal = int.Parse(textBox2.Text);
Form1.counter += userVal;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = Form1.counter.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}