av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

C#實現(xiàn)WinForm傳值實例解析

C#實現(xiàn)WinForm傳值的問題經常會做為公司面試的題目,那么作為學習C#以及WinForm傳值,我們需要掌握哪些方法和思路呢?下面我們就向你介紹詳細的思路和實現(xiàn)的具體步驟,希望對你有所幫助。

創(chuàng)新互聯(lián)建站自2013年起,先為嵊泗等服務建站,嵊泗等地企業(yè),進行企業(yè)商務咨詢服務。為嵊泗企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

C#實現(xiàn)WinForm傳值的思路:

從Form1傳遞到Form2: 2個窗體即兩個類,兩個窗體間的數(shù)據(jù)傳送,可以采用構造函數(shù)來實現(xiàn)。

從Form2返回到Form1,并傳遞數(shù)據(jù):實例化Form2后,打f2用ShowDialog()方法,然后等待f2關閉時再回傳數(shù)據(jù)到Form1。

C#實現(xiàn)WinForm傳值步驟及代碼:

1:新建兩個窗口: Form1,Form2;

2:打開Form2,添加一個textBox:textBox1;添加一個Button:button1;然后添加一個構造函數(shù):

 
 
 
  1. //定義一個變量,用來傳值。  
  2. public string returnValue ;  
  3.  
  4. public Form2(string txtValue)  
  5. {  
  6.   InitializeComponent();  
  7.  
  8.   this.textBox1.Text = txtValue;  
  9. }  

然后在button1的單擊事件中添加如下代碼:

 
 
 
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.   returnValue = this.textBox1.Text;  
  4.   this.Close();  

3:Form1中添加一個textBox:textBox1;添加一個Button:button1;然后在button1的單擊事件中添加如下代碼:

 
 
 
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.   string txtValue = this.textBox1.Text;  
  4.   Form2 f2 = new Form2(txtValue);  
  5.   f2.ShowDialog();  
  6.   this.textBox1.Text = f2.returnValue;  

Form1 中 (父窗口:)

 
 
 
  1. public class Form1 : System.Windows.Forms.Form  
  2. {  
  3.  private System.Windows.Forms.Button btnOpen;  
  4.  public System.Windows.Forms.TextBox txtContent;   
  5. //注意是public  
  6.  
  7.   ........  
  8.  
  9.   ........  
  10.  
  11.  [STAThread]  
  12. static void Main()  
  13. {  
  14. Application.Run(new Form1());  
  15. }  
  16.  
  17.  private void btnOpen_Click(object sender, System.EventArgs e)  
  18.  {  
  19.   Form2 frm=new Form2(this);  
  20.   frm.ShowDialog();  
  21.  }  
  22.  
  23. }  

Form2中(子窗口)

 
 
 
  1. public class Form2 : System.Windows.Forms.Form  
  2. {  
  3.  private System.Windows.Forms.Button button1;  
  4.  private System.Windows.Forms.TextBox txtValue;  
  5.  
  6.  private Form _parentForm=null;  
  7.  
  8.   public Form2()  
  9.   {  
  10.   InitializeComponent();   
  11.   }  
  12.  
  13.  public Form2(Form parentForm)  
  14.  {  
  15. InitializeComponent();  
  16. this._parentForm =parentForm;  
  17.  }  
  18.  
  19.  ........  
  20.  
  21. ........  

更新父窗口中文本框中的值!

 
 
 
  1. private void button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.  ((Form1)_parentForm).txtContent.Text =this.txtValue .Text ;  
  4. }  

C#實現(xiàn)WinForm傳值的內容和相關的知識就向你介紹到這里,希望對你了解和學習C#實現(xiàn)WinForm傳值的問題有所幫助。


當前題目:C#實現(xiàn)WinForm傳值實例解析
文章路徑:http://uogjgqi.cn/article/ccchios.html
掃二維碼與項目經理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流