隐藏表单并表示当点击了按钮,另一个Windows窗体应用程序 [英] Hiding a form and showing another when a button is clicked in a Windows Forms application
问题描述
我在做一个应用程序的Windows窗体应用程序。起初,会出现一定的形式,用户点击下一步按钮后,这种形式应该被隐藏和显示另一种形式。
我试图做到这一点。我设法隐藏当前形式,而是下一个会不会显示
下面是我的尝试:
这是该按钮的事件处理程序
私人无效button1_Click_1(对象发件人,EventArgs五)
{
如果(!richTextBox1.Text = NULL)
{
THIS.VISIBLE = FALSE;
}
,否则
MessageBox.Show(插入属性第一!);
}
这是主要的功能:
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Form2的窗体2 =新Form2的();
Form1的Form1中=新Form1的();
form2.Hide();
Application.Run(Form1中);
,而(真)
{
如果(form1.Visible ==假)
form2.Show();
}
}
的虽然声明不会执行,直到Form1的关闭之后 - 因为它是主要的消息循环外
删除,并修改代码的第一位为:
私人无效button1_Click_1(对象发件人,EventArgs五)
{
如果(richTextBox1.Text!= NULL)
{
THIS.VISIBLE = FALSE;
Form2的窗体2 =新Form2的();
form2.show();
}
,否则MessageBox.Show(插入属性第一!);
}
这是不是达到你正在寻找最好的方式做虽然。相反,考虑向导的设计模式。
另外,您可以实现处理两种形式的终身自定义的ApplicationContext。实现闪屏的一个例子是在这里,这应该设置你在正确的道路上。
的 http://www.codeproject.com/KB/cs/applicationcontextsplash.aspx?display=Print
I am doing an application a Windows Form application. At first, a certain form appears, and after the user hits the next button, this form should be hidden and another form is shown.
I tried to do it. I managed to hide the current form, but the next one won't show.
Here is my attempt:
This is the button's event handler
private void button1_Click_1(object sender, EventArgs e)
{
if (richTextBox1.Text != null)
{
this.Visible=false;
}
else
MessageBox.Show("Insert Attributes First !");
}
This is the main function:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form2 form2 = new Form2();
Form1 form1 = new Form1();
form2.Hide();
Application.Run(form1);
while (true)
{
if (form1.Visible == false)
form2.Show();
}
}
The While statement will not execute until after form1 is closed - as it is outside the main message loop.
Remove it and change the first bit of code to:
private void button1_Click_1(object sender, EventArgs e)
{
if (richTextBox1.Text != null)
{
this.Visible=false;
Form2 form2 = new Form2();
form2.show();
}
else MessageBox.Show("Insert Attributes First !");
}
This is not the best way to achieve what you are looking to do though. Instead consider the Wizard design pattern.
Alternatively you could implement a custom ApplicationContext that handles the lifetime of both forms. An example to implement a splash screen is here, which should set you on the right path.
http://www.codeproject.com/KB/cs/applicationcontextsplash.aspx?display=Print
这篇关于隐藏表单并表示当点击了按钮,另一个Windows窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!