MaskedTextBox is a poor choice for constraining an e-mail address, because there could be an arbitrary number of characters before and after the @ symbol. Also, some punctuation chars are accepted but others are not. A better solution is to make a normal TextBox, and give it a KeyPress event handler. Here is a "good enough" C# version that will reject most bad typing:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
^^TextBox tb = sender as TextBox;
^^if (tb == null)
^^^^throw new InvalidProgramException("KeyPress validator expecting a TextBox.");
^^
^^char ch = e.KeyChar;
^^int atSignIndex = tb.Text.IndexOf("@");
^^int newCharAt = tb.SelectionStart + tb.SelectionLength;
^^bool inDomain = (atSignIndex >= 0) && (newCharAt > atSignIndex);
^^
^^// "Control" chars are needed for navigating; e.g. BACKSPACE, TAB.
^^if (char.IsLetterOrDigit(ch) || char.IsControl(ch) ||
^^^^ch == '-' || ch == '_' || ch == '.')
^^^^return; // OK: accept this char.
^^else if (ch == '+' || ch == '$' || ch == '/')
^^{
^^^^// NOTE: We omitted several other chars that are rarely used.
^^^^// See http://en.wikipedia.org/wiki/E-mail_address for other chars you could list above.
^^^^if (inDomain)
^^^^^^e.Handled = true; // Reject this char after "@".
^^^^return;
^^}
^^else if (char.IsPunctuation(ch))
^^{
^^^^if (ch == '@' && atSignIndex < 0)
^^^^^^return; // OK: No "@" yet.
^^^^e.Handled = true; // Reject other punctuation.
^^}
^^else
^^^^e.Handled = true; // Reject any other char.
}
~ToolmakerSteve