标签内容和进度条

时间:2013-05-16 10:26:01

标签: c# winforms label

我创建了Windows窗体应用程序,并添加了进度条以显示已加载文件的进度,并在进度条上方标记以显示每个文件的路径,但总和路径很长并且显示在行中但是我的宽度形式小于某些路径不是全部出现,我在堆栈溢出和谷歌中看到多个解决方案,但没有用作make auto size false,dock fill和change size.I想要方法动态改变路径的长度。

2 个答案:

答案 0 :(得分:0)

要获得显示的整个路径(例如换行),您需要将Height的{​​{1}}设置为某个内容,让我们说Label。所以它可能类似于50 Label Height50 Width,然后是300。{ / p>

此外,您的问题似乎是您正在主线程上执行长时间运行的操作。这就是ProgressBar不令人耳目一新的原因。您需要利用Label。所以,为它添加一个类变量:

BackgroundWorker

然后在private BackgroundWorker _worker = new BackgroundWorker(); 执行此操作:

ctor

然后是那些处理程序:

_worker.WorkerReportsProgress = true;
_worker.DoWork += DoBackgroundWork;
_worker.ProgressChanged += ReportProgress;

您可能想要考虑的最后一件事就是将字符串切断为关闭到标签的长度,而不是试图将其换行。下面是一组方法,证明了一种可能的解决方案。

需要注意的一点是是截止程序利用了bin搜索,因此 难以置信 不准确。那是什么意思?好吧,假设你有一个长度为150个字符的字符串(如下例中的那个字符串),并且第一次将它切成两半并测量字符串。如果该宽度小于最大宽度(例如private void DoBackgroundWork(object sender, DoWorkEventArgs e) { // do the work you're doing in here -but add the below line when you want // to update the text of your label _worker.ReportProgress(1, "Your label text here"); } private void ReportProgress(object sender, ProgressChangedEventArgs e) { this.label.Text = e.UserState as string; } 的宽度),它只是返回该字符串。这样做有效,但标签长度上可能留有空间,可能会截断更少的字符。

为了使这个更准确,你必须做更多的传球和IMO,这不足以做出可枚举的传球。

Label

答案 1 :(得分:0)

using System;

使用System.ComponentModel; 使用System.Windows.Forms;

命名空间BackgroundWorkerSimple {     公共部分类Form1:表格     {         公共Form1()         {             的InitializeComponent();             backgroundWorker1.WorkerReportsProgress = true;             backgroundWorker1.WorkerSupportsCancellation = true;         }

    private void startAsyncButton_Click(object sender, EventArgs e)
    {
        if (backgroundWorker1.IsBusy != true)
        {
            // Start the asynchronous operation.
            backgroundWorker1.RunWorkerAsync();
        }
    }

    private void cancelAsyncButton_Click(object sender, EventArgs e)
    {
        if (backgroundWorker1.WorkerSupportsCancellation == true)
        {
            // Cancel the asynchronous operation.
            backgroundWorker1.CancelAsync();
        }
    }

    // This event handler is where the time-consuming work is done. 
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; i <= 10; i++)
        {
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                break;
            }
            else
            {
                // Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500);
                worker.ReportProgress(i * 10);
            }
        }
    }

    // This event handler updates the progress. 
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
    }

    // This event handler deals with the results of the background operation. 
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled == true)
        {
            resultLabel.Text = "Canceled!";
        }
        else if (e.Error != null)
        {
            resultLabel.Text = "Error: " + e.Error.Message;
        }
        else
        {
            resultLabel.Text = "Done!";
        }
    }
}

}