中公共方法访问形式之间 [英] Access Of Public Method Between Forms

查看:125
本文介绍了中公共方法访问形式之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想可以访问到Form1的public方法上的另一种形式的窗体2如下。我有一个 textbox6 控制在Form1上并没有约束它的公共方法。但是我想通过窗口2到绑定如下。

I am trying to get access to Form1’s public method on another form Form2 as below. I have a textbox6 control on form1 and there is public method to bind it. But I want to bind it by form2 as below.

Form1中

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
    }

    public void amount_sum()
    {
        string connstr = " server=.;initial catalog=maa;uid=mah;pwd=mah";
        SqlConnection con = new SqlConnection(connstr);
        con.Open();
        string sql = " select sum(amount)as amount from method";
        SqlDataAdapter dap = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            textBox6.Text = Convert.ToString(ds.Tables[0].Rows[i]["amount"]);
        }    
    }
}

窗体2

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    f1.amount_sum();
    this.Close();
}

上面的方法调用是错误的。请建议如何纠正它。

The above method-call is wrong. Please suggest how to correct it.

我要绑定通过调用公共方法Form1的 textBox6 从窗体2的 Button_Click 事件处理程序的控制,而当窗体2被关闭,那么Form1的 textbox6 应绑定。这有可能由窗体2调用public方法?

I want to bind Form1’s textBox6 control from Form2's Button_Click event-handler by calling the public method, and when Form2 is closed, then Form1’s textbox6 should be bound. Is that possible by calling the public method from Form2?

推荐答案

在窗体2你有

Form1 f1 = new Form1();
f1.amount_sum();

这似乎是一个常见的​​错误,当你想通过应答形式之间创建一个新的Form1中。该关键字做到了这一点,它会创建一个新的Form1中,调用该方法,不显示窗体Form1的原始实例不受影响。

This seems to be a common mistake to create a new Form1 when you want to pass the answer back between forms. The new keyword does just that, it creates a new Form1, calls the method, does not show the form, the original instance of Form1 is unaffected.

我会表现出一定的步骤,如何解决这个问题。

I'll show some steps how to fix this.

1 - 通过Form 1以窗体2

你可以做的第一件事是简单地通过现有的Form 1以窗体2,使Form2的知道它应该更新其Form1上。

1 - Pass Form1 to Form2

The first thing you can do is to simply pass the existing Form1 to Form2 so that Form2 know which Form1 it should update.

public class Form2
{
    private readonly Form1 _form1;
    public Form2(Form1 form1)
    {
        _form1 = form1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _form1.amount_sum(); // now this updates the existing form1 instance
        this.Close();
    }
}

在Form1中

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2(this); // pass this form1 instance to form2
    f2.Show();
}

一个问题,这是创建Form 1和Form之间有很强的耦合。如果你想改变什么在Form1上很容易打破窗体2和周围的其他方式。

One issue with this is that is creates a strong coupling between Form1 and Form2. If you change something in Form1 it is easy to break Form2 and the other way around.

2 - 通过一个委托

而是通过整个Form1以窗体2,我们可以简单的通过一个委托到窗体2可以运行一个更新方法。这将创建Form 1和Form之间的耦合少,如果从Form3调用窗体2,你可以通过在Form3的更新方法,而不是和Form3将被更新。相同的窗体2可以不加修改地被重复使用。

2 - Pass a delegate

Instead of passing the whole Form1 to Form2 we can simple pass a delegate to an update method that Form2 can run. This creates less coupling between Form1 and Form2, if you call Form2 from Form3 you can pass in the update method of Form3 instead and Form3 will be updated. The same Form2 can be reused without modification.

public class Form2
{
    private readonly Action _ammountUpdater;
    public Form2(Action ammountUpdater)
    {
        _ammountUpdater = ammountUpdater;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _ammountUpdater(); // now this updates the existing form1 instance
        this.Close();
    }
}

在Form1中

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2(this.amount_sum); // pass the update method to form2
    f2.Show();
}

现在你可以改变 amount_sum 给私人,因为它现在是真的Form1的内政。

Now you can change amount_sum to private since it is now really an internal affair of Form1.

这篇关于中公共方法访问形式之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆