[英]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.