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

解决方案 »

  1.   

    使用Form.MdiParent 属性可以找到MDI子窗的MDI主窗体.
      

  2.   

    还是比较容易上手的。
    使用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();
            }
        }
    }
      

  3.   

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

  4.   


    再问一下:
    在unsafe{.....}能使用(form1)(form2.Parent).CopyWithProgress()
    吗?
      

  5.   

    使用接口的方法1.定义一个简单的接口
     public interface IProgressBar
    {
        ProgressBar ProgressBar {get;}
    }2.你的类处理文件里使用这个接口
    public class MyFile
    {
      public MyFile(IProgressBar progressBar){...}
    }这样任何实现了IProgressBar的类都能传给你的文件处理类如果你要用主窗口的进度条,主窗口实现接口IProgressBar。
      

  6.   

    接口定义在哪里呢?是MainForm中吗?
    我是MainForm------>调用MdiForm----->使用自己的读文件的类。
      

  7.   

    多谢xvting指点,但我是C#新手,还是不明白接口如何使用,再次求教。1.我在MainForm中用如下方法打开MDI窗口,并且我的MainFormr的statusStrip1中有一个toolStripProgressBar1
    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
      MdiForm myMdiChild = new MdiForm();
      MdiChild.Show();
    }2.在MdiForm中的Load中打开文件
    private void Form2_Load(object sender, EventArgs e)
    {
      ReadF.RDfile("c:\\text.dat");
    }
    3.以下是读文件的类namespace myPRJ
    {
        public class ReadF
        {
            public static RDfile(string sFileName)
            {   
                FileStream fstream = new FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.None);
                byte[] bBuffer = new byte[5800]; 
                for(int i=0;i<10000;i++)
                {           
                  fstream.Read(bBuffer, 0, 5800);
                  ....处理数据
                  》》》》》》》》我想在这个地方使用MainForm中的toolStripProgressBar1
                }
                fstream.Close();
             }
         }
    }
      

  8.   

    由于工作线程与UI线程不是同一线程,而且你不能用工作线程去更新UI线程的东东。
    如果你这样做会导致很多不明错误。
    如果你是在.net 2.0基础上开发建议使用BackGroundWorker类。
    否则你需要使用Control中可以进行异步编程的方法
      

  9.   

    用事件委托,也可以把
    用过用多线程的话,用invoke调用