C# 在C中加载winforms时显示第二个表单而不是第一个表单#
我有两张windows窗体。我想在加载第一个表单时显示第二个表单。但问题是,在加载第一个表单后,它会显示两个表单,而不会隐藏第一个表单。请检查我下面的代码。感谢您提供的任何解决方案C# 在C中加载winforms时显示第二个表单而不是第一个表单#,c#,forms,winforms,C#,Forms,Winforms,我有两张windows窗体。我想在加载第一个表单时显示第二个表单。但问题是,在加载第一个表单后,它会显示两个表单,而不会隐藏第一个表单。请检查我下面的代码。感谢您提供的任何解决方案 form2 frm2= new form2(); private void form1_Load(object sender, EventArgs e) { form1 frm1 = new form1(); frm1 = this; frm1.Hide(); fr
form2 frm2= new form2();
private void form1_Load(object sender, EventArgs e)
{
form1 frm1 = new form1();
frm1 = this;
frm1.Hide();
frm2.Show();
}
我们有以下问题:
isInitialized
设置为false
,并在首次运行Paint事件后将其更改为true
private void Form1_Load(object sender, EventArgs e)
{
// We start a new task to handle our UI operations
Task.Factory.StartNew(() =>
{
this.Hide();
this.Invoke(new Action(() =>
{
Form frm = new Form();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
}), null);
});
}
bool isInitialized = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if(isInitialized) return;
Form frm = new Form();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
this.Hide();
isInitialized = true;
}
2--->
private void Form1_Load(object sender, EventArgs e)
{
// We start a new task to handle our UI operations
Task.Factory.StartNew(() =>
{
this.Hide();
this.Invoke(new Action(() =>
{
Form frm = new Form();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
}), null);
});
}
bool isInitialized = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if(isInitialized) return;
Form frm = new Form();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
this.Hide();
isInitialized = true;
}
我们有以下问题:
isInitialized
设置为false
,并在首次运行Paint事件后将其更改为true
private void Form1_Load(object sender, EventArgs e)
{
// We start a new task to handle our UI operations
Task.Factory.StartNew(() =>
{
this.Hide();
this.Invoke(new Action(() =>
{
Form frm = new Form();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
}), null);
});
}
bool isInitialized = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if(isInitialized) return;
Form frm = new Form();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
this.Hide();
isInitialized = true;
}
2--->
private void Form1_Load(object sender, EventArgs e)
{
// We start a new task to handle our UI operations
Task.Factory.StartNew(() =>
{
this.Hide();
this.Invoke(new Action(() =>
{
Form frm = new Form();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
}), null);
});
}
bool isInitialized = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if(isInitialized) return;
Form frm = new Form();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
this.Hide();
isInitialized = true;
}
从您提到的代码中,我了解到您正试图关闭一个登录表单,并试图打开一个主表单。您也可以尝试使用这段代码。此代码将进入您的程序.cs
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form_login= new Form1();
Application.Run(form_login);
if (form_login.Authentication_success)
{
Application.Run(new Form2());
}
}
在form1中声明一个公共变量
public bool Authentication_success= true;
接下来,在Form1加载事件中,添加以下代码
Authentication_success= true;
this.Close();
希望这个解决方案是有帮助的。祝你好运。从你提到的代码中,我知道你试图关闭一个登录表单,并试图打开一个主表单。您也可以尝试使用这段代码。此代码将进入您的程序.cs
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form_login= new Form1();
Application.Run(form_login);
if (form_login.Authentication_success)
{
Application.Run(new Form2());
}
}
在form1中声明一个公共变量
public bool Authentication_success= true;
接下来,在Form1加载事件中,添加以下代码
Authentication_success= true;
this.Close();
希望这个解决方案是有帮助的。祝你好运。另一个解决方案:
private void Form1_Load(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
if (InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
}));
return;
}
});
}
但我认为在你的问题下的评论中,以下建议更好:)另一种解决方案:
private void Form1_Load(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
if (InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
frm.FormClosing += (s, o) => this.Show();
}));
return;
}
});
}
但我认为在您的问题下的评论中,以下建议更好:)您想加载一个表单,只是想立即隐藏它并显示另一个表单?为什么?只需执行
Application.Run(newloginform())
后跟Application.Run(newmainform())代码>在Main
方法中。此外,加载事件在显示表单之前发生。在展示之前试图隐藏一些东西是没有用的。您肯定需要模式登录,因此至少可以调用ShowModal而不是Show。只需使用ShowDialog()即可。并注意其返回值。不要把你自己的不安全登录过程放在Windows提供的安全登录过程之上,你不是在与Internet上的陌生人交谈。你想加载一个表单,只是为了立即隐藏它并显示另一个表单?为什么?只需执行Application.Run(newloginform())
后跟Application.Run(newmainform())代码>在Main
方法中。此外,加载事件在显示表单之前发生。在展示之前试图隐藏一些东西是没有用的。您肯定需要模式登录,因此至少可以调用ShowModal而不是Show。只需使用ShowDialog()即可。并注意其返回值。不要把你自己的不安全登录过程放在Windows提供的安全登录过程之上,你不是在互联网上与陌生人交谈。我问题下面的解决方案(注释)不是我想要的。当我的情况属实时,我想让我的第二份表格显示出来。因此,如果我直接在program.cs中更改,那么我不能使用任何条件来检查它,第二个表单应该显示或不应该显示。无论如何,你的解决方案也很有效。Thx:)我问题下面的解决方案(评论)不是我想要的。当我的情况属实时,我想让我的第二份表格显示出来。因此,如果我直接在program.cs中更改,那么我不能使用任何条件来检查它,第二个表单应该显示或不应该显示。无论如何,你的解决方案也很有效。Thx:)对于第一个解决方案方法,我得到一个错误:“跨线程操作无效:从创建控件“Form1”的线程以外的线程访问控件“Form1”。(在this.Hide()中);第二个解决方案很有效。但我只是好奇使用Load event和Paint event之间的区别是什么?对于第一个解决方案方法,我得到了一个错误:“跨线程操作无效:从创建控件的线程以外的线程访问控件‘Form1’。”;第二个解决方案很有效。但我只是好奇,使用Load事件和Paint事件有什么区别?