5

我有以下代码:

这称为第二种形式

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:把更多的代码

4

6 回答 6

9

您可以在加载之前隐藏表单,然后在您的if else条件下将其设置为可见。例如:

            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();
        }
于 2012-06-22T00:38:10.370 回答
2

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

所以像:

if(funct() == "1")
{
    var form = new Form();
    form.ShowDialog();
}
于 2012-06-22T00:28:32.747 回答
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");
   }   
}
于 2013-07-22T08:44:39.460 回答
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), "");
于 2012-06-22T00:34:01.443 回答
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");
   }   
}
于 2012-06-22T00:32:48.837 回答
0

也许先隐藏它,然后仅在 funct() == "1" 时显示它:

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

    if (funct() == "1")
        MessageBox.Show("Something");
}
于 2012-06-22T00:22:06.090 回答