码迷,mamicode.com
首页 > Windows程序 > 详细

C#自学之路31

时间:2015-04-14 20:13:59      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:private   public   

31.窗口之间的交互

  窗口之间的交互主要有3中方式:

a.属性。弹出的窗口通过读写属性将数据传递到父窗口,接受父窗口的数据。

b.方法。弹出的窗口通过构造函数或方法将数据传递到父窗口,接受父窗口的数据。

c.事件。弹出的窗口通过事件的方式通知父窗口有数据需要进行交互。



通过属性现实窗口间的数据交互。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace WindowsFormsApplication17

{

    public partial class Form2 : Form

    {

        private string _strName;


        public string strName

        {

            get

            {

                return this._strName;

            }

            set

            {

                this._strName = value;

            }

        }

        public Form2()

        {

            InitializeComponent();

        }


        private void Form2_Load(object sender, EventArgs e)

        {

            this.label1.Text = "欢迎你" + this.strName + "同学!";

        }

    }

}



技术分享



通过窗口的构造函数实现窗口之间的相互交互。


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace WindowsFormsApplication18

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            string strName = this.textBox2.Text;

            Form2 form = new Form2( strName );

            form.Show();

        }


        private void button2_Click(object sender, EventArgs e)

        {

            this.Close();

        }

    }

}


与上面的效果一样。

本文出自 “郭俊的博客” 博客,转载请与作者联系!

C#自学之路31

标签:private   public   

原文地址:http://10093949.blog.51cto.com/10083949/1632155

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!