Home Backend Development C#.Net Tutorial Summary of value transfer between C# forms

Summary of value transfer between C# forms

Mar 02, 2017 am 11:49 AM

C# Summary of value transfer between forms

1. Pass value from parent form to child form

Write in the parent form:

FormChild fc = new FormChil();//新建一个子窗体
fc.ShowDialog(this);//以上两句即实现了子窗体和父窗体的联系
textBox2.Text = fc.Str1;//已实现联系,可以传值
Copy after login

2. Pass the value from the subform to the parent form


There are two methods:

(1) Write in the subform:

   FormParent fp = (FormParent)this.Owner;//实现联系
     textBox1.Text = fp.Str1;//已实现联系,可以传值
Copy after login

(2) Use the constructor of the subform to pass in the value of the parent form (the code is as follows), you can pass one or two parent forms If there are many values ​​to be transferred, you can use ArrayList, or you can directly transfer the form (the sample code directly transfers the form)

Summary: Seize the connection between the form object and the specific value to be transferred

Code:

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;//以上三句即可实现从子窗体向父窗体的传值        }
    }
}
Copy after login


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;//以上两句即可实现子窗体获取父窗体的值(需要在父窗体中声明)        }
    }
}
Copy after login
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;
        }
    }
}
Copy after login

The above is the summary of value transfer between C# forms. For more related content, please pay attention to the PHP Chinese website (www.php.cn) !


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles