zoukankan      html  css  js  c++  java
  • .Net中窗体间传递值的一种方法

    新建两个窗体,分别为Form1和Form2,前者为主窗体,两个窗体上都有一个文本框和一个命令按钮。在Form2中单击命令按钮,会关闭Form2窗体,同时将Form2中文本框的值显示在Form1的文本框中。

    Form1中的代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                frm2.frm1 = this;               //将当前窗体frm1赋给frm2中声明的窗体变量
                frm2.Show();
            }

            public void getInfo(string name)    //声明公用方法,以在frm2中使用
            {
                textBox1.Text = name;
            }
        }
    }

    Form2中的代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        public partial class Form2 : Form
        {
            public Form1 frm1;      //声明frm1窗体,以调用其方法

            public Form2()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                frm1.getInfo(textBox1.Text);//调用frm1的方法将frm2中textbox1的内容赋给frm1中的textbox1           
                this.Close();   //关闭当前窗体frm2
            }
        }
    }

  • 相关阅读:
    pycharm快捷键
    Docker
    Go语言与Elasticsearch
    Celery与APScheduler
    爬虫入门到入狱
    数据分析
    后台管理
    Linux基础与自动化运维
    微信小程序
    Git
  • 原文地址:https://www.cnblogs.com/mokliu/p/2138896.html
Copyright © 2011-2022 走看看