c# - 将数据从一个表单传递到另一个表单

标签 c# forms modal-dialog

我正在创建一个工具来管理 Windows 8.1 中的 WLAN 网络(因为 W8 没有带有 GUI 的工具)。在应用程序中

Screenshot of the application

单击“添加”按钮会显示另一个表单。填写字段后,我想单击“确定”,关闭第二个表单,并处理第一个表单上的输入数据。

我尝试实现这个问题中的示例 Send values from one form the another form ,但不能。 (我是新手,不太清楚。)

有人可以提供一个可行的例子吗?


第一个表单“添加”按钮

private void AddButton_Click(object sender, EventArgs e)
{
    // show second form

    // get input values (upon clicking on "OK" and closing the second form)

    // handle them
}

第二种形式的“确定”按钮

private void OKButton_Click(object sender, EventArgs e)
{
    // send input values to first form

    this.Close();
}

最佳答案

首先您应该创建一个类,例如:

public class YourFavoriteDefinedClass 
{
    public string NetworkName;
    public string SecurityType;
    public string EncryptType;
    public string SecurityKey;
}

那么您可以通过以下步骤解决:

程序 1:将 RefreshPerentList 方法添加到父表单

您可以在父表单中声明一个方法,然后在第二个表单的 OKButton_Click 中调用它来刷新或将 Item 添加到 ListView


示例:

首先,您应该将以下代码添加到您的 AddButton_Click 方法中。

private void AddButton_Click(object sender, EventArgs e)
{
     var frmSecond = new YourSecondFormName();
     frmSecond.Owner = this;
     frmSecond.ShowDialoge();
}

然后在您的 ParentForm 中声明以下方法(无线网络管理器)

public void RefreshPerentList(YourFavoriteDefinedClass objSecondFormParams)
{
    // Implement Your Code Here to refresh or add item to listview.
    var strNetWorkName = objSecondFormParams.NetworkName;
    var stSecurtiyType = objSecondFormParams.SecurityType; 
    ...
}

You Can Pass SecondForm parameters using an object with special type or Class that I named for example : var objSecondFormParams = new YourFavoriteDefinedClass();

然后您可以在 OKButton_Click 中使用以下代码调用它:

private void OKButton_Click(object sender, EventArgs e)
{
     objSecondFormParams.NetworkName = txtNetWorkName.Text;
     objSecondFormParams.SecurityType= cbSecurityType.SelectedValue;
     ...

    ((YourParentFormName)this.Owner).RefreshPerentList(objSecondFormParams);

    this.Close();
}

程序 2:在第二个表单中添加事件

您还可以在第二个表单中添加事件,然后在父表单中调用其监听器以刷新或将项目添加到您的listview

关于c# - 将数据从一个表单传递到另一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31216961/

相关文章:

javascript - 在同一页面上呈现新建/创建和编辑/更新表单

forms - Yesod 中动态大小的字段列表

php - javascript 应该返回 false,表单仍然有效并且 php 在表中插入数据

javascript - 基金会揭示模态 : how can I return to parent modal after closing modal-in-a-modal

C# Web 请求 w RestSharp - "The request was aborted: Could not create SSL/TLS secure channel"

c# - 委托(delegate)类型和事件处理程序类型有什么区别?

c# - Func<MyType> 隐式转换为 MyType

C# 反序列化列表计数为零

javascript - 第二次单击时 jQuery 工具叠加层不显示

java - JDialog:如何禁用模态对话框的 ESC 键?