如何在 C# 中将值从第二个窗体传递回第一个窗体



我对专门用 c# 编程相对较新。我通常在前端使用 asp.net,在后端使用 C#,但我正在尝试学习如何使用 C# 做更多的事情。本质上,我有一个表单(form1),上面有文本框和一个按钮,可以转到form2(配置表单)。在 form2 上,我想从 SQL Server 中的数据库中填充一些文本框,并且还允许我在需要配置/更改值时键入文本框。然后,当点击接受按钮时,我想用 form1 文本框中的值填充 form2 上的文本框。

所以,我基本上需要知道的是

1)如何使用sql服务器填充文本框
2)然后如何将这些值传递回形式1?

我已经查找/知道如何将键入的值从一个表单传递到另一个表单,但是我在不使用pageload的情况下将它们恢复到第一个表单时被绊倒了。

编辑:我能够修改和保存从sql服务器加载的信息,但是当我回到表单1时,它没有反映这些更改。这是我的代码:

表格1:

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。如果对话框结果正常,则重新加载配置。

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

在关闭 Form2 之前,您应该相应地设置对话框结果。

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 关闭。

相关内容

最新更新