C# 폼 간 값 전송 요약
1. 상위 폼에서 하위 폼으로 값 전달
상위 양식에 작성:
1 2 3 | FormChild fc = new FormChil();
fc.ShowDialog(this);
textBox2.Text = fc.Str1;
|
로그인 후 복사
2. 하위 양식에서 상위 양식으로 값을 전달하는 방법에는 두 가지가 있습니다.
:
(1) 하위 양식에 작성:
1 2 | FormParent fp = (FormParent)this.Owner;
textBox1.Text = fp.Str1;
|
로그인 후 복사
(2) 하위 양식의 생성자를 사용하여 상위 양식의 값을 전달합니다(코드는 다음과 같습니다). 또는 상위 폼이 2개 전송되어야 할 값이 많은 경우 ArrayList를 사용하거나, 폼을 직접 전송할 수도 있습니다(샘플 코드가 폼을 직접 전송합니다)
요약: 폼 간의 연결을 점유하세요. 개체 및 전송할 특정 값
코드:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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;
}
}
|
로그인 후 복사
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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;
}
}
|
로그인 후 복사
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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;
}
}
}
|
로그인 후 복사
위는 C# 양식 간의 값 전송에 대한 요약입니다. PHP 중국어 홈페이지(www.php.cn)를 주목해주세요!