如何访问另一个类的方法?

时间:2018-06-04 01:30:34

标签: c# winforms methods

我有一个WinForms应用程序。在主窗体中,我编写了一个方法,它将清除以任何形式作为参数传递的所有TextBox。我想从另一种形式调用此方法。以下代码是经过多次试用/错误并浏览本网站后我想出的代码。每次新表单的清除所有按钮被点击良好练习时,是否实例化主表单的新版本?如果我要使用它自己的清除所有按钮制作另一个表单,我将不得不通过类似的练习实例化一个新的主表单(除非我使该方法静态)?任何人都可以建议从不同的形式访问一种形式的方法的替代方法吗?提前谢谢了。

编辑:我知道让方法静态是一个简单有效的解决方案,但我很好奇使用非静态方法。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public  void ClearAll(Form formToClear) //CLEAR TEXTBOXES
    {
        foreach (var box in formToClear.Controls.OfType<TextBox>())
        {
            box.Text = "";
        }
    }

 }

public partial class NewItemForm : Form
{
    public NewItemForm()
    {

        InitializeComponent();
    }

     private void clearAllButton_Click(object sender, EventArgs e)
    {
        Form1 mainForm=new Form1();
        mainForm.ClearAll(this);
    }
}

3 个答案:

答案 0 :(得分:2)

您几乎肯定会创建一个带静态函数的静态实用程序类。这将通过防止创建不必要的Form1实例来保留内存。根据Form类的大小和其中包含的对象/变量,创建一个新实例只是为了使用该类中的1个函数,最终可能导致大量内存随着时间的推移而丢失。静态类中的静态方法可以防止这种情况发生,因为静态方法仅在进程的生命周期中定义/实例化一次,而不是每个实例一次。

您可能会选择以下内容:

internal static class FormUtils
{
    internal static void ClearAllTextBoxes(Form form)
    {
        if (form == null)
            return;
        if (form.Controls.Count <= 0)
            return;
        foreach (var box in form.Controls.OfType<TextBox>())
        {
            box.Clear();
        }
    }
}

然后,该函数将使用如下:

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

    private void clearAllButton_Click(object sender, EventArgs e)
    {
        FormUtils.ClearAllTextBoxes(this);
    }
}

答案 1 :(得分:2)

您不必将ClearAll方法设为静态。如果您保留对主表单的全局引用就足够了。你可以在Program.cs中完成。这不是最好的方法。

static class Program {
    public static Form1 TheForm;

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        TheForm = new Form1();
        Application.Run(TheForm);
    }
}

再次!仅仅因为它可以完成,并不意味着我会鼓励你这样做。这不符合OOP的精神。

如果您想要访问Form1的方法的唯一原因是清除TextBoxes,那么我建议创建一个中间类:

public class InterForm : Form
{
    public void ClearAll() //CLEAR TEXTBOXES
    {
        foreach (var box in this.Controls.OfType<TextBox>())
        {
            box.Text = "";
        }
    }
 }

所有其他表单都应该继承自InterForm

答案 2 :(得分:2)

您可以在此处使用Event的概念。

您想要调用主表单方法的另一个表单表单应该是一个事件,

在创建此表单的实例时(我猜您只是从主表单创建此表单的实例),您可以将此表单的事件订阅到您的目标方法。

因此,无论何时您需要调用主表单的方法(从您的其他表单),您都可以举起该事件。

见下面的示例代码。

假设Form1是您的主要表格

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

    void f2_ClearTextBoxOfForm(Form targetForm)
    {
        foreach (Control control in targetForm.Controls)
        {
            if (control is TextBox)
                ((TextBox)control).Text = string.Empty;
        }
    }

    private void btnShowForm2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.ClearTextBoxOfForm += f2_ClearTextBoxOfForm;
        f2.Show();
    }
}

Form2是您想要清除Form1

所有文本框的另一种形式
public delegate void ClearTextBoxEventHandler (Form targetForm);
public partial class Form2 : Form
{
    public event ClearTextBoxEventHandler ClearTextBoxOfForm;
    public Form2()
    {
        InitializeComponent();
    }

    private void btnClearTextBox_Click(object sender, EventArgs e)
    {
        if (ClearTextBoxOfForm != null)
        {
            //here passing 'this' means we want to clear textBoxes of this form (Form2)
            //you can pass any Form's object of which you want to clear Textboxes
            ClearTextBoxOfForm(this);
        }
    }
}
相关问题