如何在主窗体中隐藏窗体

发布于 2024-12-11 21:57:24 字数 501 浏览 0 评论 0原文

我一整天都在调整我的程序,但我在隐藏一个会弹出“请稍候”的表单时遇到了问题

,例如:

    private void button12_Click(object sender, EventArgs e)
    {
        form2 wait = new form2();
        pw.Show();
    }
    private void button13_Click(object sender, EventArgs e)
    {
        form2 wait = new form2();
        pw.Hide();
    }

这不会起作用,尽管我确信这对于休闲 C# 程序员来说并不是什么新闻。有没有一种简单的方法可以完成我正在尝试的事情?我尝试在网上搜索,确实找到了一些东西,尽管我不能 100% 确定他们想要做什么。我本来想找一个例子来向您展示,但我关闭了页面 - 典型。但是我认为他们试图超越该节目并让您用 bool 控制 .show ?

I have been tweaking my program all day and I am having a problem hiding a form which will pop up saying "Please wait"

For example:

    private void button12_Click(object sender, EventArgs e)
    {
        form2 wait = new form2();
        pw.Show();
    }
    private void button13_Click(object sender, EventArgs e)
    {
        form2 wait = new form2();
        pw.Hide();
    }

This will not work, although I am sure this isn't news to the casual C# programmer. Is there a simple way to do what I am attempting? I have tried searching online and I did find something although I wasn't 100% sure what they were trying to do. I was going to find an example to show you but I closed page - Typical. However I think they were trying to overide the show and give you control over the .show with a bool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

也只是曾经 2024-12-18 21:57:24

该代码未按您预期的方式工作,因为 button12_Click 内部的 form2button13_click< 内部的 form2 不同/代码>。请注意,您使用了 new 关键字两次。因此,在 button13_click 中,您正在创建一个新的 form2,然后隐藏它,即使您还没有显示它!

相反,您可以创建一个 form2 实例来在两个方法之间共享:

//define this code outside both of the methods below
form2 _waitForm = new form2();

private void button12_Click(object sender, EventArgs e)
{
    _waitForm.Show();
}
private void button13_Click(object sender, EventArgs e)
{
    //this will hide the same form2 that was shown in button12_Click
    _waitForm.Hide();
}

The code isn't working as you expect it to because the form2 inside of button12_Click is different from the form2 inside of button13_click. Notice that you are using the new keyword twice. So in button13_click, you are creating a new form2, and then hiding it, even though you haven't even shown it yet!

Instead you can create a single form2 instance to share between your two methods:

//define this code outside both of the methods below
form2 _waitForm = new form2();

private void button12_Click(object sender, EventArgs e)
{
    _waitForm.Show();
}
private void button13_Click(object sender, EventArgs e)
{
    //this will hide the same form2 that was shown in button12_Click
    _waitForm.Hide();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文