我常用於C#上Form間的傳值

  • 501
  • 0
  • 2012-10-17

摘要:我常用於C#上Form間的傳值

 

I usually use on "how to commucation in two forms" have two solutions.

We assume have two forms, A is the master form and B is the slave form.

The first solution is "new" an A object in B, and then use the object to access

the component in A. For example:


//In Form B
A f1 = new A(); \
f1.textBox1.Text = textBox1.Text;

The value will show on textBox1 of form A.

The second solution is use a global variable. For exampl:


//In Form A
public string[] strArray string[3];
//In Form B
A f1 = new A();
f1.strArray[0] = "Test";

!!Warning!!

Those soultions are stupid and easy.

If you want to know more.

Plz check:

http://www.dotblogs.com.tw/billchung/archive/2011/12/07/60985.aspx

2012/10/17傳值時最好設定誰是Parentform

//In Form1
Form2 aForm2 = new Form2();
//In Form2
public Form2(Form1 Parentform)
        {
            InitializeComponent();
            this.Tag = Parentform;
        }