单独删除带有 Button 的 TextBox Class

Delete TextBox with Button in separate Class

我想使用单独的 class 'Delete' 从 TextBox 'txtName' 中删除值,方法是 .resetText()。 我无法在这个单独的 class 中访问我的文本框。 我该如何解决这个问题?

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

    public void butDelete_Click(object sender, EventArgs e)
    {
        Delete delete = new Delete();
    }
}


class Delete
{
    public Delete()
    {
        txtName.ResetText();
    }
}

将文本框对象作为参数传递。

class Delete
{
    public Delete(TextBox txtName)
    {
        txtName.ResetText();
    }
}

发送带有删除方法参数的文本框控件:


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

    public void butDelete_Click(object sender, EventArgs e)
    {
        Delete delete = new Delete();
        delete.Delete(txtName);
    }
}


class Delete
{
    public Delete(Control control)
    {
         var txtBox = control as TextBox;
         if (txtBox == null)
            return;
         txtBox.ResetText();
    }
}

已解决

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

    public void butDelete_Click(object sender, EventArgs e)
    {
        Delete delete = new Delete(txtName);
    }
}


class Delete
{
    public Delete(TextBox txtName)
    {
        txtName.ResetText();
    }
}