zoukankan      html  css  js  c++  java
  • c# winform窗体间的传值

    说明:本文讲解两个窗体之间的传值,主要用到两个窗体,form1,form2

    1、在form1窗体单击按钮,打开窗体form2,然后把form2中文本框的值传递给form1

    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 FormToform
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            //获取值
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 fr = new Form2();
                DialogResult rsult = fr.ShowDialog();
                if(rsult==DialogResult.OK)
                {
                    //获取窗体2传回来的值
                    listBox1.Items.Add(fr.UKind);
                    listBox1.Items.Add(fr.UName);
                }
            }
        }
    }
    窗体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 FormToform
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            //给窗体定义两个属性
         
            public string UKind {
                get { return textBox1.Text; }
                set { textBox1.Text= value; }
            }   
            public string UName
            {
                get { return textBox2.Text; }
                set { textBox2.Text = value; }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                DialogResult = DialogResult.OK;
            }
        }
    }
    2、在form1窗体单击按钮,打开窗体form2,然后给form2中文本框赋值

    form1中按钮的代码如下:

      private void button2_Click(object sender, EventArgs e)
            {
                Form2 fr = new Form2();
                fr.UName = "姓名";
                fr.UKind = "类别";
                DialogResult rsult = fr.ShowDialog();
               
            }

    form2与标题1一样这里不做赘述

  • 相关阅读:
    11月12号实验课
    10月29日实验
    10.15实验课作业
    Idea的使用和设置
    常用的Linux命令
    KAFKA总结
    SpringBoot+Maven+MyBaties+Mysql快速搭建一个项目
    Spring 常见面试
    Windows下安装ZK kafka Mysql dubbo redis
    MySQL常见的面试题
  • 原文地址:https://www.cnblogs.com/net064/p/8177035.html
Copyright © 2011-2022 走看看