2017-05-24 98 views
0

我相對較新的編程專門在C#中。我通常在前端使用asp.net,在後端使用C#,但我正在嘗試學習如何使用C#做更多的事情。從本質上講,我有一個窗體(form1),它有文本框和一個按鈕,可以轉到form2(一個配置窗體)。在form2上,我想從SQL Server中的數據庫填充一些文本框,並且還允許我在需要時鍵入文本框來配置/更改值。然後,當點擊接受按鈕時,我想用form2文本框中的值填充form1上的文本框。如何將值從第二種形式傳遞迴第一種形式在C#

所以,我需要基本知道是
1)我如何填充文本框使用SQL Server
2)我怎麼然後通過這些值BACK形成1?

我擡頭/知道如何將值從一種表單傳遞給另一種表單,但我越來越絆倒在沒有使用頁面載入的情況下將表單返回到第一個表單。

編輯:我能夠修改和保存從sql server加載的信息,但是當我回到表單1時,它不反映這些更改。這裏是我的代碼:

Form1中:

public Form1() 
     { 
      InitializeComponent(); 

      // Load the most recent configuration 
      LoadConfig(); 
     } 

private void LoadConfig() 
    { 
     // Populate the textboxes based on the last used settings 
     // Create the SQL Connection 
     String conString = ConfigurationManager.ConnectionStrings["mainConnection"].ConnectionString; 
     SqlConnection con = new SqlConnection(conString); 

     // Create the SELECT command 
     String sqlSelect = //took out for formatting purposes, but its just a select from a table 
     SqlCommand com = new SqlCommand(sqlSelect, con); 

     try 
     { 
      con.Open(); 

      using (SqlDataReader read = com.ExecuteReader()) 
      { 
       // Set the textboxes to the values from the database 
       while (read.Read()) 
       { 
        txtInstrumentNoType.Text = (read["instr_group_filter"].ToString()); 
        txtDocType.Text = (read["doc_types_filter"].ToString()); 
        txtPageThreshhold.Text = (read["pages_per_subfolder"].ToString()); 
       } 

      } 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 


private void btnConfigure_Click(object sender, EventArgs e) 
     { 
      // Open the Form2 
      Form2 configureForm = new Form2(); 
      configureForm.Show(); 
     } 

FORM 2

private void btnApply_Click(object sender, EventArgs e) 
     { 
      // Update the configuation 
      ExecuteSqlUpdate(); 

     } 


     private void ExecuteSqlUpdate() 
     { 
      // Create the SQL Connection 
      String conString = ConfigurationManager.ConnectionStrings["mainConnection"].ConnectionString; 
      SqlConnection con = new SqlConnection(conString); 

      // Create the UPDATE command to update the configuration settings 
      // that are stored in the database 
      String sqlUpdate = "UPDATE Table" + 
            "SET instr_group_filter = @instr_group_filter," + 
            " doc_types_filter = @doc_types_filter," + 
            " pages_per_subfolder = @pages_per_subfolder"; 
      SqlCommand com = new SqlCommand(sqlUpdate, con); 

      try 
      { 
       con.Open(); 
       // Replace the parameters with what is typed in the textboxes 
       com.Parameters.AddWithValue("@instr_group_filter", txtInstrumentNoType.Text); 
       com.Parameters.AddWithValue("@doc_types_filter", txtDocType.Text); 
       com.Parameters.AddWithValue("@pages_per_subfolder", txtPageThreshhold.Text); 
       com.ExecuteNonQuery(); 
      } 
      finally 
      { 
       con.Close(); 
      } 

      this.Close(); 
     } 
+1

到目前爲止你的代碼是什麼? –

+0

我有點困惑。爲什麼你需要避免頁面加載?如果你在form2上,那麼form1不再是最新的權利?我會將這些值推送到form2上的數據庫,並在form1 pageload上檢索數據庫中的值。 –

+0

@JacobH會發送我已有的一些內容,目前正在修改其中的一些 – smelmo

回答

0

您應該使用ShowDialog顯示窗體2。如果DialogResult確定,則重新加載您的配置。

private void btnConfigure_Click(object sender, EventArgs e) 
{ 
    // Open the Form2 
    Form2 configureForm = new Form2(); 
    if (configureForm.ShowDialog(this) == DialogResult.OK) 
    { 
     LoadConfig(); 
    } 
} 

在關閉Form2之前,應該相應地設置DialogResult。

private void btnApply_Click(object sender, EventArgs e) 
{ 
    // Update the configuation 
    ExecuteSqlUpdate(); 
    DialogResult = DialogResult.OK; 
    this.Close(); // Remove this line from your ExecuteSqlUpdate() method! 
} 

使用ShowDialog將使Form2充當模態。它將阻止Form1,直到Form2關閉。

+1

就像一個魅力一樣,它現在能夠更新配置webform上的表格,將這些更改應用到sql server,並使用新值返回到前一個表單。非常感謝! – smelmo

相關問題