Martin Tollkühn
Martin Tollkühn

Reputation: 33

Delete TextBox with Button in separate Class

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

Upvotes: 2

Views: 197

Answers (3)

Martin Tollkühn
Martin Tollkühn

Reputation: 33

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

Upvotes: 0

Mansur Qurtov
Mansur Qurtov

Reputation: 772

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

Upvotes: 0

Amit Verma
Amit Verma

Reputation: 2488

Pass text box object as parameter.

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

Upvotes: 1

Related Questions