我有一个包含邮件收件人地址的组合框。当它被删除时,用户会看到他发送邮件的所有地址列表。我希望组合框在其文本字段中追加地址,而不是在那里替换所有的文本。C#电子邮件地址组合框
举例来说,我有一个电子邮件列表[email protected],[email protected],[email protected]在combobox的列表和[email protected]是在combobox.Text。当我从cbox的列表中选择[email protected]时,我想将combobox.Text变成“[email protected],[email protected]”,但文本只是变成了[email protected]。
This is the combobox: +----------------+ +----------------+ | [email protected] |V| | [email protected] |V| +----------------+ +----------------+ | [email protected] | | [email protected] | | [email protected] | trying to select this brings | [email protected] | | [email protected] | which is not desired | [email protected] | +----------------+ +----------------+ When someone clicks on [email protected] or [email protected] or [email protected] the editable field becomes that value. For example, after selecting [email protected] it will become +----------------+ | [email protected] |V| +----------------+ I want combobox to append values and not just select them. So I want it to show +------------------------------+ | [email protected], [email protected] |V| +------------------------------+ instead of just +----------------+ | [email protected] |V| +----------------+
我写了这个代码(这只是一个组合框,按钮和按键的形式,形式的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);
}
}
我不知道你想达到什么,这似乎很荒谬 – V4Vendetta
我想追加地址,而不是在combobox中的可编辑字段中替换它们。可能吗? – Blablablaster
为什么不把它们追加到像文本框这样的不同控件上,因为这会混淆[email protected]会发生什么,它会从列表中消失,还是在单击它时会再次追加。这不是在任何情况下都要使用组合框的方式 – V4Vendetta