[英]How to pass value from a second form back to first form in c#
我是專門使用C#編程的新手。 我通常在前端使用asp.net,在后端使用C#,但是我正在嘗試學習如何使用C#進行更多操作。 本質上,我有一個表單(form1),該表單具有文本框和一個按鈕,可以轉到form2(配置表單)。 在form2上,我想從SQL Server中的數據庫中填充一些文本框,如果需要配置/更改值,還允許我在文本框中鍵入內容。 然后,當點擊接受按鈕時,我想用在form2文本框中的值填充form1上的文本框。
所以,我需要基本了解的是 1)如何使用SQL Server填充文本框
2)然后如何將這些值傳回表格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();
}
表格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();
}
您應該使用ShowDialog來顯示Form2。 如果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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.