在Form1設計師如何讓3個標籤顯示整體的計數數字?
在label25的LABEL1計數號碼的開出號碼。 在中間,我添加了另一個標籤,僅用於「/」
但我運行該程序時的結果是標籤之間存在很大的空間:我嘗試移動「/」和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是計數器。
爲什麼不使用一個標籤並正確格式化這兩個值和斜槓的單個標籤的文本?請顯示設置這些標籤的實際代碼 – Steve
格式化代碼時請格外小心。 *每個*代碼片段在第一行之後大量縮進,大概是因爲您最初從第一行的* text *開頭複製並粘貼。使用後期預覽來確保代碼看起來像你可以做到的那樣可讀。 –
_label15.Text = string.Format(「{0}/{1}」,mypro.Report2,mypro.Report4); _只需使標籤足夠長並刪除不必要的標籤 – Steve