取消表单加载

时间:2012-06-22 00:19:40

标签: c# winforms forms

我有以下代码:

这称为第二种形式

private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Update fm = new Update();
            fm.ShowDialog();
        }

这是构造函数

    public Update()
    {
        InitializeComponent();
    }

这是负载

    private void Update_Load(object sender, EventArgs e)
    {
        String ver = checkver();
        if (ver == "update")
        {
            if (RemoteFileExists(dlUrl) == true)
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                webClient.DownloadFileAsync(new Uri(dlUrl), "");
            }
            else
                MessageBox.Show("An error occurred. Please try later.");
        }
        else if (ver == "newest")
        {
            MessageBox.Show("You are currently using the newest version.");
            this.Close();
        }
        else
        {
            this.Close();
        }
    }

我的问题是,当函数结果为2或3时,表单显示为毫秒然后关闭(闪烁)。我希望表单不闪烁。有可能吗?

我尝试使用 this.Hide() this.Visible = False ,但没有任何帮助。

编辑:我把原始代码 EDIT2:放更多代码

6 个答案:

答案 0 :(得分:8)

您可以在加载前隐藏表单,然后在if else条件下将其设置为可见。 e.g:

            MyForm myForm = new MyForm();
            myForm.Opacity = 0;
            myForm.Show();

然后:

if (ver == "update")
        {
            if (RemoteFileExists(dlUrl) == true)
            {
               myForm.Opacity = 100;
               ...

            }
            else
                MessageBox.Show("An error occurred. Please try later.");
        }
        else if (ver == "newest")
        {
            MessageBox.Show("You are currently using the newest version.");
            this.Close();
        }
        else
        {
            this.Close();
        }

答案 1 :(得分:2)

最好的方法:

private void Form_Load(object sender, EventArgs e) 
{ 
   switch(funct())
   {
      case 2:
         this.BeginInvoke(new MethodInvoker(this.Close));
          break;
      case 3:
         this.BeginInvoke(new MethodInvoker(this.Close));
           break; 
     default:
           MessageBox.Show("Something");
   }   
}

答案 2 :(得分:1)

在您选择首先打开表单之前,您可能应该执行任何检查。

类似于:

if(funct() == "1")
{
    var form = new Form();
    form.ShowDialog();
}

答案 3 :(得分:1)

我认为Update_Load是您的FormLoad处理程序?在您的表单显示之后,这称为。如果你不想显示它,那就太晚了。将您的updateToolStripMenuItem_Click更改为此:

String ver = checkver();
if (ver == "update")
{
    if (RemoteFileExists(dlUrl))
    {
        Update fm = new Update();
        fm.ShowDialog();
    }
    else
        MessageBox.Show("An error occurred. Please try later.");
}
else if (ver == "newest")
{
    MessageBox.Show("You are currently using the newest version.");
}

将您的Update_Load更改为:

WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri(dlUrl), "");

答案 4 :(得分:0)

可能先隐藏它,然后只显示funct()==“1”:

private void Form_Load(object sender, EventArgs e)
{
    this.Close();

    if (funct() == "1")
        MessageBox.Show("Something");
}

答案 5 :(得分:0)

试试这个

private void Form_Load(object sender, EventArgs e) 
{ 
   switch(funct())
   {
      case 2:
         this.Close();
          break;
      case 3:
          this.Close();
           break; 
     default:
           MessageBox.Show("Something");
   }   
}