日期:2014-05-19  浏览次数:20907 次

求助:在我自已写的类中,如何使用MDI主窗口中的状态栏里的进度条控件。多谢!
求助:
在我自已写的类中要读出一个大文件,要用很长时间,
所以想用进度条显示读取文件的进度,
但是我不知道如何能在我的类中使用MDI主窗口中的状态栏里的进度条控件。多谢!

------解决方案--------------------
不懂 帮顶
------解决方案--------------------
使用Form.MdiParent 属性可以找到MDI子窗的MDI主窗体.
------解决方案--------------------
最简单的方法,全局窗体,全局控件,
要不用句柄,委托...
------解决方案--------------------
还是比较容易上手的。
使用ProgressBar控件。下面的代码是copy多个文件时显示进度条的代码。
若你读取一个文件要显示进度条,必须把文件分成多块来读,每读完一块,进度条前进相应的步数,楼主自己去了解一下。

代码是单线程的。
private void CopyWithProgress(string[] filenames)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being copied.
pBar1.Step = 1;

// Loop through all files to copy.
for (int x = 1; x <= filenames.Length; x++)
{
// Copy the file and increment the ProgressBar if successful.
if(CopyFile(filenames[x-1]) == true)
{
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
}
}
}


------解决方案--------------------
但是如何在我的读文件的类里使用主窗口中的进度条控件呢?
=========================
把主窗口中驱动进度条的类公布为public的,比如这个CopyWithProgress方法。
然后(form1)(form2.Parent).CopyWithProgress()
------解决方案--------------------
使用接口的方法

1.定义一个简单的接口
public interface IProgressBar
{
ProgressBar ProgressBar {get;}
}

2.你的类处理文件里使用这个接口
public class MyFile
{
public MyFile(IProgressBar progressBar){...}
}

这样任何实现了IProgressBar的类都能传给你的文件处理类

如果你要用主窗口的进度条,主窗口实现接口IProgressBar。

------解决方案--------------------
look study
------解决方案--------------------
接口定义随便在哪都行。

使用的时候把接口所在名字空间引入就可以用了。
------解决方案--------------------
由于工作线程与UI线程不是同一线程,而且你不能用工作线程去更新UI线程的东东。
如果你这样做会导致很多不明错误。
如果你是在.net 2.0基础上开发建议使用BackGroundWorker类。
否则你需要使用Control中可以进行异步编程的方法