I am looking for a control (or suggestions on building my own) for a .NET 2.0 (windows) app that works like the address box in the Outlook mail window (bee below)
Outlook Address Control http://content.screencast.com/users/Ryan_Farley/folders/Jing/media/a511142b-dd04-4885-ad1e-f2582c201723/2009-03-12_2116.png
The control basically works where each e-mail address is like an item in the text area. I don't care so much about letting the user also type into this area like you can in Outlook. I just want to be able to add these complete strings (e-mail addresses) to the text area, or list, and the user can select them (but not edit) and can delete or backspace through the list to delete entire items (e-mail addresses).
Anyone know of a control out there that does this? Any suggestions for building my own? (or anyone know what you even call this control so I know what to google?)
Here's some code to get you started.
using System.Text;
using System.Windows.Forms;
using System;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "mail@mail.com; mail2@mail.com; mail3@mail.com";
}
private void textBox1_Click(object sender, EventArgs e)
{
int nextSpaceIndex = textBox1.Text.Substring(textBox1.SelectionStart).IndexOf(' ');
int firstSpaceIndex = textBox1.Text.Substring(0, textBox1.SelectionStart).LastIndexOf(' ');
nextSpaceIndex = nextSpaceIndex == -1 ? textBox1.Text.Length : nextSpaceIndex + textBox1.SelectionStart;
firstSpaceIndex = firstSpaceIndex == -1 ? 0 : firstSpaceIndex;
textBox1.SelectionStart = firstSpaceIndex;
textBox1.SelectionLength = nextSpaceIndex - firstSpaceIndex;
}
}
}
This will, when you click on an email address, select the entire email address. I'm not sure if this is the functionality you're going for (it sounds like it is, though), but it'll get you started. If you want to do other things beyond having click functionality, hook into the other events offered by TextBox
.
.NET 2.0 Windows Forms already has that, and it's simply a MaskedTextBox.
But in order to provide maximum input validity such as email address, you can add regular expression (Regex) validation when the text in the MaskedTextBox is changed.
Update:
To provide more customization such as multiple email address, you can also use MaskedTextBox combined with RichTextBox, since there's no native Windows Forms implementation of the exact functionality of Outlook email address input control.
I also have done this, by capturing user's current cursor when the RichTextBox control got focus, and then directly masking the input using additional MaskedTextBox generated on the fly at runtime, displayed on top of the RichTextBox. Therefore there can be multiple MaskedTextBoxes when the email addresses in entered more than one.
I'm not saying that this is an easy task, but this is doable.
See this:
MSDN documentation on MaskedTextBox