C# 以编程方式从另一个窗体打开一个窗体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15041382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
opening a window form from another form programmatically
提问by vow
I am making a Windows Forms application. I have a form. I want to open a new format run time from the original form on a button click. And then close this new form(after 2,3 sec) programatically but from a thread other than gui main thread.
我正在制作一个 Windows 窗体应用程序。我有一个表格。我想在运行时通过单击按钮从原始表单打开一个新表单。然后以编程方式关闭这个新表单(2,3 秒后),但从 gui 主线程以外的线程关闭。
- Can anybody guide me how to do it ?
- Will the new form affect or hinder the things going on in the original main form ? (if yes than how to stop it ?)
- 有人可以指导我怎么做吗?
- 新形式会影响还是阻碍原有主形式的发展?(如果是,那么如何阻止它?)
回答by mohamedHabib
You just need to use Dispatcher to perform graphical operation from a thread other then UI thread. I don't think that this will affect behavior of the main form. This may help you : Accessing UI Control from BackgroundWorker Thread
您只需要使用 Dispatcher 从 UI 线程以外的线程执行图形操作。我认为这不会影响主窗体的行为。这可能对您有所帮助: 从 BackgroundWorker 线程访问 UI 控件
回答by Amrit Sharma
To open from with button click please add the following code in the button event handler
要从按钮单击打开,请在按钮事件处理程序中添加以下代码
var m = new Form1();
m.Show();
Here Form1 is the name of the form which you want to open.
这里 Form1 是您要打开的表单的名称。
Also to close the current form, you may use
另外要关闭当前表单,您可以使用
this.close();
回答by Tacit
I would do it like this:
我会这样做:
var form2 = new Form2();
form2.Show();
and to close current form I would use
并关闭当前表单我会使用
this.Hide();
instead of
this.Hide();
代替
this.close();
check out this Youtube channel link for easy start-up tutorials you might find it helpful if u are a beginner
查看此Youtube 频道链接以获取简单的入门教程,如果您是初学者,您可能会发现它很有帮助
回答by Nivas
This is an old question, but answering for gathering knowledge. We have an original form with a button to show the new form.
这是一个古老的问题,但回答是为了收集知识。我们有一个带有按钮的原始表单来显示新表单。
The code for the button click is below
按钮点击的代码如下
private void button1_Click(object sender, EventArgs e)
{
New_Form new_Form = new New_Form();
new_Form.Show();
}
Now when click is made, New Form is shown. Since, you want to hide after 2 seconds we are adding a onload event to the new form designer
现在,当单击时,将显示新表单。因为,您想在 2 秒后隐藏我们正在向新的表单设计器添加一个 onload 事件
this.Load += new System.EventHandler(this.OnPageLoad);
This OnPageLoadfunction runs when that form is loaded
此OnPageLoad函数在加载该表单时运行
In NewForm.cs,
在NewForm.cs 中,
public partial class New_Form : Form
{
private Timer formClosingTimer;
private void OnPageLoad(object sender, EventArgs e)
{
formClosingTimer = new Timer(); // Creating a new timer
formClosingTimer.Tick += new EventHandler(CloseForm); // Defining tick event to invoke after a time period
formClosingTimer.Interval = 2000; // Time Interval in miliseconds
formClosingTimer.Start(); // Starting a timer
}
private void CloseForm(object sender, EventArgs e)
{
formClosingTimer.Stop(); // Stoping timer. If we dont stop, function will be triggered in regular intervals
this.Close(); // Closing the current form
}
}
In this new form , a timer is used to invoke a method which closes that form.
在这个新表单中,计时器用于调用关闭该表单的方法。
Here is the new form which automatically closes after 2 seconds, we will be able operate on both the forms where no interference between those two forms.
这是2秒后自动关闭的新表单,我们将能够在这两个表单之间没有干扰的情况下对这两个表单进行操作。
For your knowledge,
对于你的知识,
form.close()
will free the memory and we can never interact with that form again
form.hide()
will just hide the form, where the code part can still run
form.close()
将释放内存,我们再也无法与该表单交互
form.hide()
只会隐藏该表单,代码部分仍然可以在其中运行
For more details about timer refer this link, https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netframework-4.7.2
有关计时器的更多详细信息,请参阅此链接,https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netframework-4.7.2
回答by Anjan Thakuri
private void btnchangerate_Click(object sender, EventArgs e)
{
this.Hide(); //current form will hide
Form1 fm = new Form1(); //another form will open
fm.Show();
}
on click btn current form will hide and new form will open
单击 btn 当前表单将隐藏并打开新表单
回答by SeruiX
This might also help:
这也可能有帮助:
void ButtQuitClick(object sender, EventArgs e)
{
QuitWin form = new QuitWin();
form.Show();
}
Change ButtQuitto your button name and also change QuitWinto the name of the form that you made.
将ButtQuit更改为您的按钮名称,并将QuitWin更改为您创建的表单的名称。
When the button is clicked it will open another window, you will need to make another form and a button on your main form for it to work.
单击按钮时,它将打开另一个窗口,您需要在主窗体上创建另一个窗体和一个按钮才能使其工作。