C# 窗口間傳值

         窗口間傳值方法

 

方法一: 利用窗口的初始化,來傳遞參數,這種只適用一個窗口打開另一個窗口,在新窗口中傳入參數。

 

Form1.cs

 

String s="123";

Form2 f2=new f2(s);

f2.show();

 

 

 

Form2.cs

 

public Form2(string s)

       {

           InitializeComponent();

                            Button1.text=s;

       }

這時窗口2的button按鈕的值就是s的值123。

 

 

方法二:利用ShowDialog的返回值判定子窗口是否銷燬,然後把子窗口的屬性傳遞到form1中。

Form1.cs

private string m_IP;

 

       public string IP

       {

           get { return m_IP; }

           set { m_IP = value; }

       }

 

       private string m_content;

 

       public string Content

       {

           get { return m_content; }

           set { m_content = value; }

       }

 

       private string m_ID;

 

       public string ID

       {

           get { return m_ID; }

           set { m_ID = value; }

       }

       public Form1()

       {

           InitializeComponent();

       }

 

       private void button1_Click(object sender, EventArgs e)

       {

           Form2 frm = new Form2();

           if (frm.ShowDialog() == DialogResult.OK)

           {

                IP = frm.IP;

                Content = frm.Content;

                ID = frm.ID;

                button1.Text = IP;

           }

       }

 

 

private string m_IP;

 

       public string IP

       {

           get { return m_IP; }

           set { m_IP = value; }

       }

 

       private string m_content;

 

       public string Content

       {

           get { return m_content; }

           set { m_content = value; }

       }

 

       private string m_ID;

 

       public string ID

       {

           get { return m_ID; }

           set { m_ID = value; }

       }

 

       public Form2()

       {

           InitializeComponent();

       }

 

       private void button1_Click(object sender, EventArgs e)

       {

           IP = textBox1.Text;

           Content = textBox2.Text;

           ID = textBox3.Text;

           this.DialogResult = DialogResult.OK;   //讓這個窗口的DialogResult的值爲OK當這時觸發form1中的if (frm.ShowDialog() == DialogResult.OK)成功。

           this.Close();//關閉

        }


例子在百度網盤有下載:點擊打開鏈接

窗口傳值程序下載連接

發佈了27 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章