直到控件被更改,表单才出现.为什么? [英] Form doesn't appear until control is changed. Why?

查看:56
本文介绍了直到控件被更改,表单才出现.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我被要求开发一个非常简单的GUI来前端是一个控制台程序,以便使用户确信该程序实际上正在做某事.我用一个表单创建了一个Windows应用程序.我设计了表单,以便有一个显示正在工作.请站在...旁边"的标签.在Form1()方法(?)中,我做了一些工作&.跟踪程序是否成功完成其任务.根据成功或失败,我更改标签中的文本,更改图片框中的图像,并使用户可以单击以取消该表单的确定"按钮可见.

这是我遇到的问题:在我进入改变标签文本的方法之前,表单实际上不会出现.因此,实际上,在程序完成其工作之前,用户根本看不到该表格.我不确定自己在做什么错.我希望该窗体在调用InitializeComponent()之后立即出现.任何建议将不胜感激.

Hello all,

I was asked to develop an extremely simple GUI to front-end what was a console program in order to give the user assurance that the program actually is doing something. I created a Windows application with a single form. I designed the form so that there is a label which displays "Working. Please stand by...". In the Form1() method(?) I do some work & track whether or not the program completed its task successfully. Depending on success or failure, I change the text in the label, alter the image in a picturebox and make an OK button visible that the user can click to dismiss the form.

Here''s the problem I''m running into: The form will not actually appear until I drop into the method which alters the text in the label. So, in effect, the user doesn''t see the form at all until the program has completed its work. I''m not sure what I am doing wrong. I would expect the form to appear immediately after calling InitializeComponent(). Any advice would be greatly appreciated.

namespace Blah
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            bool result = doStuff();
            if (result) 
                success();
            else
                fail();
        }

        private void success()
        {
            lblStatus.Text = "Process was successful.";
            pbxStatus.Image = global::IACRenew2.Properties.Resources.check;
            btnOK.Visible = true;
        }

        private void fail()
        {
            lblStatus.Text = "An error occurred.  Please contact support.";
            pbxStatus.Image = global::IACRenew2.Properties.Resources.error;
            btnOK.Visible = true;
        }
    }
}

推荐答案

您不应执行会影响表单初始化的任务(doStuff),即使在构造函数中也是如此.将其放入Load事件处理程序中.
You should not do tasks (doStuff) that affect form initialization, even less in a constructor. Put it in Load event handler.


这篇关于直到控件被更改,表单才出现.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆