//form类是这样的
public partial class Main : Form
{
    //...
    public Main()
    {
        InitializeComponent();
    }    public void Add(string log)
    {
        textBox1.Text += log + "\r\n";
    }
    //...
}
//另一个类是这样的
public class Watcher
{
    //...
    public Watcher()
    {
        fileSystemWatcher.Path = folderPath;
        fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
        fileSystemWatcher.EnableRaisingEvents = true;
    }    void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
    {
        //想要在这里通过调用Main类中的Add方法修改界面中的控件 如何实现?
    }
    //...
}

解决方案 »

  1.   

    建议不要"在另一个类中 修改form类中的控件"。
    而是form类侦听另一个类的事件变化,改变自己。    public Main()
        {
            InitializeComponent();
            watcher.XXXchanged += (sender, e)
            {
                textbox1.Text = ...;
            }
        }
      

  2.   

    //form类是这样的
    public partial class Main : Form
    {
        //...
        public Main()
        {
            InitializeComponent();
        }    public void Add(string log)
        {
            Watcher w=new Watcher();
            w.main=this; //这里把this传给Main属性
            textBox1.Text += log + "\r\n";
        }
        //...
    }
    //另一个类是这样的
    public class Watcher
    {
        //...
        public Watcher()
        {
            fileSystemWatcher.Path = folderPath;
            fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
            fileSystemWatcher.EnableRaisingEvents = true;
        }    void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
        {
           if(main!=null)
             main.Add();
            //想要在这里通过调用Main类中的Add方法修改界面中的控件 如何实现?
        }
        //...   public Main main{get;set;} //加一个Main属性
    }
      

  3.   

    1 楼2楼都是可行的办法,但是2楼必须 主类和另一个类在一个DLL中,如果 主类的dll引用 另一个类的dll时,就不行了。所以建议
      public Main main{get;set;} //加一个Main属性 改为   public form main{get;set;} //加一个Main属性
    另外:2楼 Watcher w=new Watcher();
            w.main=this; //这里把this传给Main属性 放在add方法中,有些逻辑上不合理,应该改在 Main 方法中.改进的代码如下://form类是这样的
    public partial class Main : Form
    {
        //...
        public Main()
        {
            InitializeComponent();        Watcher w=new Watcher();
            w.main=this; //这里把this传给Main属性
        }    public void Add(string log)
        {
           
            textBox1.Text += log + "\r\n";
        }
        //...
    }
    //另一个类是这样的
    public class Watcher
    {
        //...
        public Watcher()
        {
            fileSystemWatcher.Path = folderPath;
            fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
            fileSystemWatcher.EnableRaisingEvents = true;
        }    void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
        {
           if(main!=null)
             main.Add();
            //想要在这里通过调用Main类中的Add方法修改界面中的控件 如何实现?
        }
        //...   public form main{get;set;} //加一个Main属性
    }
      

  4.   

    不如直接public class Watcher
    {
       Main m_Form;
       public Watcher(Main form)
       {
           m_Form=form;
       }
       void fileSystemWatcher_Created(object sender, FileSystemEventArgs e) 
       {              
         string log="so what!"
         m_Form.Add(log);

    }
    我感觉应该有其他办法更好,不过这个也行吧 呵呵
      

  5.   


    谢谢 不过这样弄完之后 一调用main.Add 程序就直接关闭 没有任何提示是怎么回事?
      

  6.   

    呵呵 最简单的 public state ....