C#窗体之间传递参数

1.  (1->2) 把第一个窗体中的参数传递给第二个窗体


把第二个窗口写个带参数的构造函数,用参数给变量赋值,或者把这变量声明为静态的,直接赋值


Form2中
string strText="";//你要赋值的变量
public Form2(string text)
{
strText=text; 
}
Form1中
Form2 form2=new Form2(textBox1.text.trim());
form2.show();


2.  (1<-2)把第二個窗體的參數傳遞給第一個窗體

第一個窗體代碼:

public partial class Form1 : Form
    {
        public string str;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.ShowDialog();  //必須使用ShowDialog()函數,否則不能正常進行參數傳遞
            this.str = f.str;
            label1.Text = str;
        }
    }
第二個窗體代碼:

public partial class Form2 : Form
    {
        public string str, str1, str2;
        
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            str1=textBox1.Text;
            str2 = textBox2.Text;
            str = str1 + str2;
           
        }
需要設置Form2的button的DialogResult為OK


3.  (1<->2) C#窗體之間參數互傳(第一個傳第二個、第二個傳第一個都可以)

比如说form1上有一个button1,Form2上有一个textBox1和一个button1。

Form1

class Form1:Form
{
public string Str;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2=new Form2();
f2.f1=this;
f2.show();
}
}

Form2
class Form2:Form
{
public Form1 f1;
private void button1_Click(object sender, EventArgs e)
{
f1.Str=this.textBox1.Text;
}
} 


### C#窗体传递参数的方法与示例 在 C# 中,跨窗体传递参数可以通过构造函数、属性或事件等方式实现。以下是通过构造函数传递参数的示例代码: ```csharp // 窗体 1 (Form1) using System; using System.Windows.Forms; public class Form1 : Form { private Button button; public Form1() { button = new Button(); button.Text = "打开窗体2"; button.Click += new EventHandler(Button_Click); this.Controls.Add(button); } private void Button_Click(object sender, EventArgs e) { string parameter = "这是从窗体1传递参数"; Form2 form2 = new Form2(parameter); // 通过构造函数传递参数 form2.Show(); } } ``` ```csharp // 窗体 2 (Form2) using System; using System.Windows.Forms; public class Form2 : Form { private Label label; public Form2(string text) // 构造函数接收参数 { label = new Label(); label.Text = "接收到的参数: " + text; // 显示接收到的参数 label.AutoSize = true; this.Controls.Add(label); } } ``` 上述代码展示了如何通过构造函数将参数从一个窗体传递到另一个窗体[^2]。 此外,还可以通过公共属性的方式实现跨窗体参数传递。例如: ```csharp // 窗体 1 (Form1) using System; using System.Windows.Forms; public class Form1 : Form { private Button button; public Form1() { button = new Button(); button.Text = "打开窗体2"; button.Click += new EventHandler(Button_Click); this.Controls.Add(button); } private void Button_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Parameter = "这是从窗体1传递参数"; // 设置公共属性 form2.Show(); } } // 窗体 2 (Form2) using System; using System.Windows.Forms; public class Form2 : Form { private Label label; public string Parameter { get; set; } // 公共属性 public Form2() { label = new Label(); label.Text = "接收到的参数: " + Parameter; // 显示接收到的参数 label.AutoSize = true; this.Controls.Add(label); } } ``` 此方法使用公共属性 `Parameter` 来传递数据[^3]。 ### 注意事项 - 如果需要在多个窗体之间共享数据,可以考虑使用全局变量或静态类。 - 使用事件机制也可以实现更复杂的跨窗体通信,但实现相对复杂,适合高级场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值