繁体   English   中英

C# email 地址 combobox

[英]C# email address combobox

我有一个 combobox ,其中包含邮件收件人的地址。 当它被下拉时,用户会看到他曾经发送过邮件的所有地址的列表。 我希望 combobox 到 append 地址在它的文本字段中,而不是替换那里的所有文本。

So for example, I have a list of emails aaa@mail.com, bbb@mail.com, ccc@mail.com in combobox's list and aaa@mail.com is in combobox.Text. When I select bbb@mail.com from cbox's list I want to combobox.Text to became "aaa@mail.com, bbb@mail.com" but Text just becames bbb@mail.com.

This is the combobox:
+----------------+                                     +----------------+
| aaa@mail.com |V|                                     | aaa@mail.com |V|
+----------------+                                     +----------------+
| aaa@mail.com   |                                     | aaa@mail.com   |
| bbb@mail.com   |    trying to select this brings     | bbb@mail.com   | 
| ccc@mail.com   |       which is not desired          | ccc@mail.com   |
+----------------+                                     +----------------+

When someone clicks on aaa@mail.com or bbb@mail.com or ccc@mail.com the editable field becomes that value. For example, after selecting bbb@mail.com it will become 
+----------------+
| bbb@mail.com |V|
+----------------+

I want combobox to append values and not just select them. So I want it to show
+------------------------------+
| aaa@mail.com, bbb@mail.com |V|
+------------------------------+

instead of just 
+----------------+
| bbb@mail.com |V|
+----------------+

我写了这段代码(它只是一个带有 combobox 的表单,按钮和按钮,设置了表单的 FormClosed 事件,设置了按钮的 Click 事件,并设置了组合框的事件 selectedIndexChanged 和 TextChanged)但它不像我预期的那样工作。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        cboxFrom.Tag = string.IsNullOrEmpty(Settings.Default.emailFrom) ? "" : Settings.Default.emailFrom;
        cboxFrom.Text = (string)cboxFrom.Tag;
        if (Settings.Default.emailFroms == null)
            Settings.Default.emailFroms = new System.Collections.Specialized.StringCollection();
        //cboxFrom.DataSource = Settings.Default.emailFroms;
        foreach (string s in Settings.Default.emailFroms)
        cboxFrom.Items.Add(s);
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        Settings.Default.Save();
    }

    private void cboxFrom_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ComboBox cbox = (ComboBox)sender;
            string addr = (string)cbox.Items[cbox.SelectedIndex];
            if (addr != null && cbox.Tag != null && !(cbox.Tag as string).Contains(addr))
            {
                if (cbox.Text.Trim().Length == 0)
                    cbox.Text = addr;
                else
                    cbox.Text = cbox.Tag + ", " + addr;
                cbox.Tag = cbox.Text;
            }
            else
                cbox.Text = (string)cbox.Tag;
        }
        catch { }
    }

    private void cboxFrom_KeyPress(object sender, KeyPressEventArgs e)
    {
        ComboBox cbox = (ComboBox)sender;
        cbox.Tag = cbox.Text;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string[] addrs = cboxFrom.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        foreach(string s in addrs)
        if(!Settings.Default.emailFroms.Contains(s.Trim()))
            Settings.Default.emailFroms.Add(s);
    }
}

我这里只有一个建议。

而不是附加您的 comboBox 文本。 为什么不添加一个文本框和另一个按钮,单击时将 comboBox 中的选定 email 地址添加到文本框? 添加 textBox.Text 属性会更简单,它还允许用户在将其添加到发送列表之前确保他选择了正确的 email。

据我了解,按照您的方式进行操作有很多 email,向下滚动用户可能会做出错误的选择,然后它会自动添加到您的 comboBox.Text 中,用户无法退出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM