事件触发???方法触发???

2000lhzh 2004-10-18 01:44:44
在winform 下,有两个窗体A,B
A中button的click事件中,
private void button_Click(*****)
{
******

savaeData();
//执行完后 要求刷新B 窗口 如B 窗口的初始化
*******
}

private void savaeData()
{
*****
}


执行完A 窗口的button后,要求能够刷新 B窗口
这类问题应该属于:事件触发
在B窗口中不能够触发A窗口的 button 事件,(因为button中的不用全部执行。只是执行saveData方法而已。)
如何在A窗口的saveData方法中来触发B窗口的初始化???
...全文
170 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
2000lhzh 2004-10-18
  • 打赏
  • 举报
回复
请去
http://community.csdn.net/Expert/topic/3466/3466727.xml?temp=.405697
接分。。。
brightheroes 2004-10-18
  • 打赏
  • 举报
回复
哦,你说的是这个地方的this啊
对,这个地方的this,就是当前的form1
2000lhzh 2004-10-18
  • 打赏
  • 举报
回复
Form2 f = new Form2(this);
是以上中的this,this一定指当前的Form1吗???什么时候用this
brightheroes 2004-10-18
  • 打赏
  • 举报
回复
关于第二个问题
this.f
是指上面的那个private form1 f

Form2的构造函数而已
2000lhzh 2004-10-18
  • 打赏
  • 举报
回复

关于this 的问题。盼解。。
2000lhzh 2004-10-18
  • 打赏
  • 举报
回复
多谢 brightheroes(闭关|那一剑的风情)
真是高手,还有两个问题不是很懂,需要接着请教。
1) Form1 f1=new Form1();
这样为什么不行、同构造函数中传递Form1 有什么区别?
2)通过构造函数传递Form1时,this 。。。不是很懂。this指当前的,这个表示当前的窗体Form1吗?
brightheroes 2004-10-18
  • 打赏
  • 举报
回复
你Form1 f1 = new Form1之后,f1是一个新的对象,而不是你运行着的那个窗体,所以显然不行
brightheroes 2004-10-18
  • 打赏
  • 举报
回复
Form1 f1=new Form1();
是不对的
你在这里不能New
那样就没有意义了

比如说你的A中某一个按钮来弹出B

那么应该这样

Form2 f = new Form2(this);



Form2的构造函如下:
private Form1 f;
public Form2 Form2(Form1 f)
{
this.f = f;
}

然后把你的那个Form1 f1 =new form1();去掉就可以了
2000lhzh 2004-10-18
  • 打赏
  • 举报
回复
我的代码如下。。为什么 不行??

Form1 窗口中。

public void xx(object sender,System.EventArgs e)
{
SqlConnection con=new SqlConnection("server=localhost;uid=sa;pwd= ;database=test");
con.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from tab1",con);
DataSet ds=new DataSet();
da.Fill(ds,"test");
this.dataGrid1.DataSource=ds.Tables[0].DefaultView;
}


Form2窗口中
private void InitializeComponent()
{
×××××
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 168);
this.button1.Name = "button1";
this.button1.TabIndex = 4;
this.button1.Text = "Login";
this.button1.Click += new System.EventHandler(this.button1_Click);

Form1 f1=new Form1();
this.button1.Click += new System.EventHandler(f1.xx);

×××××
}

通过Form2中的button1 click事件来刷新Form1中的DataGrid为什么没有刷新???
xinshaw 2004-10-18
  • 打赏
  • 举报
回复
这种问题好像是典型的Observer模式的场景。
你上网找找Observer模式,可以找到解决方法。
2000lhzh 2004-10-18
  • 打赏
  • 举报
回复
我试一下。。谢谢先。
brightheroes 2004-10-18
  • 打赏
  • 举报
回复
比如说,在B里面写一个方法
public void xxx(ojbect sender,EventArgs e)
{
...............
}

然后把这个方法注册到A的ButtonClick上

A中
b是B的一个实例
this.Button1.Click += System.EventHandler(this.Button1_Click);
this.Button1.Click += System.EventHandler(b.xxx);


2000lhzh 2004-10-18
  • 打赏
  • 举报
回复
我如果想用事件触发的方法来解决的话。如何实现???
brightheroes 2004-10-18
  • 打赏
  • 举报
回复
可以考虑在A的Button里面调用B的某个方法

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧