日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#實現(xiàn)WinForm傳值實例解析

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

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

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

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

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

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

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

2:打開Form2,添加一個textBox:textBox1;添加一個Button:button1;然后添加一個構(gòu)造函數(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傳值的內(nèi)容和相關(guān)的知識就向你介紹到這里,希望對你了解和學習C#實現(xiàn)WinForm傳值的問題有所幫助。


當前題目:C#實現(xiàn)WinForm傳值實例解析
本文網(wǎng)址:http://www.5511xx.com/article/ccchios.html