有两个窗体F1和F2,F2是下载文件用的。目的是,F2下载完成后才出现F1。F1启动F2,代码如下:        private void F1_Load(object sender, EventArgs e)
        {
            F2 f2 = new F2();
            f2.Show();
        }
F2窗体里面下载文件,我用的是异步,来实现进度条效果,代码如下:private void F2_Load(object sender, EventArgs e)
{
Start_DownFile(string URL, string FileNamePath);
}        //定义委托
        delegate void Delegate_DownFile(string URL, string FileNamePath);        //回调函数
        private void CallBackMethod_DownFile(IAsyncResult ar)
        {
            Delegate_DownFile dn = (Delegate_DownFile)ar.AsyncState;
        }        //用线程方式启动
        private void Start_DownFile(string URL, string FileNamePath)
        {
            Delegate_DownFile dn = new Delegate_DownFile(DownFile);
            AsyncCallback acb = new AsyncCallback(CallBackMethod_DownFile);
            IAsyncResult iar = dn.BeginInvoke(URL, FileNamePath, acb, dn);                   }        //下载文件
        private void DownFile(string URL, string FileNamePath)
        {            try
            {
                WebClient client = new WebClient();                //下载过程中
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
                //下载完成
                client.DownloadFileCompleted += client_DownloadFileCompleted;                Uri uri = new Uri(URL);
                client.DownloadFileAsync(uri, FileNamePath);
            }
            catch (UriFormatException ex)
            {
                
            }        }        private void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            SetStatus1(e.ProgressPercentage);
        }delegate void SetTextCallback1(int i);        public void SetStatus1(int i)
        {
            if (this.Pbar.InvokeRequired)
            {
                SetTextCallback1 d = new SetTextCallback1(SetStatus1);
                this.Invoke(d, new object[] { i });
            }
            else
            {
                Pbar.Value =i;
                PbarV = Pbar.Value;
            }        }但这样做了后,F2启动正确,但F1也显示了。如何做能让F2下载完成后 才显示F1。

解决方案 »

  1.   

    代码可能不全。但F2下载没有问题。核心问题可能是 F1和F2的关系。
      

  2.   


        private void F1_Load(object sender, EventArgs e) 
            { 
                F2 f2 = new F2(); 
                this.Hide();
                f2.Show(); 
            } 
    F2窗体里面下载文件,我用的是异步,来实现进度条效果,代码如下: private void F2_Load(object sender, EventArgs e) 

    Start_DownFile(string URL, string FileNamePath); 
    }         //定义委托 
            delegate void Delegate_DownFile(string URL, string FileNamePath);         //回调函数 
            private void CallBackMethod_DownFile(IAsyncResult ar) 
            { 
                Delegate_DownFile dn = (Delegate_DownFile)ar.AsyncState; 
            }         //用线程方式启动 
            private void Start_DownFile(string URL, string FileNamePath) 
            { 
                Delegate_DownFile dn = new Delegate_DownFile(DownFile); 
                AsyncCallback acb = new AsyncCallback(CallBackMethod_DownFile); 
                IAsyncResult iar = dn.BeginInvoke(URL, FileNamePath, acb, dn);                  }         //下载文件 
            private void DownFile(string URL, string FileNamePath) 
            {             try 
                { 
                    WebClient client = new WebClient();                 //下载过程中 
                    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged); 
                    //下载完成 
                    client.DownloadFileCompleted += client_DownloadFileCompleted;                 Uri uri = new Uri(URL); 
                    client.DownloadFileAsync(uri, FileNamePath); 
                    f2.Hide();
                    this.Show();
    //隐藏f2显示f1            } 
                catch (UriFormatException ex) 
                { 
                    
                }         }         private void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
            { 
                SetStatus1(e.ProgressPercentage); 
            } delegate void SetTextCallback1(int i);         public void SetStatus1(int i) 
            { 
                if (this.Pbar.InvokeRequired) 
                { 
                    SetTextCallback1 d = new SetTextCallback1(SetStatus1); 
                    this.Invoke(d, new object[] { i }); 
                } 
                else 
                { 
                    Pbar.Value =i; 
                    PbarV = Pbar.Value; 
                }         } 
      

  3.   

     private void F1_Load(object sender, EventArgs e) 
            { 
                F2 f2 = new F2(); 
                this.Hide();//隐藏f1
                f2.Show(); 
            }   //下载完成 
                    client.DownloadFileCompleted += client_DownloadFileCompleted;                 Uri uri = new Uri(URL); 
                    client.DownloadFileAsync(uri, FileNamePath); 
                    f2.Hide();
                    this.Show();
    //隐藏f2显示f1
      

  4.   

    你可以当下载完成时在
     F2 f2 = new F2(); 
     f2.Show(); 
      

  5.   

    可能是我的意思没有说明白:F1启动F2,F2下载完成后才显示F1.这是前提,不能改的。这该咋做呢?