Delete TextBox with Button in separate Class

Viewed 113

I would like delete the value from TextBox 'txtName' by using a separate class 'Delete', with the methode .resetText(). i don't get access in this separate class to my TextBox. how can i solve this problem?

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();
    }
}
3 Answers

Pass text box object as parameter.

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

Send your textbox cotrol with parametr of your Delete method:


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();
    }
}

SOLVED

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();
    }
}
Related