【问题标题】:Show a specific part of an Item when selected in a ComboBox在组合框中选择时显示项目的特定部分
【发布时间】:2019-07-07 14:28:15
【问题描述】:

我有一个显示每个国家/地区名称及其移动代码的组合框。
我希望 ComboBox 在选择项目时仅显示国家代码而不是其名称。
比如我有Albania +355;当我选择它时,我只想看到代码 (+355)。
这些项目写在Items 集合属性中。
有没有代码可以做到这一点?

我没有尝试太多,因为我不知道如何做到这一点,我唯一尝试过的是comboBox6.Text = "";,但这会删除整个文本。

【问题讨论】:

  • 嗨,Simon,欢迎来到 Stackoverflow,您能否提供将值设置为 comboBox6 的代码
  • @PrasadTelkikar 但我已经在 (items) 属性中写入了所有项目。

标签: c# winforms combobox


【解决方案1】:

我的建议是构建一个包含您需要的所有值的类,然后使用List<class> 设置ComboBox.DataSource 属性。

DisplayMember 设置为要用作列表可见字符串的属性名称,将 ValueMember 设置为要用作列表可见字符串的属性名称ComboBox.Text

然后你可以将ComboBox.Text设置为ComboBox.SelectedValue.ToString()SelectedValue,因为SelectedItem,是一个object,你需要转换它。你知道它是一个字符串,所以只需转换它)。
ComboBox.Text 必须在一小段延迟后设置(在控件已经自行更改文本之后)。这可以通过调用Control.BeginInvoke() 来完成,并使用一个将文本更改为我们需要的操作。

使用简单的类对象来存储值:

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DisplayMember = "CountryAndCode";
    comboBox1.ValueMember = "Code";
    comboBox1.DataSource = countryCodes;
    comboBox1.SelectedIndex = -1;

    comboBox1.SelectedIndexChanged += (o, ev) => {
        if (comboBox1.SelectedIndex < 0) return;
        comboBox1.BeginInvoke(new Action(() => {
            comboBox1.Text = comboBox1.SelectedValue.ToString();
        }));
    };
}

public List<CountryCode> countryCodes = new List<CountryCode>() {
    new CountryCode() { Country = "USA", Code = "+1" },
    new CountryCode() { Country = "Canada", Code = "+1" },
    new CountryCode() { Country = "Argentina", Code = "+54"},
    new CountryCode() { Country = "Brasil", Code = "+55"}
};

类定义:

public class CountryCode
{
    public CountryCode() { }
    public string Country { get; set; }
    public string Code { get; set; }
    public string CountryAndCode => this.ToString();

    public override string ToString() => this.Country + (char)32 + this.Code;
}

注意comboBox1.SelectedItem是一个CountryCode类对象。您只需将其转换为 CountryCode 即可读取此项目的所有属性。

var item = comboBox1.SelectedItem as CountryCode;
string selectedName = item.Country;
string selectedCode = item.Code; // etc.

如果您想保留简单的字符串格式,可以使用List&lt;string&gt;
这或多或少是一样的(除了灵活性的损失)。您可以从 ComboBox.SelectedItem 中提取国家代码,使用 string.LastIndexOf() 来确定 + 字符在字符串中的位置:

private List<string> simpleCountryCodes = new List<string>() {
    "USA +1", "Canada +1", "Argentina +54", "Brasil +55"
};

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DataSource = simpleCountryCodes;
    comboBox1.SelectedIndex = -1;

    comboBox1.SelectedIndexChanged += (o, ev) => {
        if (comboBox1.SelectedIndex < 0) return;
        string item = comboBox1.SelectedItem.ToString();
        string code = item.Substring(item.LastIndexOf("+"));
        comboBox1.BeginInvoke(new Action(() => { comboBox1.Text = code; }));
    };
 }

如果您决定将 List&lt;string&gt; 转换为 List&lt;CountryCode&gt;,您可以使用 LINQ 生成 CountryCode 对象像这样的字符串:

var countryCodes = new List<CountryCode>();
countryCodes.AddRange(simpleCountryCodes.Select(c => new CountryCode() {
    Code = c.Substring(c.LastIndexOf("+")),
    Country = c.Substring(0, c.IndexOf("+") - 1)
}).ToArray());

在这两种情况下都是这样工作的:

【讨论】:

    【解决方案2】:

    在您的表单上放置一个文本框和一个组合框,然后这样做:

    public partial class Form1 : Form
    {
        private Dictionary<string, string> countryCodes = new Dictionary<string, string>()
        {
            { "Albania", "Albania +355" },
            { "USA", "USA +1" },
            { "Iran", "Iran +98" },
        };
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
            comboBox1.Items.AddRange(countryCodes.Values.ToArray());
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = countryCodes.Where(x => x.Value == comboBox1.SelectedItem.ToString()).First().Key;
        }
    }
    

    选择项目后:

    编辑: 如果您想在文本框中查看代码,可以将 countryInfo 更改为:

    private Dictionary<string, string> coubtryCodes = new Dictionary<string, 
    string>()
    {
         { "+355", "Albania +355" },
         { "+1", "USA +1" },
         { "+98", "Iran +98" },
    };
    

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 2011-08-30
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多