zoukankan      html  css  js  c++  java
  • C# 窗体间传值总结

    1、父窗体传值给子窗体

    在父窗体中写:
    FormChild fc = new FormChil();//新建一个子窗体
    fc.ShowDialog(this);//以上两句即实现了子窗体和父窗体的联系
    textBox2.Text = fc.Str1;//已实现联系,可以传值
    2、子窗体传值给父窗体
    有两种方法:
     (1) 在子窗体中写:
         FormParent fp = (FormParent)this.Owner;//实现联系
         textBox1.Text = fp.Str1;//已实现联系,可以传值
      (2)运用子窗体的构造函数将父窗体的值传入(代码如下),可传一两个父窗体的值,如果要传的值多可以用ArrayList,也可直接传窗体(示例代码直接传窗体)
    总结:抓住窗体对象和要传具体值的联系
    代码:
    Form1.cs
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace TestTransValue
    {
    public partial class Form1 : Form
    {
    private string form1Text;

    public string Form1Text
    {
    get { return form1Text; }
    set { form1Text = value; }
    }
    public Form1()
    {
    InitializeComponent();
    form1Text
    = "这是来自Form1的Text";
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
    Form2 f2
    = new Form2();
    f2.ShowDialog(
    this);
    textBox1.Text
    = f2.Form2Text;//以上三句即可实现从子窗体向父窗体的传值
    }

    private void button2_Click(object sender, EventArgs e)
    {
    Form3 f3
    = new Form3(this);
    f3.ShowDialog(
    this);
    textBox2.Text
    = f3.Form3Text;//以上三句即可实现从子窗体向父窗体的传值
    }
    }
    }
    Form2.cs
    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 TestTransValue
    {
    public partial class Form2 : Form
    {
    private string form2Text = "这是来自form2的Text";

    public string Form2Text
    {
    get { return form2Text; }
    set { form2Text = value; }
    }
    public Form2()
    {
    InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
    Form1 f1
    = (Form1)this.Owner;
    textBox1.Text
    = f1.Form1Text;//以上两句即可实现子窗体获取父窗体的值(需要在父窗体中声明)
    }
    }
    }
    Form3.cs
    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 TestTransValue
    {
    public partial class Form3 : Form
    {
    private string form3Text = "这是来自form3的Text";
    Form1 f1;

    public string Form3Text
    {
    get { return form3Text; }
    set { form3Text = value; }
    }

    public Form3(Form1 form1)
    {
    InitializeComponent();
    f1
    = form1;
    }

    private void Form3_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
    textBox1.Text
    = f1.Form1Text;
    }
    }
    }
  • 相关阅读:
    linux命令行下命令参数前的一横(-)和两横(--)的区别
    sql的集合操作
    二叉树的遍历
    linux网络编程中阻塞和非阻塞socket的区别
    Python 信号量
    python中文件的复制
    Linux网络服务器epoll模型的socket通讯的实现(一)
    Linux启动提示“unexpected inconsistency;RUN fsck MANUALLY”
    Linux用户级线程和内核级线程区别
    nodejs的cs模式聊天客户端和服务器实现
  • 原文地址:https://www.cnblogs.com/step/p/2096523.html
Copyright © 2011-2022 走看看