Cancel form load

后端 未结 6 1346
猫巷女王i
猫巷女王i 2021-01-14 01:15

I have the following code :

This call the second form

private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Up         


        
相关标签:
6条回答
  • 2021-01-14 01:47

    try this

    private void Form_Load(object sender, EventArgs e) 
    { 
       switch(funct())
       {
          case 2:
             this.Close();
              break;
          case 3:
              this.Close();
               break; 
         default:
               MessageBox.Show("Something");
       }   
    }
    
    0 讨论(0)
  • 2021-01-14 01:49

    You can hide the form before loading and then set it back to visible in your if else conditions. e.g:

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

    And then:

    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();
            }
    
    0 讨论(0)
  • 2021-01-14 01:51

    You should probably do whatever check you're performing before you choose to open the form in the first place.

    So something like:

    if(funct() == "1")
    {
        var form = new Form();
        form.ShowDialog();
    }
    
    0 讨论(0)
  • 2021-01-14 01:55

    The best way to do so :

    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");
       }   
    }
    
    0 讨论(0)
  • 2021-01-14 01:57

    Maybe hide it first, then only show it if funct() == "1":

    private void Form_Load(object sender, EventArgs e)
    {
        this.Close();
    
        if (funct() == "1")
            MessageBox.Show("Something");
    }
    
    0 讨论(0)
  • 2021-01-14 02:07

    I assume Update_Load is your FormLoad Handler? That is called after your form has been displayed. If you don't want to display it, that's too late. Change your updateToolStripMenuItem_Click to this:

    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.");
    }
    

    And change your Update_Load to:

    WebClient webClient = new WebClient();
    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
    webClient.DownloadFileAsync(new Uri(dlUrl), "");
    
    0 讨论(0)
提交回复
热议问题