0

First of all, I know that there are probably better solutions and I am very willing to listen to those solutions. I have searched around the internet looking for a problem like this but am unable to find one that works so far. I have multiple textboxes on a form, when the user clicks on the textbox I run a method that finds out which textbox is focused, gets the tag of that textbox and the name of the textbox both as strings (TextboxX and test). My goal is to mask the textboxes with for example 'Email' and when the user clicks on the textbox the textbox forecolor changes to black and the textbox text is null, with as little code as possible. Here is the code i have for that.

public void masked()
    {
        if (textboxX.Text == test)
            {
                textboxX.ForeColor = Color.Black;
                textboxX.Text = "";
            }
            else
            {
                textboxX.Select(0, textboxX.Text.Length);
            }          
    }

When the textbox is clicked this is what it does currently.

private void txtSignup_email_Click(object sender, EventArgs e)
    {
        textboxX = txtSignup_email;
        test = "Email";
        masked();
    }

The reason for this is that I have 7 textboxes, it will save me about 14 lines of code that is actually not necessary. Thanks in advance.

Matthew Mitchell
  • 298
  • 2
  • 14
  • 2
    You can point all your text boxes to the same eventhandler, which means they all use the same validation code. – ainwood Aug 31 '17 at 06:35
  • do you do anything else in the click events? beside the masking? If yes how does it differ from textbox to textbox? – Mong Zhu Aug 31 '17 at 06:40

1 Answers1

2

OK there are a few things that can be done better.

First of all you can use Password instead of TextBox which is automatically masked and can't be seen if I understand you requirement correctly.

Second thing is what ainwood said in the comment you can point all clicked or focused events of your textboxes to a single method. Event handler methods have two parameters sender and e. The former is of type object and shows who called this method in your case you can cast is as a TextBox and that will be calling textbox. The cast operation is like this:

var textBox = sender as TextBox;
if (textBox != null)
{
    //Do what you want with textBox here
}

Also if you use the new C# 7 you can do (Which is not any different internally just better to read):

if (sender is TextBox textBox)
{
    //Do what you want with textBox here
}
Emad
  • 3,293
  • 3
  • 27
  • 41
  • using [VS 2015 and framework 4.6 which would correspond to C#6](https://stackoverflow.com/a/247623/5174469) I get a compiler error: "textBox not available in the current context" – Mong Zhu Aug 31 '17 at 07:39
  • but if I look at [the list of new features in C#7](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#pattern-matching) I can find the syntax there – Mong Zhu Aug 31 '17 at 07:43
  • 1
    Yes I checked again and you are right. But I thought I had used it in C# 6. Thank you. I also edited the answer to broaden the availability :) – Emad Aug 31 '17 at 07:44
  • 1
    I think you should leave the other possibility also in your answer. Not everybody knows this nice new feature. Just mark it as C#7 way. :) – Mong Zhu Aug 31 '17 at 07:45