f2.show()方法不显示form2中的进度条

发布于 2024-11-18 02:56:37 字数 894 浏览 1 评论 0原文

我的申请有 2 个表格。第一个是我完成所有工作的地方,第二个只是用于显示进度条。

我想从主窗体打开第二个。如果我使用

Form2 newForm = new Form2();
newForm.Show();

Form2 在需要打开和关闭时打开和关闭,但我看不到进度条。我只能看到一个空白而不是它。

当我使用时,

Form2 newForm = new Form2();
newForm.ShowDialog();

我可以看到进度条,但 Form2 在需要时不会关闭。它永远运行,我该怎么办?

我使用静态公共变量 closeForm 来关闭第二个表单。当我需要关闭我设置的表单

closeForm = true;

和第二个表单时,我有一个计时器,

private void timer1_Tick(object sender, EventArgs e)
{
    if (Form1.closeForm)
    {
        this.Dispose();
        this.Close();
        return;
    }
    else
    {
        progVal++;
        progressBar1.Value = (progVal % 100);
    }            
}

这是我放置 ProgressBar 值并关闭表单的地方。

当我使用 show 方法时,我只看到空白而不是 form2 中的控件。不仅仅是进度条,我希望 form1 关闭 form2

I have an application that has 2 forms. First one is where I do all the job and second one is just for displaying a progressbar.

I want to open the second one from the main form. if I use

Form2 newForm = new Form2();
newForm.Show();

Form2 opens and closes when it needs to open and close, but I cannot see the progress bar. I just can see a blank instead of it.

When I use

Form2 newForm = new Form2();
newForm.ShowDialog();

I can see the progressbar but Form2 doesn't close when it needs. It runs forever, what should I do?

I use a static public variable closeForm to close the second form. When I need to close the form I set

closeForm = true;

and in the second form, I have a timer

private void timer1_Tick(object sender, EventArgs e)
{
    if (Form1.closeForm)
    {
        this.Dispose();
        this.Close();
        return;
    }
    else
    {
        progVal++;
        progressBar1.Value = (progVal % 100);
    }            
}

this is where I put the ProgressBar value and close the form.

When I use show method, I only see blanks instead of the controls in form2. not just the progressbar, and I want form1 to close form2

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

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

发布评论

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

评论(2

九厘米的零° 2024-11-25 02:56:37

首先,您需要向进度条报告进度,

 int iProgressPercentage = (int)(dProgressPercentage * 100);
 // update the progress bar
 progressBar1.ReportProgress(iProgressPercentage);

尝试先这样做,然后调用 this.close();

first of all you need to report progress to progressbar

 int iProgressPercentage = (int)(dProgressPercentage * 100);
 // update the progress bar
 progressBar1.ReportProgress(iProgressPercentage);

try doing that first then call this.close();

大海や 2024-11-25 02:56:37

正如我在评论中所说,您需要从这里检查模态对话框 Form.ShowDialog方法,我只是在那里引用以下表单:

您可以使用此方法在应用程序中显示模式对话框。调用此方法时,直到对话框关闭后才会执行其后面的代码。

至于为什么您无法使用 Show();Form2 上看到 ProgressBar,您需要提供有关如何处理它的更多信息,如下所示如果我将您的程序分成两部分并使用两个按钮单击来运行它们(单击button1以显示Form2;然后单击button2以关闭它)我可以看到您的预期结果:进度条

如果没有您的进一步信息,我最好的猜测是正在运行的某些东西会阻止 Form2 更新其 GUI。

As I said above in the comment, you need to check Modal dialog from here Form.ShowDialog Method, and I just quote the following form there:

You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.

As why you can't see your ProgressBar on Form2 with Show(); you need to provide more information of how you handles it, as if I separate your program into two parts and use two button click to run them (Click button1 to show Form2; and click button2 to close it) I can see your expected result: the progressbar.

Without your further information, my best guess is something running prevents the Form2 to update its GUI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文