c# - 在 C# .net 中单击第二个表单上的按钮时,如何从第二个表单获取主表单中的字符串数据?

标签 c# .net winforms

一开始我以为这对我来说不会有问题,但现在我想不通了。所以, 当我单击主窗体中的 Button1 时,窗体 2 打开。 Form2是简单的数字键盘,用户可以输入一些数据。在form2上也是保存。当用户单击它时,输入的值应该传递到主窗体,从那时起,主窗体中必须发生一些事件,其中包含来自 form2 的数据。您能给我一些例子或任何帮助吗?谢谢!

 // code from main form to create form2
 private void button1_Click(object sender, EventArgs e)
    {
        // Create a new instance of the Form2 class
        Form2 settingsForm = new Form2();

        // Show the settings form
        settingsForm.Show();

        string val = settingsForm.ReturnValue1;            

        MessageBox.Show(val);
    }

//button save on form2
private void button13_Click(object sender, EventArgs e)
    {
        this.ReturnValue1 = "Something";
        this.ReturnValue2 = DateTime.Now.ToString(); //example
        this.Close();
        //after this, some event should happen in main form !
    }

最佳答案

有很多解决方案可以满足您的需求;但我认为其中之一可以解决您的问题。

1- 简单易用:在 Form2 中使用公共(public)属性,在单击 buttonSave 时初始化它们,并在 中访问它们表格1:

表格2:

public partial class Form2 : Form {
    public Form2() {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e){
        YourDate = "something";
        Close();
    }

    public object YourDate { get; private set; }
}

表格1:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e){
        var f2 = new Form2();
        f2.ShowDialog();
        var data = f2.YourDate;
    }
}

2- 更好的方法是使用更灵活且更专业的编程友好的事件:

表格2:

public partial class Form2 : Form {

    public Form2() {
        InitializeComponent();
    }

    // create an event of Action<T> which T is your data-type. e.g. in this example I use an object. 
    public event Action<object> SaveClicked;

    // create an event invocator, to invoke event whenever you want
    protected virtual void OnSaveClicked(object data){
        var handler = SaveClicked;
        if (handler != null)
            handler(data);
    }

    private void button1_Click(object sender, EventArgs e){
        // prepare your data here, -object, or string, or int, or whatever it is
        var data = PrepareYourDataHere;
        // invoke the event
        OnSaveClicked(data);
        Close();
    }
}

表格1:

public partial class Form1 : Form {

    public Form1() {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e){
        // create an instance of Form2
        var f2 = new Form2();
        // add an event listener to SaveClicked event -which we have declared it in Form2
        f2.SaveClicked += f2_SaveClicked;
        f2.Show();
        // or: f2.ShowDialog();
    }

    void f2_SaveClicked(object obj) {
        // get data and use it here...
        // any data which you pass in Form2.OnSaveClicked method, will be accessible here
    }
}

更新:

如果您想在 form2 关闭后触发 form1 中的某些事件,只需向 Form2.FormClosed 事件添加一个监听器即可:

// code from main form to create form2
private void button1_Click(object sender, EventArgs e) {
    // Create a new instance of the Form2 class
    Form2 settingsForm = new Form2();
    settingsForm.FormClosed += SettingFormClosed;

    // Show the settings form
    settingsForm.Show();

    string val = settingsForm.ReturnValue1;            

    MessageBox.Show(val);
}

void SettingFormClosed(object sender, FormClosedEventArgs e) {
    // this method will be called automatically when form2 closed
}

关于c# - 在 C# .net 中单击第二个表单上的按钮时,如何从第二个表单获取主表单中的字符串数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22276709/

相关文章:

c# - 尝试从 DataGridView 计算 xml 值

c# - 如何在 C# 中停靠 Windows 窗体?

c# - 带有附加属性的疑问

c# - 将 datetime c++ 结构(以字节形式接收)转换为 c# datetime 实例

c# - 使用 C# 从 UIElement 获取 WPF 屏幕截图 JPG

c# - 如何读取 C# 测试项目中的 app.config 值

c# - MSTest 未使用我的项目目标框架

.net - 监控数据库中的表

c# - 什么是 C# 开发人员学习 Android 开发的理想方法?

c# - 异常 "Cannot access a disposed object"来自非用户代码