martin kandrik
martin kandrik

Reputation: 3

Editing text of multiple textboxes with function

I`m not able to find answer. I have several text boxes and if user enters/leaves them I check if he has changed something and the code below works.

private void txtRegNazov_Enter(object sender, EventArgs e)
{
    if (txtRegNazov.Text == "n/a")
    {txtRegNazov.Text = "";}
}

private void txtRegNazov_Leave(object sender, EventArgs e)
{
    if (txtRegNazov.Text == "")
    {txtRegNazov.Text = "n/a";}
}

I would like to create a function like

public void ClearFieldDataByEnter() 
{
  thisHlep.text = "";
}

public void FieldDataByleave() 
{
  thisHelp.text = "n/a";
}

And then in every field event would be something like:

private void txtRegNazov_Enter(object sender, EventArgs e)
{
   thisHelp.Name = name of this txtBox; 
   ClearFieldDataByEnter();
}

This is only an easy example of what I want ... I am looking for principe ... and I`m still new to C#.

Thank you

Upvotes: 0

Views: 47

Answers (2)

Sjips
Sjips

Reputation: 1258

You can use the sender parameter, like this:

private void txtRegNazov_Enter(object sender, EventArgs e)
{
    ClearFieldDataByEnter(sender);
}

private void txtRegNazov_Leave(object sender, EventArgs e)
{
    FieldDataByleave(sender);
}

public void ClearFieldDataByEnter(object text) 
{ 
    textBox = text as TextBox;
    if (textbox == null)
       return;
    if (textbox.Text == "n/a")
    {
         textbox.Text = String.Empty;
    }
}

public void FieldDataByleave(object text) 
{
    textBox = text as TextBox;
    if (textbox == null)
       return;
    if (String.IsNullOrEmpty(textbox.Text))
    {
       textbox.Text = "n/a";
    }
}

Upvotes: 0

dario
dario

Reputation: 5259

Rember that the "sender", in this case, is the actual TextBox.

TextBox txtSender = (TextBox)sender;

Upvotes: 1

Related Questions