【问题标题】:Winform ComboBox- Default Value Cannot Be SetWinform ComboBox-无法设置默认值
【发布时间】:2016-08-05 23:07:39
【问题描述】:

我的Winform 上有一个简单的comboBox 控件。 我想将组合框的其中一项设置为将在表单加载时显示的默认项:

duration_ComboBox.SelectedItem = duration_ComboBox.Items.IndexOf("0 minutes");
        duration_ComboBox.Text = duration_ComboBox.SelectedText;  

我在组合框中确实有 0 分钟 项,但在加载后该字段仍为空。
有什么想法吗?

【问题讨论】:

    标签: c# winforms combobox default-value


    【解决方案1】:

    设置Combo.SelectedIndex,而不是设置Combo.SelectedItem

    duration_ComboBox.SelectedIndex = duration_ComboBox.Items.IndexOf("0 分钟");

    希望这会有所帮助。

    【讨论】:

    • 谢谢。但这不是第一个索引。我想为每个文本项设置它。
    • 然后尝试使用 Combo.SelectedIndex = Combo.Items.IndexOf("Text");
    【解决方案2】:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Forms;
    
    namespace SOFAcrobatics
    {
        public partial class ComboBoxTesting : Form
        {
            public ComboBoxTesting()
            {
                this.InitializeComponent();
            }
    
            private void ComboBoxTesting_Load(object sender, EventArgs e)
            {
                List<String> items = new List<String>()
                {
                    "0 minutes",
                    "1 minutes",
                    "2 minutes"
                };
    
                foreach (String item in items)
                {
                    this.comboBox1.Items.Add(item);
                }
    
                this.comboBox1.SelectedIndex = 0;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多