在C#中将数据写入文本框时显示进度条

Display Progress bar when writing data into textbox in C#

我要在文本框里写很长的文字,屏幕停了很久。 所以我想在将文本写入文本框时显示进度条。对我的代码有什么建议吗?谢谢!

private void btnCheckProcStep_Click(object sender, EventArgs e)
        {
            try
            {
                txtResults.Clear();
                DataTable dtx = new DataTable();             
                foreach (DataGridViewRow row in grdMametanCheckList.Rows)
                {
                    var _MAMETAN_NO = row.Cells[0].Value.ToString();
                    dtx = get_Missing_Proc_Inst_Seq(_MAMETAN_NO);
                    foreach (DataRow dr in dtx.Rows)
                    {
                        txtResults.Text += row.Cells[0].Value.ToString()+ ","+ dr[0].ToString() + Environment.NewLine;
                    }                   
                }   
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }            
        }

您可以计算在完全等于之前需要添加多少次进度(设置为 100 或您自己选择,称为最大值)。我会通过将所需的进度条更改为 100(或最大值)除以数据行中的行数来执行此操作。

ProgBarChange = progBar.Maximum / DataRow.Count()

然后在您的 Foreach 中,我将使用此 ProgBarChange 值增加您的进度条。

progBar.Value += ProgBarChange;

记得像您对 txtResults.Clear() 所做的那样清除 progBar。

progBar.Value = 0;

您应该使用 BackgroundWorker 来异步处理

delegate void SetTextCallback(string text);
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }
    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // Your background task goes here
        for (int i = 0; i <= 100; i++)
        {
            SetText(i.ToString()+" %");
            backgroundWorker1.ReportProgress(i);
            System.Threading.Thread.Sleep(100);
        }
    }
    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("MEssagebox");
    }
    private void button1_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void SetText(string text)
    {
        if (this.textBox1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.textBox1.Text = text;
        }
    }
}

注意 : 在从数据库中检索数据并写入文本框时需要运行进度条继续。

您应该使用 BackgroundWorker 来更新进度。

这是一个客户端功能。如果可能,请尝试以下简单的 java 脚本进度条。

<html>
<head>
    <style>
        #ProgressBar {
            width: 100%;
            background-color: #ddd;
        }
        #Bar {
            width: 1%;
            height: 30px;
            background-color: #4CAF50;
        }
    </style>
    <meta charset="utf-8" />
    <script>
        function showProgress() {
            var element = document.getElementById("Bar");
            var width = 1;
            var id = setInterval(frame, 10);
            function frame() {
                if (width >= 100) {
                    clearInterval(id);
                } else {
                    width++;
                    element.style.width = width / 2 + '%';
                }
            }
        }
    </script>
</head>
<body>
    <div id="ProgressBar">
        <div id="Bar"></div>
    </div>
    <br>
    <input type="text" id="txtResults" onkeypress="showProgress()" />
</body>
</html>