我有一个关于在计算机上管理时间使用的小项目。 表格1:
public partial class Form1 : Form
{
private Timer t = new Timer();
public static int counter = 60;
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
Form2 TheForm2 = new Form2();
TheForm2.ShowDialog();
}
void Timer_Tick(object sender, EventArgs e)
{
counter -= 1;
if (counter==20)
{
MessageBox.Show("Time remaining "+counter.ToString());
}
}
}
和Form2:
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)
{
}
}
最终程序:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 TheForm = new Form1();
Application.Run();
}
}
我试图使此应用程序在Windows上运行,并且Form1处于隐藏状态。但是我也想使form2不可见。并且仅在用户执行应用程序时显示。我该如何解决? 我只是将这个exe放到文件夹启动中,以使其在启动时运行。(我将尝试使用Registry进行制作)
答案 0 :(得分:0)
如果我理解正确,那么您想控制一种形式与另一种形式之间的可见性。为此,请使用表单的.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;
}
您执行此操作的方式还有其他问题,但是我想您会随着时间的推移进行梳理,或者发表其他问题:)
编辑: 好的,我已经修改了您的代码以演示如何进行这项工作。该代码将为我编译并运行,并显示如何使form2最初不可见,然后在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.
}
}
}
Form1:
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());
}
}
}
Form2:
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)
{
}
}
}