黑马程序员_WinForm窗体传值示例

环境:现在有两个窗体,一个父窗体,一个子窗体,如下图示。

目的:子窗体显示出来以后,点击父窗体的修改按钮可以修改子窗体的标题,点击子窗体的修改按钮可以修改父窗体的标题。

黑马程序员_WinForm窗体传值示例黑马程序员_WinForm窗体传值示例

黑马程序员_WinForm窗体传值示例
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace 窗体传值
11 {
12     public partial class MainForm : Form
13     {
14         public MainForm()
15         {
16             InitializeComponent();
17             isEnabled(true);
18         }
19         Form sf;
20         private void btnSubShow_Click(object sender, EventArgs e)
21         {
22             sf = new SubForm(this);
23             sf.Show();
24             isEnabled(false);            
25         }
26         private void btnSubClose_Click(object sender, EventArgs e)
27         {
28             sf.Close();
29             isEnabled(true);
30         }
31         private void btnSubChange_Click(object sender, EventArgs e)
32         {
33             sf.Text = txtSubTitle.Text.Trim();
34         }        
35         public void isEnabled(bool isEnabled)
36         {
37             btnSubChange.Enabled = !isEnabled;
38             btnSubClose.Enabled = !isEnabled;
39             btnSubShow.Enabled = isEnabled;
40         }
41     }
42 }
Form1.cs
黑马程序员_WinForm窗体传值示例
  1 namespace 窗体传值
  2 {
  3     partial class MainForm
  4     {
  5         /// <summary>
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary>
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region Windows 窗体设计器生成的代码
 24 
 25         /// <summary>
 26         /// 设计器支持所需的方法 - 不要
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.btnSubShow = new System.Windows.Forms.Button();
 32             this.btnSubClose = new System.Windows.Forms.Button();
 33             this.btnSubChange = new System.Windows.Forms.Button();
 34             this.txtSubTitle = new System.Windows.Forms.TextBox();
 35             this.lbSubTitle = new System.Windows.Forms.Label();
 36             this.SuspendLayout();
 37             // 
 38             // btnSubShow
 39             // 
 40             this.btnSubShow.Location = new System.Drawing.Point(39, 30);
 41             this.btnSubShow.Name = "btnSubShow";
 42             this.btnSubShow.Size = new System.Drawing.Size(75, 23);
 43             this.btnSubShow.TabIndex = 0;
 44             this.btnSubShow.Text = "显示子窗体";
 45             this.btnSubShow.UseVisualStyleBackColor = true;
 46             this.btnSubShow.Click += new System.EventHandler(this.btnSubShow_Click);
 47             // 
 48             // btnSubClose
 49             // 
 50             this.btnSubClose.Location = new System.Drawing.Point(39, 79);
 51             this.btnSubClose.Name = "btnSubClose";
 52             this.btnSubClose.Size = new System.Drawing.Size(75, 23);
 53             this.btnSubClose.TabIndex = 1;
 54             this.btnSubClose.Text = "关闭子窗体";
 55             this.btnSubClose.UseVisualStyleBackColor = true;
 56             this.btnSubClose.Click += new System.EventHandler(this.btnSubClose_Click);
 57             // 
 58             // btnSubChange
 59             // 
 60             this.btnSubChange.Location = new System.Drawing.Point(205, 79);
 61             this.btnSubChange.Name = "btnSubChange";
 62             this.btnSubChange.Size = new System.Drawing.Size(75, 23);
 63             this.btnSubChange.TabIndex = 2;
 64             this.btnSubChange.Text = "修改";
 65             this.btnSubChange.UseVisualStyleBackColor = true;
 66             this.btnSubChange.Click += new System.EventHandler(this.btnSubChange_Click);
 67             // 
 68             // txtSubTitle
 69             // 
 70             this.txtSubTitle.Location = new System.Drawing.Point(211, 33);
 71             this.txtSubTitle.Name = "txtSubTitle";
 72             this.txtSubTitle.Size = new System.Drawing.Size(100, 21);
 73             this.txtSubTitle.TabIndex = 3;
 74             // 
 75             // lbSubTitle
 76             // 
 77             this.lbSubTitle.AutoSize = true;
 78             this.lbSubTitle.Location = new System.Drawing.Point(131, 38);
 79             this.lbSubTitle.Name = "lbSubTitle";
 80             this.lbSubTitle.Size = new System.Drawing.Size(77, 12);
 81             this.lbSubTitle.TabIndex = 4;
 82             this.lbSubTitle.Text = "子窗体标题:";
 83             // 
 84             // MainForm
 85             // 
 86             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
 87             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
 88             this.ClientSize = new System.Drawing.Size(363, 150);
 89             this.Controls.Add(this.lbSubTitle);
 90             this.Controls.Add(this.txtSubTitle);
 91             this.Controls.Add(this.btnSubChange);
 92             this.Controls.Add(this.btnSubClose);
 93             this.Controls.Add(this.btnSubShow);
 94             this.Name = "MainForm";
 95             this.Text = "MainForm";
 96             this.ResumeLayout(false);
 97             this.PerformLayout();
 98 
 99         }
100 
101         #endregion
102 
103         private System.Windows.Forms.Button btnSubShow;
104         private System.Windows.Forms.Button btnSubClose;
105         private System.Windows.Forms.Button btnSubChange;
106         private System.Windows.Forms.TextBox txtSubTitle;
107         private System.Windows.Forms.Label lbSubTitle;
108     }
109 }
Form1.Designer.cs
黑马程序员_WinForm窗体传值示例
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace 窗体传值
11 {
12     public partial class SubForm : Form
13     {
14         Form mainForm;
15         public SubForm()
16         {
17             InitializeComponent();
18         }
19         public SubForm(Form m):this()
20         {
21             this.mainForm = m;
22         }
23         private void btnMainChange_Click(object sender, EventArgs e)
24         {
25             mainForm.Text = txtMainTitle.Text.Trim();
26         }
27     }
28 }
SubForm.cs
黑马程序员_WinForm窗体传值示例
 1 namespace 窗体传值
 2 {
 3     partial class SubForm
 4     {
 5         /// <summary>
 6         /// Required designer variable.
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary>
11         /// Clean up any resources being used.
12         /// </summary>
13         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region Windows Form Designer generated code
24 
25         /// <summary>
26         /// Required method for Designer support - do not modify
27         /// the contents of this method with the code editor.
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.lbMainTitle = new System.Windows.Forms.Label();
32             this.txtMainTitle = new System.Windows.Forms.TextBox();
33             this.btnMainChange = new System.Windows.Forms.Button();
34             this.SuspendLayout();
35             // 
36             // lbMainTitle
37             // 
38             this.lbMainTitle.AutoSize = true;
39             this.lbMainTitle.Location = new System.Drawing.Point(65, 47);
40             this.lbMainTitle.Name = "lbMainTitle";
41             this.lbMainTitle.Size = new System.Drawing.Size(77, 12);
42             this.lbMainTitle.TabIndex = 9;
43             this.lbMainTitle.Text = "主窗体标题:";
44             // 
45             // txtMainTitle
46             // 
47             this.txtMainTitle.Location = new System.Drawing.Point(145, 42);
48             this.txtMainTitle.Name = "txtMainTitle";
49             this.txtMainTitle.Size = new System.Drawing.Size(152, 21);
50             this.txtMainTitle.TabIndex = 8;
51             // 
52             // btnMainChange
53             // 
54             this.btnMainChange.Location = new System.Drawing.Point(144, 89);
55             this.btnMainChange.Name = "btnMainChange";
56             this.btnMainChange.Size = new System.Drawing.Size(75, 23);
57             this.btnMainChange.TabIndex = 7;
58             this.btnMainChange.Text = "修改";
59             this.btnMainChange.UseVisualStyleBackColor = true;
60             this.btnMainChange.Click += new System.EventHandler(this.btnMainChange_Click);
61             // 
62             // SubForm
63             // 
64             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
65             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
66             this.ClientSize = new System.Drawing.Size(363, 150);
67             this.Controls.Add(this.lbMainTitle);
68             this.Controls.Add(this.txtMainTitle);
69             this.Controls.Add(this.btnMainChange);
70             this.Name = "SubForm";
71             this.Text = "SubForm";
72             this.ResumeLayout(false);
73             this.PerformLayout();
74 
75         }
76 
77         #endregion
78 
79         private System.Windows.Forms.Label lbMainTitle;
80         private System.Windows.Forms.TextBox txtMainTitle;
81         private System.Windows.Forms.Button btnMainChange;
82     }
83 }
SubForm.Designer.cs

主窗口想要修改子窗口标题很简单,因为在主窗体中中已经创建了一个子窗体的对象sf,并且已经实例化

黑马程序员_WinForm窗体传值示例
1         Form sf;
2         private void btnSubShow_Click(object sender, EventArgs e)
3         {
4             sf = new SubForm(this);
5             sf.Show();
6             isEnabled(false);            
7         }
黑马程序员_WinForm窗体传值示例

点击按钮后将txtSubTitle.Text.Trim()直接传给子窗体的Text属性,也就是sf.Text

1         private void btnSubChange_Click(object sender, EventArgs e)
2         {
3             sf.Text = txtSubTitle.Text.Trim();
4         }

但是,子窗体怎么修改主窗体的标题呢?主窗体为什么能修改子窗体的标题,因为在主窗体中已经定义了子窗体的引用,所以可以通过引用直接将标题传给子窗体。子窗体想要修改主窗体的标题,也必须获得父窗体的引用。

不管是父窗体,还是子窗体,都是以类的形式存在的,类和类之间的传值,就要用到构造方法,在将子窗体实例化的时候,将主窗体的引用通过构造方法传进去。首先要在子窗体中定义一个构造方法。构造方法中有一个参数为主窗体的类型Form,用来接收传进去的主窗体。

1         public SubForm(Form m):this()
2         {
3             this.mainForm = m;
4         }

子窗体可以接收了,父窗体怎么传呢?就是在实例化子窗体引用的时候,通过构造方法,将自己传进去。

1         private void btnSubShow_Click(object sender, EventArgs e)
2         {
3             sf = new SubForm(this);
4             sf.Show();
5             isEnabled(false);            
6         }

这里的sf=new SubForm(this);就是将自己this,做为参数传进构造方法中。这时子窗体中就有了父窗体的引用mainForm,就可以更改父窗体的属性了。

1         private void btnMainChange_Click(object sender, EventArgs e)
2         {
3             mainForm.Text = txtMainTitle.Text.Trim();
4         }

疑问:这个问题解决了,但是,这个程序存在一个问题,子窗体点击关闭按钮,就是那个红色的X后,子窗体关闭了,但是父窗体的“关闭子窗体”和“修改”按钮,依然可以点击,也就是没有执行父窗口中的isEnabled()方法,具体应该怎么才能执行到,目前我还解决不了,等有时间的时候,我会解决它的!

黑马程序员_WinForm窗体传值示例,布布扣,bubuko.com

黑马程序员_WinForm窗体传值示例

上一篇:CryptAPI 数字签名 与 Openssl 验证签名


下一篇:Photoshop为外景美女加上唯美的中性红褐色