ga('set', 'anonymizeIp', 1);
主從關係在程式中是重要的一塊,不管是scope或是這邊要討論的傳值權限問題都是撰寫程式前必須理解的部分。
本篇文章討論如何在兩個Form窗口互相傳值。
假設From1為父視窗,Form2為子視窗。
Form1父視窗程式如下:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 childForm = new Form2();
childForm.Msg = "Msg to child-form success.";
childForm.SetValue();
childForm.ShowDialog();
}
Form2子視窗程式如下:
private string string1;
public string Msg
{
set{ string1 = value }
}
public void setValue()
{
this.label1.Text = string1;
}
public Form2()
{
InitializeComponent();
}
Form1父視窗程式如下:
public Form1()
{
InitializeComponent();
}
private string strValue;
public string MsgFromChild
{
set{ strValue = value }
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
Form2 childForm = new Form2();
// important!! Point the Form2's owner to Form1
childForm.Owner = this;
childForm.ShowDialog();
MessageBox.Show(strValue);
}
Form2子視窗程式如下:
public Form2()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
// point the Form2's owner to Form1
Form1 parentForm = (Form1)this.Owner;
parentForm.MsgFromChild = "Msg from child-form success";
this.close();
}
簡單的說,
就是利用class中get, set以及form owner來控制變數值的傳遞。