在C#中我有两个窗口,一个是form1,一个是FORM2,如何在form2中获得form中lable中的值

2025-05-12 14:31:17
推荐回答(5个)
回答1:

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;

//注意Lable属性要更改成public

namespace WindowsFormsApplication2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            Form2 fr2 = new Form2(this);

            fr2.Show();

        }

    }

}

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 WindowsFormsApplication2

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        private Form1 _fr1;

        public Form2(Form1 fr1)

        {

            InitializeComponent();

            _fr1 = fr1;

        }

        private void Form2_Load(object sender, EventArgs e)

        {

           MessageBox.Show( _fr1.label1.Text);

        }

    }

}

回答2:

如果你是希望在Form1中点击一个按钮弹出Form2,那么双击按钮,代码如下
private void button1_click(Object sender,EventArgs e){
String str = textBox1.Text;
//使用构造方法将值传过去
Form2 f = new Form2(str);
}

以上是在Form1.cs中的代码

在Form2.cs中添加:
public Form2(String str){
/*
str就是传过来的textBox1的值
在这里做你想做的
*/
}
望采纳!

回答3:

假如label控件name属性为aaa;
在Form1窗体中设置全局变量。public static string bbb;
bbb=aaa.text;
然后在Form2窗体中你要调用lable的值。直接写Form1.aaa就写了。

注意我上面说的form1.form2都是窗体的name属性名称。

回答4:

form1中定义一个public属性,该属性的get方法为获取lable.text的值,那么form2就可以直接使用form1的该属性值了

回答5:

我觉得构造或许对你来讲有点难,你可以在form1中定义变量,在form2 中new出个form1 的对象在把lable.text中的值赋给这个变量就行了,在from1 中就有了你想要得值,我初学的时候就是这样干的,不懂可以追问,我可以提供代码哈