窗口传值方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
网络上有几种方法,先总结如下:
调用窗体(父):FormFather,被调用窗体(子):FormSub
 
 
方法1: 所有权法
       //FormFather:
       //需要有一个公共的刷新方法
       public void Refresh_Method()
       {
               //...
       }
       //在调用FormSub时,要把FormSub的所有者设为FormFather
       FormSub f2 = new FormSub() ;
       f2.Owner = this;
       f2.ShowDialog() ;
 
 
       //FormSub:
       //在需要对其调用者(父)刷新时
       FormFather f1 ;
       f1 = (FormFather)this.Owner;
       f1.Refresh_Method() ;
 
  
 
方法2:自身传递法
       //FormFather:
       //需要有一个公共的刷新方法
       public void Refresh_Method()
       {
               //...
       }
       FormSub f2 = new FormSub() ;
       f2.ShowDialog(this) ;
 
 
       //FormSub:
       private FormFather p_f1;
       public FormSub(FormFather f1)
       {
               InitializeComponent();
               p_f1 = f1;
       }
       //刷新时
       p_f1.Refresh_Method() ;
 
  
 
方法3:属性法
       //FormFather:
       //需要有一个公共的刷新方法
       public void Refresh_Method()
       {
               //...
       }
       //调用时
       FormSub f2 = new FormSub() ;
       f2.P_F1 = this; //重点,赋值到子窗体对应属性
       f2.Show() ;
 
       //FormSub:
       private FormFather p_f1;
       public FormFather P_F1
       {
               get{return p_f1;}
               set{p_f1 = value;}
       }
       //刷新时
       p_f1.Refresh_Method() ;
 
 
方法4:委托法
       //FormFather:
       //需要有一个公共的刷新方法
       public void Refresh_Method()
       {
               //...
       }
       //调用时
       FormSub f2 = new FormSub() ;
       f2.ShowUpdate += new DisplayUpdate(Refresh_Method) ;
       f2.Show() ;
 
 
       //FormSub:
       //声明一个委托
       public delegate void DisplayUpdate();
       //声明事件
       public event DisplayUpdate ShowUpdate;
       //刷新时,放在需要执行刷新的事件里
 
       if(ShowUpdate!=null)   ShowUpdate();
 
  
 
       //子窗体提交后
       private void btnOK_Click(object sender, EventArgs e)
       {
               this.DialogResult = DialogResult.OK;
               this.Close();
       }
 
 
  
      //判断子窗体
       if(form.ShowDialog() == DialogResult.OK)
       {
                刷新父窗体中的DataGRIDVIEW数据
       }

  

posted @   不买南孚电池  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示