//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方法修改界面中的控件 如何实现?
}
//...
}
解决方案 »
- 为什么要用属性(get,set),利用类中的公有字段不是照样可以实现这两个操作吗
- vs2008 新建SQL SERVER 与sql server 文件(*.mdf)的连接要求安装 SQL SERVER....
- 请教用过DevExpress的朋友,如何在gridcontl上得到前当单元格的坐标,并且在点击此单元格时,在单元格下方弹出自己的窗体?
- 控制richtextbox中的图片大小
- 如果想做游戏,需要学什么语言呀
- 一个简单的问题
- 自定义控件的问题
- 【推荐】Infragistics NetAdvantage 2004 Vol.2 COM/.NET/ASP.NET
- 如何把HTML文件作为嵌入资源,在运行的时候显示?
- 请教高手和专家:在窗体的 TitleBar 上单击(MouseDown也可),我要在程序中响应这次单击(MouseDown),要怎样做???
- 帮忙解释下这个类
- 求教FtpWebRequest问题
而是form类侦听另一个类的事件变化,改变自己。 public Main()
{
InitializeComponent();
watcher.XXXchanged += (sender, e)
{
textbox1.Text = ...;
}
}
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属性
}
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属性
}
{
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);
}
}
我感觉应该有其他办法更好,不过这个也行吧 呵呵
谢谢 不过这样弄完之后 一调用main.Add 程序就直接关闭 没有任何提示是怎么回事?