zoukankan      html  css  js  c++  java
  • this控件传递引发的引用思考

    控件
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace testconroler
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 控件标志设定及this传参数
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                if (checkBox1.Text == "checkBox1")
                {
                    checkBox1.CheckState = CheckState.Checked;
                    checkBox2.CheckState = CheckState.Checked;
                }
                Form2 form = new Form2(this);
                form.ShowDialog();
            }
        }
    }
    Form1
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace testconroler
    {
        public partial class Form1 : Form
        {
            /// <summary>
            /// Form1中含有上面的控件
            /// </summary>
            public Form1()
            {
                InitializeComponent();
            }
            private void userControl11_Load(object sender, EventArgs e)
            {
            }
    
        }
    }
    Form2
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace testconroler
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            /// <summary>
            /// Form2想从Form1种接收一个和Form1中完全一样的控件
            /// 包括点选状态   关于过滤的测试
            /// </summary>
            private Control form = null;
            public Form2(Control  formtest)
            {
                InitializeComponent();
                form = formtest;
            }
            private void Form2_Load(object sender, EventArgs e)
            {
                this.Controls.Add(form);
            }
        }
    }

    Control控件

    Form1窗体

    Form2

    描述:

    Form2中写了一个窗体的构造函数,该构造函数可以接收一个控件,用于接收控件中this控件。

    private Control form = null;
            public Form2(Control  formtest)
            {
                InitializeComponent();
                form = formtest;
            }

    在控件的button中有这样一段代码,代码中if判断只是为了设定证明From2种接收的控件正是控件中this引用传递过来的,如果Form2中的add的控件点选状态和控件本身的点选状态一样,则证明Form2加载了本来属于Form1的控件。

          private void button1_Click(object sender, EventArgs e)
            {
                if (checkBox1.Text == "checkBox1")
                {
                    checkBox1.CheckState = CheckState.Checked;
                    checkBox2.CheckState = CheckState.Checked;
                }
                Form2 form = new Form2(this);
                form.ShowDialog();
            }

     Form2中在load时就加载控件。

            private void Form2_Load(object sender, EventArgs e)
            {
                this.Controls.Add(form);
            }

    点击Form1的控件按钮,Form1的控件传递到了Form2中,即窗体Form1变得空白。从这我们可以看出引用this传递,this本身只有一个,在该例子中,this指代控件,他只能存在于一个窗体中,而不能被两个窗体同时拥有,及一个this不能被两个对象同时引用。

  • 相关阅读:
    iOS开发CoreAnimation解读之三——几种常用Layer的使用解析
    iOS开发CoreAnimation解读之二——对CALayer的分析
    iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程
    在最完整的搜索提示降史上的用户交互的研究——阅读《An Eye-tracking Study of User Interactions with Query Auto Completion》
    学习算法
    This Android SDK requires Android Developer Toolkit version 22.6.2 or above.
    一切都不是为了营销手段的目的都是耍流氓
    LeetCode219:Contains Duplicate II
    无尽的循环ViewPager
    允许Ubuntu14.04&quot;保存&quot;屏幕亮度值
  • 原文地址:https://www.cnblogs.com/yuerdongni/p/2662307.html
Copyright © 2011-2022 走看看