2016-05-14 79 views
0

我有兩種形式Form 1和Form:從一種形式將數據傳遞到在Windows窗體另一種形式的C#

  1. Form1名爲 「DemandePrixClient」 從TextboxesComboboxsbutton轉讓取得。名爲「DemandePrixFournisseur」
  2. Form2Textboxes製成(通過所有的數據項)

當我點擊button轉移,這將顯示form2Form1全部投入。

在此先感謝。

我有下面的代碼,但它不工作。

//Code Form1 
public partial class DemandePrixClient : Form 
{ 
    public string textbox1; 
    public string textbox2; 
    public string textbox3; 
    public string textbox5; 
    public string combobox1; 
    public string combobox2; 
    public string combobox4; 
    public string combobox6; 
    public string datetime; 

    public DemandePrixClient() 
    { 
     InitializeComponent(); 
    } 
    private void DemandePrixClient_Load(object sender, EventArgs e) 
    { 
     // TODO: cette ligne de code charge les données dans la table 'timarDataSet1.Client'. Vous pouvez la déplacer ou la supprimer selon vos besoins. 
     this.clientTableAdapter.Fill(this.timarDataSet1.Client); 

    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     DemandePrixClient info = new DemandePrixClient(); 
     info.textBox1 = textBox1; 
     info.textBox2 = textBox2; 
     info.textbox3 = textbox3; 
     info.textbox5 = textbox5; 
     info.comboBox1 = comboBox1; 
     info.combobox2 = combobox2; 
     info.comboBox4 = comboBox4; 
     info.comboBox5 = comboBox5; 
     info.datetime = datetime; 
     DemandePrixFournisseur dpf = new DemandePrixFournisseur(info); 
     dpf.Show(); 
     this.Hide(); 
    } 

//代碼窗體2

public partial class DemandePrixFournisseur : Form 
{ 

    private DemandePrixClient info; 

    public DemandePrixFournisseur(DemandePrixClient information) 
    { 
     InitializeComponent(); 
     info = information; 
    } 
} 

回答

1

使用呼叫DemandePrixFournisseurDemandePrixClient目前情況this關鍵字

public string text1; 
public string text2; 
public string text3; 
private void button1_Click(object sender, EventArgs e) 
    { 
     this.text1 = this.textBox1.Text; 
     this.text2 = this.textBox2.Text; 
     this.text3 = this.textBox3.Text; 
     DemandePrixFournisseur dpf = new DemandePrixFournisseur(this); 
     dpf.Show(); 
     this.Hide(); 
    } 

那麼你就可以得到所有值

public partial class DemandePrixFournisseur : Form 
{ 

    private DemandePrixClient info; 

    public DemandePrixFournisseur(DemandePrixClient information) 
    { 
     InitializeComponent(); 
     info = information; 
     this.textBox1.Text = info.text1; // you can get other value like this way 
     this.textBox2.Text = info.text2; 
     this.textBox3.Text = info.text3; 
     //or simply 
     this.textBox2.Text = information.text2; 
     // and others textbox and combobox value similarly 
    } 


} 
+0

當我編寫'this.textBox1.Text = info.textBox1.Text'它低於它,我的假 –

+0

你是什麼意思加下劃線?你試過我最新的編輯嗎? – Mostafiz

+0

'this.textBox1.Text = info.textBox1.Text',它underligne'文本(它找不到它) –

0

通過類靜態字段實現此目的的簡單方法。在靜態字段中和Form2上的Form1設置數據從靜態字段中獲取數據。

相關問題