2

I'm starting to learn C#. I'm well versed in php and other web related languages. I'm starting off by creating a log in window, and one of the very first things I wanted to do was create a text box that can have a placeholder.

I assumed I would have to create a new class that is an extension of the TextBox and everything below seems to work fine EXCEPT, if I use designer to edit or move one of the text boxes, it adds a line to the InitializeComponent() section that specifies what color the text should be, and it is (naturally) overriding whatever my new class set it to be, right now it's OK since its the same color I want it to be. But if I ever wanted to change ALL placeholder textbox's color using my class, that line it adds will just keep that specific box the old color, that seems to totally go against the idea of object oriented programming, since now I'd have to manually go back and change all of my text boxes in the InitializeComponent() section instead of just the class, so I know I must be doing something wrong.

Here is my new class, ignore any other weird things I did that don't affect my problem, unless you wish to help with that too, but like I said I just started learning (yesterday in fact), my SelectionStart and Length aren't working, but I haven't gotten that far to see what's up with that.

class PlaceHolderTextBox : System.Windows.Forms.TextBox
{
    private string defaultText;
    public bool isPassword = false;
    public string DefaultText {
        get
        {
            return defaultText;
        }
        set
        {
            defaultText = value;
            addPlaceHolder();
        }
    }
    private void removePlaceholder()
    {
        Text = "";
        ForeColor = System.Drawing.Color.Black;
        if (isPassword)
        {
            PasswordChar = '\u2022';
        }
    }
    private void addPlaceHolder()
    {
        Text = defaultText;
        SelectionStart = 0;
        SelectionLength = 0;
        ForeColor = System.Drawing.Color.FromArgb(160, 160, 160);
        if (isPassword)
        {
            PasswordChar = '\0';
        }
    }
    private void placeHolderBox_KeyDown(object sender, System.EventArgs e)
    {
        if (this.Text == defaultText)
        {
            removePlaceholder();
        } 
    }
    private void placeHolderBox_KeyUp(object sender, System.EventArgs e)
    {
        if (this.Text == "")
        {
            addPlaceHolder();
        }
    }
    private void placeHolderBox_Leave(object sender, System.EventArgs e)
    {
        if (this.Text == "")
        {
            addPlaceHolder();
        }
    }
    public PlaceHolderTextBox()
    {
        KeyDown += new System.Windows.Forms.KeyEventHandler(placeHolderBox_KeyDown);
        KeyUp += new System.Windows.Forms.KeyEventHandler(placeHolderBox_KeyUp);
        Leave += new System.EventHandler(placeHolderBox_Leave);
    }
}

And this is the line its adding to my login.designer file

this.passwordBox.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(160)))), ((int)(((byte)(160)))), ((int)(((byte)(160)))));

After some googling I see there might be come kind of code that only designer uses, to say the placeholder color is the new "default" color, keeping designer from adding that line. But if that is true, I couldn't figure out where to put that in my code.

thegaffney
  • 340
  • 2
  • 10

0 Answers0