如何将进度栏链接到其他功能?

时间:2019-03-27 12:25:16

标签: c# progress-bar

我希望我的进度条在用户按下“验证”按钮时开始运行。但是,当我单击进度条时,只能使其移动。当我按下“验证”按钮时,如何链接进度栏以启动。

    private void ValidateButton_Click(object sender, EventArgs e)
    {           

        int counter = 0;
        string line;

        // Read the file and display it line by line.  
        System.IO.StreamReader file =
        new System.IO.StreamReader(FileNameBox.Text);
        while ((line = file.ReadLine()) != null)
        {
            System.Console.WriteLine(line);
            counter++;
        }

        file.Close();
        System.Console.WriteLine("There were {0} records.", counter);
        // Suspend the screen.  
        System.Console.ReadLine();
    }


    private void ProgressBar_Click()
    {
        // Display the ProgressBar control.
        ProgressBar.Visible = true;
        // Set Minimum to 1 to represent the first file being copied.
        ProgressBar.Minimum = 1;
        // Set Maximum to the total number of files to copy.
        ProgressBar.Maximum = FileNameBox.TextLength;
        // Set the initial value of the ProgressBar.
        ProgressBar.Value = 1;
        // Set the Step property to a value of 1 to represent each file being copied.
        ProgressBar.Step = 1;
        // Loop through all files to copy.
        for (int x = 1; x <= FileNameBox.TextLength; x++) ;

    }
}

FileNameBox是用户选择的文件路径。这按预期工作。

1 个答案:

答案 0 :(得分:1)

我猜您正在使用winform,并且您想显示实时进度。

我认为您可以在单击Validate Button时初始化progressBar,并通过计时器更新ProgressBar.Value。

顺便说一句,我不明白'ProgressBar.Maximum = FileNameBox.TextLength;'意思是,TextLength表示文本长度,文件路径文本长度,不是吗?