簡體   English   中英

標簽控件未顯示在for循環C#中

[英]Label control does not display in for loop c#

我是編程界的新手,請保持柔和,我的問題是,為什么在每個循環中調用標簽控件時,標簽控件都不一一顯示文件名。 我的任務是讀取包含更多文件的文件夾,我需要閱讀該文件名並顯示在標簽控件中,但對我而言效果不佳,對所有人來說都很簡單,但我是初學者為什么不知道查找如何找出錯誤?

請找到代碼。

namespace ImageScanning
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(21, 79);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(81, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "File scanning-->";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(27, 144);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
    }
}

按鈕單擊事件為:

private void button1_Click(object sender, EventArgs e)
{
     string imageloc = @"D:\Image";
     string[] files = Directory.GetFiles(imageloc);
     foreach (string file in files)
     {
          System.Threading.Thread.Sleep(1000);
          label1.Text = "File Scanning--> " + file;
          System.Threading.Thread.Sleep(3000);
          label1.Text = "";
     } 
}

您需要使用線程,在運行循環迭代時,UI線程忙,並且UI部件在反映更改標簽和其他UI控件之前正在等待事件完成,因此您需要啟動另一個線程進行循環迭代,然后UI線程將成為免費更新用戶界面。

嘗試這個

private void button1_Click(object sender, EventArgs e)
{
    Task.Factory.StartNew(() =>
    {
         string imageloc = @"D:\Image";
         string[] files = Directory.GetFiles(imageloc);
         foreach (string file in files)
         {
             System.Threading.Thread.Sleep(1000);
             // Any UI control which you want to use within thread,
             // you need Invoke using UI thread. I have declare a method below

             ExecuteSecure(() => label1.Text = "File Scanning--> " + file);
             System.Threading.Thread.Sleep(3000);
             ExecuteSecure(() => label1.Text = "");    
         }          
    });        
}    

//---
private void ExecuteSecure(Action action)
{
    if (InvokeRequired)
    {
        Invoke(new MethodInvoker(() => action()));
    }
    else
    {
        action();
    }
}

這是使用后台工作進程的示例代碼

BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
    InitializeComponent();

    bw.WorkerReportsProgress = true;
    bw.DoWork += bw_DoWork;
    bw.ProgressChanged += bw_ProgressChanged;
}

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   label1.Text = "File Scanning --> " + e.UserState as string;     
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    string imageloc = @"D:\Image";
    string[] files = Directory.GetFiles(imageloc);
    foreach (var item in files)
    {
       bw.ReportProgress(0, item);
       Thread.Sleep(1000);
    }
 }

protected void button1_Click(object sender, EventArgs e)
{
   if (!bw.IsBusy)
   {
       bw.RunWorkerAsync();
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM