c# - 主窗体运行时隐藏窗体也在启动时隐藏
问题描述
我有一个关于管理计算机时间使用的小项目。表格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());
}
}
}
和表格2:
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 放到文件夹 start up 中,让它在启动时运行。(我会尝试用 Registry 来制作)
解决方案
如果我理解正确,您想控制一个表单从另一个表单的可见性。为此,请使用.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.
}
}
}
表格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)
{
}
}
}
推荐阅读
- javascript - 如何将包含许多对象的一个对象的数组转换为对象数组
- spring - 使用 Spring Security 限制对新引入的角色的控制器类中某些方法的访问
- java - 如何从 java maven 项目生成 python protobuf
- firebase - Firestore:我收集了 1 个 doc.set() 和 2 个 snapshotChanges()
- php - 从数组中提取数据
- javascript - 是否有合适的时间例行删除yarn.lock/package-lock.json?
- java - 多级字符串拆分
- google-cloud-platform - 是否可以在云功能中获取项目元数据?
- microservices - JHipster - 如何在 application.yml 中添加到外部微服务的路由
- python - scrapy 如何在内部发送 Web 请求?