c# winform中,怎样得到另一窗体的值(总结)

c#中如何得到子窗口上的控件值?

 

方法1:将textBox1定义为public,不推荐

(在A项目中用一个内存变量存值.并设为PUBLIC.

在B项目中引用A项目。获取需要的值。

用的时候注意 using A的命名空间。)

 

方法2:给form2增加一个属性,推荐

如名为 TextBoxValue

public string TextBoxValue{ get { return textBox1.Text; } set { textBox1.Text = value; } }

然后就可以在form1中访问from2.TextBoxValue

 

参考:http://blog.csdn.net/catshitone/article/details/49558511

 

 

C#简单实现子窗体向父窗体传值的方法_C#教程_脚本之家

来源网址: http://www.jb51.net/article/72446.htm

 

 

本文实例讲述了C#简单实现子窗体向父窗体传值的方法。分享给大家供大家参考。具体如下:

击Form1的button1 打开Form2再点击Form2的button2

在button2_Click事件中 通过this.Owner将Form2的textBox2的值设置给Form1的textBox1并关闭Form2

?

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

public partial class Form1 : Form

{

  public Form1()

  {

   InitializeComponent();

  }

  private void button1_Click(object sender, EventArgs e)

  {

   Form2 frm2 = new Form2();

   frm2.Show(this);//或 frm2.ShowDialog(this);

   ////或者

   //Form2 frm2 = new Form2();

   //frm2.Owner = this;

   //frm2.Show();//或 frm2.ShowDialog();

  }

}

public partial class Form2 : Form

{

  public Form2()

  {

   InitializeComponent();

  }

  private void button2_Click(object sender, EventArgs e)

  {

   Form1 frm1 = (Form1)this.Owner;

  //注意 如果textBox1是放在panel1中的 则先找panel1 再找textBox1

   ((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;

   this.Close();

  }

}

 

点击Form1的button1 打开Form2

再点击Form2的button2

在button2_Click事件中 通过this.Owner及调用父窗体Form1的公开属性或方法

将Form2的textBox2的值设置给Form1的textBox1

并关闭Form2

?

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

31

32

33

34

public partial class Form1 : Form

{

  public Form1()

  {

   InitializeComponent();

  }

  public string TextBox1Text

  {

   set { this.textBox1.Text = value; }

   get { return this.textBox1.Text; }

  }

  private void button1_Click(object sender, EventArgs e)

  {

   Form2 frm2 = new Form2();

   frm2.Show(this);//或 frm2.ShowDialog(this);

   ////或者

   //Form2 frm2 = new Form2();

   //frm2.Owner = this;

   //frm2.Show();//或 frm2.ShowDialog();

  }

}

public partial class Form2 : Form

{

  public Form2()

  {

   InitializeComponent();

  }

  private void button2_Click(object sender, EventArgs e)

  {

   Form1 frm1 = (Form1)this.Owner;

   frm1.TextBox1Text = this.textBox2.Text;

   this.Close();

  }

}


 

c# winform中,怎样得到另一窗体的值_百度知道

来源网址: https://zhidao.baidu.com/question/390505985.html

 

 

 

c# winform中,怎样得到另一窗体的值10

迷茫了,在vb中可以很简单实现的问题在c#中竟如此困难,我的vb代码是这样的:

form2中:

private sub botton1_click()

text1.text=form1.text1.text'就这一句能搞定的事,c#搞复杂是为什么?求解?

end sub

换成c#,如何得到?

 

dongao8080| 浏览 5383 次|举报

 

 

 

 

 

推荐于2017-10-02 04:59:33最佳答案

 

 

以下示例程序实现:

(1)Form2从Form1获取一个字符串;

(2)Form2修改这个字符串后,再将修改后的字符串返回给Form1显示;

实现方法:

(1)在Visual Studio中创建一个“Windows 窗体应用程序”项目

(2)向项目中添加Form2

(3)在Form1上布置一个Label和一个Button

(4)在Form2上布置一个TextBox和一个Button

(5)窗体代码Form1.cs

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;

 

using System.Windows.Forms;

 

 

 

namespace WindowsFormsApplication1

 

{

 

    public partial class Form1 : Form

 

    {

 

        public Form1()

 

        {

 

            InitializeComponent();

 

            label1.Text = "你好,世界!";

 

            button1.Text = "显示Form2";

 

        }

 

 

 

        // 添加一个公共属性 StringValue

 

        public string StringValue

 

        {

 

            get { return label1.Text; }

 

            set { label1.Text = value; }

 

        }

 

 

 

        private void button1_Click(object sender, EventArgs e)

 

        {

 

            // 实例化Form2

 

            // 实例化使用Form2重载的构造函数,详见Form2.cs

 

            Form2 f2 = new Form2(this);

 

            f2.Show();

 

        }

 

    }

 

}

 

(5)窗体代码Form2.cs

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

 

31

 

32

 

33

 

34

 

35

 

36

 

37

 

using System;

 

using System.Windows.Forms;

 

 

 

namespace WindowsFormsApplication1

 

{

 

    public partial class Form2 : Form

 

    {

 

        // 对Form1实例的引用

 

        Form1 f1;

 

 

 

        public Form2()

 

        {

 

            InitializeComponent();

 

        }

 

 

 

        // 重载构造函数

 

        // 构造函数参数 f1:对Form1实例引用

 

        public Form2(Form1 f1)

 

            : this()

 

        {

 

            // 建立对Form1实例的引用

 

            this.f1 = f1;

 

            // 通过f1.StringValue属性获取Form1上label1显示的内容

 

            textBox1.Text = f1.StringValue;

 

             

 

            button1.Text = "关闭";

 

        }

 

 

 

        private void button1_Click(object sender, EventArgs e)

 

        {

 

            // 关闭前,利用f1.StringValue属性,将textBox1的内容

 

            // 显示在Form1上的label1中

 

            f1.StringValue = textBox1.Text;

 

            this.Close();

 

        }

 

    }

 

}

 

(6)运行

点击Form1上“显示Form2”按钮后

在Form2中,修改textBox1内容

点击Form2上“关闭”按钮后

 

 

 

 

怎样在C#中实现父窗体向子窗体传值和子窗体向父窗体传值_百度知道

来源网址: https://zhidao.baidu.com/question/217626694.html

 

 

 

怎样在C#中实现父窗体向子窗体传值和子窗体向父窗体传值10

具体是在Form1和Form2中各有一个textbox和button,点击Form1的button将Form1的textbox中的值传入Form2中的textbox中。点击Form2中的button将Form2中的textbox中的值传入Form1中的textbox中,如图:

我有更好的答案

推荐于2016-07-28 08:25:21

 

form1.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 传值练习

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

//1、利用构造函数由父窗体向子窗体传值

private void button1_Click(object sender, EventArgs e)

{

Form2 f2 = new Form2(this.textBox1.Text);

f2.Show();

}

 

//利用方法由子窗体向父窗体传值

public void chuanzhi(string data)

{

this.textBox1.Text = data;

}

}

}

 

 

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 传值练习

{

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

//1、利用构造函数由父窗体向子窗体传值

public Form2(string name)

{

InitializeComponent();

this.textBox1.Text = name;

}

 

//2、利用方法由子窗体向父窗体传值

private void button1_Click(object sender, EventArgs e)

{

Form1 f1 = new Form1();

f1.chuanzhi(this.textBox1.Text);

f1.Show();

}

 

}

}

 

 

 

 

你可能感兴趣的:(c# winform中,怎样得到另一窗体的值(总结))