当前位置:首页 > C# > 正文内容

C# 窗体间传值(Form与From之间互相传值)

admin7年前 (2018-06-04)C#6884

1、委托

  两个窗体,窗体很简单,只实现改变颜色功能,一看就会:

代码如下,只贴按钮事件代码:



打开Form2按钮事件

private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.ChangeColor += new ChangeFormColor(frm2_ChangeColor);
            frm2.Show();
        }

        void frm2_ChangeColor(bool topmost)
        {
            this.BackColor = Color.LightBlue;
            this.Text = "改变成功!";
        }



改变Form1颜色按钮

     //定义一个委托         
        public delegate void ChangeFormColor(bool topmost); 
        //给一个事件
        public event ChangeFormColor ChangeColor; 
        private void button1_Click(object sender, EventArgs e)
        {
            ChangeColor(true);
            this.Close();
        }


2、先截图来看

修改之后按Confirm,关闭form2,自动刷新form1,思路是这样:在form1上面查询显示到datagridview,右键选择修改,弹出模式窗体form2,修改,关闭,自动刷新form1。



            //添加到修改按钮里面,弹出模式窗体Form2,进行修改
            Form2 form2 = new Form2();
            form2.Owner = this;
            form2.ShowDialog();


Confirm按钮代码

if (//你的修改方法)
            {
                MessageBox.Show("修改成功!");
                //把拥有Form2的窗体强制转换为Form1,然后赋值给Form1窗体类型的变量form1
                Form1 form1 = (Form1)this.Owner;
                //注意下面select()方法,是form1里面的一个查询方法,作用等于refresh,但refresh不行,无所谓,调用这个方法来刷新一样的。
                form1.select();
                Close();
            }

3、第三种有点小麻烦,但也不错(如果修改的数据单一,比较少的话)

两个窗体的截图如下


单击choose 弹出form2,选择完性别之后,关闭窗体,数据传递到form1sex框中,代码少,全贴出来吧


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 test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            if (frm2.ShowDialog()==DialogResult.OK)
            {
                //用模式窗体弹出来,给form2里面public属性的strSex赋值
                textBox2.Text = frm2.strSex;
                frm2 = null;  //清空
            }
        }
    }
}



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 test
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        
        //定义一个private属性str用于存储sex,用到什么属性,就自己加什么属性,完了把光标停到属性str这里,按Ctrl+R,E,自动封装
        private string str;

        public string strSex //这里自动封装后应该是Str,我改了改名字,表纠结这个
        {
            get { return str; }
            set { str = value; }
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            //赋值完之后关闭窗体
            this.strSex = comboBox1.SelectedText;
            this.DialogResult = DialogResult.OK;
        }
    }
}




扫描二维码推送至手机访问。

版权声明:本文由视觉博客发布,如需转载请注明出处。

本文链接:https://feelsight.cn/post/63.html

“C# 窗体间传值(Form与From之间互相传值)” 的相关文章

c#保存textbox中的字符串到txt文件中

/********************** 保存接收按钮 *****************************/         private void Savetx...

c# 串口发送接收数据

/********************** 串口数据接收事件 *****************************/         private void Seri...

c#实现FTP上传

/// <summary>         /// 上传文件         /// <...

UDP打洞原理及代码

UDP"打洞"原理 1.       NAT分类根据Stun协议(RFC3489),NAT大致分为下面四类1)      Full Cone这种NAT内部的机器...

UDP穿透NAT的原理与实现(UDP“打洞”原理)

NAT(The IP Network Address Translator) 的概念和意义是什么?NAT, 中文翻译为网络地址转换。具体的详细信息可以访问RFC 1631 - http://www.faqs.org/rfcs/rfc1631.html, 这是对于NAT的定义和解释的最权威的...

C#图片处理示例(裁剪,缩放,清晰度,水印)

using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; usin...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。