我应该在代码中将标签中的标签更改为true?

时间:2017-03-11 21:29:05

标签: c# .net winforms

private void downloadFile(IEnumerable<string> urls)
{
    foreach (var url in urls)
    {
        _downloadUrls.Enqueue(url);
    }

    // Starts the download
    btnStart.Text = "Downloading...";
    btnStart.Enabled = false;
    pBarFileProgress.Visible = true;

    label2.Visible = true;
    label3.Visible = true;
    label4.Visible = true;
    label7.Visible = true;

    DownloadFile();
}

private void DownloadFile()
{
    if (_downloadUrls.Any())
    {
        WebClient client = new WebClient();
        client.DownloadProgressChanged += client_DownloadProgressChanged;
        client.DownloadFileCompleted += client_DownloadFileCompleted;

        url = _downloadUrls.Dequeue();

        if (url.Contains("animated") && url.Contains("infra"))
        {
            string startTag = "animated/";
            string endTag = "/infra";

            int index = url.IndexOf(startTag);
            int index1 = url.IndexOf(endTag);

            fname = url.Substring(index + 9, index1 - index - 9);
            var countryName = codeToFullNameMap[fname];
            downloadDirectory = tbxMainDownloadPath.Text;
            downloadDirectory = Path.Combine(downloadDirectory, countryName);
        }
        else
        {
            fname = "Tempfile";
            downloadDirectory = tbxMainDownloadPath.Text;
        }

        client.DownloadFileAsync(new Uri(url), downloadDirectory + "\\" + fname + ".gif");

        lastDownloadedFile = downloadDirectory + "\\" + fname + ".gif";
        label4.Text = downloadDirectory + "\\" + fname + ".gif";
        return;
    }

    // End of the download
    label2.Text = "All files have been downloaded";
}

private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error != null)
    {
        // handle error scenario
        throw e.Error;
    }
    if (e.Cancelled)
    {
        // handle cancelled scenario
    }

    if (url.Contains("animated") && url.Contains("infra"))
    {
        Image img = new Bitmap(lastDownloadedFile);
        Image[] frames = GetFramesFromAnimatedGIF(img);
        foreach (Image image in frames)
        {
            countFrames++;
            image.Save(downloadDirectory + "\\" + fname + ".gif");
        }
    }

    label2.Text = "Download Complete";
    tracker.NewFile();
    DownloadFile();
}

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    tracker.SetProgress(e.BytesReceived, e.TotalBytesToReceive);
    pBarFileProgress.Value = (int)(tracker.GetProgress() * 100.0);
    label3.Text = e.BytesReceived + "/" + e.TotalBytesToReceive;
    label7.Text = tracker.GetBytesPerSecondString();
    label2.Text = "Downloading";
}

开始下载按钮

private void btnStart_Click(object sender, EventArgs e)
{
    downloadFile(lines);            
}

现在,当我点击下载按钮时,我将标签visible属性更改为true。但是在我进入progressChanged事件并更新标签之前,我会看到标签几秒钟。

问题是我应该在哪里将标签2,3,4,7的可见性更改为true,并且它会将其更改一次为真?

1 个答案:

答案 0 :(得分:1)

如果您在实际为其设置任何文字之前担心显示标签,您应该确保在获得进度更改事件之前将文本设置为它们 - 例如

label3.Text = "0/calculating...";
label7.Text = "0 kbps";
label2.Text = "Preparing for download...";

downloadFiles方法中(将.Visible设置为true之前或之后)。另一方面 - 为您的标签选择更好的名称! label3可能会更好lblTotalDownloadedlabel7lblDownloadSpeed等等。