zoukankan      html  css  js  c++  java
  • c# 主窗体更新子窗体 进程间通信

    1.窗体间数据传输

    主窗体连续不断更新给子窗体  本文章来源于网络 年代久远 如有侵犯 请联系删除

    1.通过

    在windows form之间传值,我总结了有四个方法:全局变量、属性、窗体构造函数和delegate。

    第一个全局变量:

    这个最简单,只要把变量描述成static就可以了,在form2中直接引用form1的变量,代码如下:

    在form1中定义一个static变量public static int i= 9 ;

      1 Form2中的钮扣按钮如下:
      2 
      3 private void button1_Click(object sender, System.EventArgs e)
      4 
      5 {
      6 
      7     textBox1.Text = Form1.i.ToString();
      8 
      9 }
     10 

    第二个方法:

    这也许是个老话题了,但是今天我在用property 时却有了新的发现。那就是如果传递的是类的话,那么是按地址(引用)来传递的。例如如下两个程序。假设有两个form,form1、form2和一个Class1.cs的类文件。form1是程序的开始窗体,通过form1 来调用form2 。程序如下:

      1 Class1.cs文件的内容是
      2 
      3 public class Class1
      4 
      5 {
      6 
      7    public int i;
      8 
      9    public Class1()
     10 
     11    {
     12 
     13     //
     14 
     15     // TODO:
     16 
     17     i = 9;
     18 
     19    }
     20 
     21    public void modify( int u )
     22 
     23    {
     24 
     25     i = u;
     26 
     27    }
     28 
     29 }
     30 
      1 Form1中的内容是
      2 
      3 private Class1 ttt;
      4 
      5 private void Form1_Load(object sender, System.EventArgs e)
      6 
      7    {
      8 
      9     ttt = new Class1();
     10 
     11     Form2 temp = new Form2();
     12 
     13     temp.Change = ttt;
     14 
     15     temp.Show();
     16 
     17    }
     18 
     19    private void button1_Click(object sender, System.EventArgs e)
     20 
     21    {
     22 
     23     textBox1.Text = ttt.i.ToString();
     24 
     25    }
     26 
     27    private void button2_Click(object sender, System.EventArgs e)
     28 
     29    {
     30 
     31     ttt.modify( 44);
     32 
     33    }
     34 
      1 form2中的内容是:
      2 
      3 private Class1 change;
      4 
      5    public Class1 Change
      6 
      7    {
      8 
      9     get { return change ;}
     10 
     11     set
     12 
     13     {
     14 
     15      change= value;
     16 
     17     }
     18 
     19    }
     20 
     21 private void button1_Click(object sender, System.EventArgs e)
     22 
     23    {
     24 
     25     textBox1.Text = change.i.ToString();
     26 
     27    }
     28 
     29    private void button2_Click(object sender, System.EventArgs e)
     30 
     31    {
     32 
     33     change.modify( 98 );
     34 
     35    }
     36 

    运行程序你会发现,改form1 中的textbox值,那么form2 中change中的i 的值也会相应的变,同样form2中change的 i 值变了,那么form1 中的 ttt 中的 i 也相应的变了。就好像两个form在使用同一个数据变量一样,为什么呢?

    经过思考,其实很简单,原因就在于我们在使用property传递数据时使用了同一块内存空间。

    在传递class类型的数据时(如上),由于我们没有new一个新的实例,而是直接赋值,所以就相当于使用了引用,把上面的赋值过程改成下面这样,

    private Class1 change;

      1    public Class1 Change
      2 
      3    {
      4 
      5     get { return change ;}
      6 
      7     set
      8 
      9     {
     10 
     11      change = new Class1();
     12 
     13     change.i = value.i ;
     14 
     15     }
     16 
     17    }
     18 

    那么两个form中的值相互之间就不再有什么关联了,也就是说,改其中一个,不会影响另外一个。这也给了我们一个启示,当我们想在form之间传值时,而且还想让值之间有一定的联系,那么就把这些值用class来包起来,再传。这样又清楚又省事。

    如果你传的不是类而是一般数据(int , string )等,那么这些数据在form之间是没有联系的,因为C#在定义这些数据类型时,就默认new了他们,例如:int i ; 和int i = new int() ; 是一样的,所以我以前经常在form之间传递简单的变量时,没有发现数据之间的关系,直到今天传 class 时才发现。像下面这样的 class :

      1 class temp
      2 
      3 {
      4 
      5       int i ;
      6 
      7       int[]   mm;
      8 
      9        public temp ()
     10 
     11       { mm = new int [10] ; }
     12 
     13 }
     14 

    在form之间传递时,变量 i 是两个窗体共用的,但是mm 却不是,其原因就是我上面讲的那样,所以利用property的这个特性,我们可以灵活的在form之间传递我们想变和不想变的值。

    第三个方法是用构造函数:

      1 Form1 的button按钮这样写:
      2 
      3 private void button1_Click(object sender, System.EventArgs e)
      4 
      5 {
      6 
      7     Form2 temp = new Form2( 9 );
      8 
      9     temp.Show();
     10 
     11 }
     12 
     13 Form2 的构造函数这样写:
     14 
     15 public Form2( int i )
     16 
     17 {
     18 
     19     InitializeComponent();
     20 
     21     textBox1.Text = i.ToString();
     22 
     23 }
     24 

    第四个方法是用delegate,代码如下:

      1 Form2中先定义一个delegate
      2 
      3 public delegate void returnvalue( int i );
      4 
      5 public returnvalue ReturnValue;
      6 
      7 form2 中的button按钮代码如下:
      8 
      9 private void button1_Click(object sender, System.EventArgs e)
     10 
     11 {
     12 
     13     if ( ReturnValue != null )
     14 
     15         ReturnValue( 8 );
     16 
     17 }
     18 
     19 Form1中的button按键如下:
     20 
     21 private void button1_Click(object sender, System.EventArgs e)
     22 
     23 {
     24 
     25     Form2 temp = new Form2( );
     26 
     27     temp.ReturnValue = new temp.Form2.returnvalue( showvalue );
     28 
     29     temp.Show();
     30 
     31 }
     32 
     33 private void showvalue( int i )
     34 
     35 {
     36 
     37     textBox1.Text = i.ToString();
     38 
     39 }
     40 
  • 相关阅读:
    LeetCode(111) Minimum Depth of Binary Tree
    LeetCode(108) Convert Sorted Array to Binary Search Tree
    LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode(99) Recover Binary Search Tree
    【Android】通过经纬度查询城市信息
    【Android】自定义View
    【OpenStack Cinder】Cinder安装时遇到的一些坑
    【积淀】半夜突然有点想法
    【Android】 HttpClient 发送REST请求
  • 原文地址:https://www.cnblogs.com/polar-lights/p/8457802.html
Copyright © 2011-2022 走看看