在form1设计师中:
在label1中,使用label25中的数字计算数字。 在中间我添加了另一个标签,仅用于" /"
但是我运行程序的结果是标签之间有一个很大的空间:我试图移动" /"和label25一样多,我可以在靠近label1的左边,但这不是一个好方法。
实现它的最佳方法是什么?
在form1中我有这个课程:
public class MyProgress
{
public string Report1 { get; set; }
public string Report2 { get; set; }
public string Report3 { get; set; }
public string Report4 { get; set; }
}
然后是方法DirSearch,我也在其中报告backgroundworker1的进度:
int numberofdirs = 0;
void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
{
List<string> resultsoftextfound = new List<string>();
List<string> resultsoftextfound1 = new List<string>();
List<string> filePathList = new List<string>();
int numberoffiles = 0;
try
{
filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null,worker,e).ToList();
}
catch (Exception err)
{
string ad = err.ToString();
}
label21.Invoke((MethodInvoker)delegate
{
label21.Text = "Phase 2: Searching in files";
});
MyProgress myp = new MyProgress();
myp.Report4 = filePathList.Count.ToString();
foreach (string file in filePathList)
{
try
{
_busy.WaitOne();
if (worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
bool reportedFile = false;
for (int i = 0; i < textToSearch.Length; i++)
{
if (File.ReadAllText(file).IndexOf(textToSearch[i], StringComparison.InvariantCultureIgnoreCase) >= 0)
{
resultsoftextfound.Add(file + " " + textToSearch[i]);
if (!reportedFile)
{
numberoffiles++;
myp.Report1 = file;
myp.Report2 = numberoffiles.ToString();
myp.Report3 = textToSearch[i];
backgroundWorker1.ReportProgress(0, myp);
reportedFile = true;
}
}
}
numberofdirs++;
label1.Invoke((MethodInvoker)delegate
{
label1.Text = numberofdirs.ToString();
label1.Visible = true;
});
}
catch (Exception)
{
}
}
}
在这一行中,我报告了整体文件数:
myp.Report4 = filePathList.Count.ToString();
然后报告文件数量:
myp.Report2 = numberoffiles.ToString();
更新label1:
label1.Invoke((MethodInvoker)delegate
{
label1.Text = numberofdirs.ToString();
label1.Visible = true;
});
这是backgorundworker1 dowork事件:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
_stopwatch.Restart();
string[] values = textBox1.Text.Split(new string[] { ",," }, StringSplitOptions.None);
DirSearch(textBox3.Text, textBox2.Text, values, worker, e);
_stopwatch.Stop();
}
progresschanged事件:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
MyProgress mypro = (MyProgress)e.UserState;
ListViewCostumControl.lvnf.Items.Add(mypro.Report1);
label15.Text = mypro.Report2;
label25.Text = mypro.Report4;
label15.Visible = true;
if (ListViewCostumControl.lvnf.Items.Count > 9)
textBox4.Enabled = true;
}
最后,我在运行程序时想要做的是显示整个文件中的文件数量,例如:21/244,其中21是计数器。
答案 0 :(得分:1)
尝试更改标签控件中的文本对齐方式。将最左侧的标签与右侧和最右侧的标签对齐。将“/”字符标签与“中心”对齐,使其非常窄。然后使用水平对齐工具调整all以使文本基线相同。
答案 1 :(得分:1)
如果要使用三种不同的标签,请执行以下步骤:
P.S。我建议你只使用一个标签并将其作为格式化文本放在那里。