Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Control like Outlook's E-mail Address Text Control

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?)

like image 668
Ryan Farley Avatar asked Nov 27 '22 12:11

Ryan Farley


1 Answers

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 = "[email protected]; [email protected]; [email protected]";
        }

        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.

like image 136
nsantorello Avatar answered Dec 10 '22 22:12

nsantorello