我创建 Windows 窗体应用程序并添加进度条以显示加载文件的进度,并在进度条上方添加标签以显示每个文件的路径,但总路径很长并出现在行中,但我的宽度形式很小,因为某些路径没有出现总而言之,我在堆栈溢出和谷歌中看到了不止一种解决方案,但没有有用,因为使自动大小为 false 、停靠填充和更改大小。我想要动态更改路径长度的方法。
3 回答
using System;
使用 System.ComponentModel;使用 System.Windows.Forms;
命名空间 BackgroundWorkerSimple { 公共部分类 Form1 : Form { 公共 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!";
}
}
}
}
要让整个路径出现(例如换行),您需要将 的 设置Height
为Label
某事,比如说50
. 所以它可能是类似于 a和Label
a Height
of的东西,然后是 a在其下方。50
Width
300
ProgressBar
此外,您的问题似乎是您正在主线程上执行长时间运行的操作。这就是为什么Label
不令人耳目一新。您需要利用BackgroundWorker
. 因此,为其添加一个类变量:
private BackgroundWorker _worker = new BackgroundWorker();
然后在ctor
这样做:
_worker.WorkerReportsProgress = true;
_worker.DoWork += DoBackgroundWork;
_worker.ProgressChanged += ReportProgress;
然后对于那些处理程序:
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;
}
您可能要考虑的最后一件事是将绳子切断到接近标签长度的位置,而不是试图将其包裹起来。下面是一组方法,证明了一个可能的解决方案。
需要注意的一点是,截止过程利用了 bin 搜索,因此它的准确度并不高。我的意思是什么?好吧,假设您有一个长度为 150 个字符的字符串(如下例中的字符串),并且第一次将其切成两半并测量字符串。如果该宽度小于最大宽度(例如 的宽度Label
),它只会返回该字符串。这行得通,但是标签的长度上可能有剩余空间,并且可能会截断更少的字符。
为了使这一点更准确,您必须进行更多的传球,而 IMO,这对于进行可枚举的传球来说还不够重要。
public partial class Form1 : Form
{
string[] chars = new string[]
{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var filePath = BuildString(150);
var g = this.label1.CreateGraphics();
var size = g.MeasureString(filePath, this.Font);
var oneCharacterLen = (size.Width / (float)filePath.Length);
if (size.Width > ((float)this.label1.Width - (oneCharacterLen * 3f)))
{
this.label1.Text = CutoffStringBinSearch(g, string.Format("{0}", filePath), 0, this.label1.Width);
}
}
private string CutoffStringBinSearch(Graphics g, string s, int startIndex, int maxWidth)
{
var midPoint = (s.Length - startIndex) / 2;
var subString = string.Format("{0}...", s.Substring(startIndex, midPoint));
var len = g.MeasureString(subString, this.Font);
if (len.Width > (float)maxWidth) { return CutoffStringBinSearch(g, s.Substring(startIndex, midPoint), 0, maxWidth); }
return subString;
}
private string BuildString(int len)
{
string[] s = new string[len];
var random = new Random(0);
for (int i = 0; i < len; i++)
{
s[i] = chars[random.Next(0, chars.Length - 1)];
}
return string.Join("", s);
}
}
使用 labelX.Refresh()。
label1.Text = "First text";
Thread.Sleep(1500); //*This line is only for testing purpose as the change is pretty quick*.
progressBar1.Value = 50;
label1.Refresh();
label1.Text = "Second text";
Thread.Sleep(1500); //*This line is only for testing purpose as the change is pretty quick*.
label1.Refresh();