[英]Winform ComboBox- Default Value Cannot Be Set
I have a simple comboBox
control on my Winform
. 我有一个简单comboBox
在我的控制Winform
。 I would like to set one of the items of comboBox, as the default item that will be shown on form load: 我想将comboBox的项目之一设置为将在表单加载时显示的默认项目:
duration_ComboBox.SelectedItem = duration_ComboBox.Items.IndexOf("0 minutes");
duration_ComboBox.Text = duration_ComboBox.SelectedText;
I do have the 0 minutes item in the comboBox, but on from load the field remains empty. 我的确在comboBox中有0分钟的项目,但是从加载开始,该字段仍然为空。
Any ideas? 有任何想法吗?
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;
}
}
}
Instead of setting Combo.SelectedItem
, set Combo.SelectedIndex
. 代替设置Combo.SelectedItem
,而是设置Combo.SelectedIndex
。
duration_ComboBox.SelectedIndex = duration_ComboBox.Items.IndexOf("0 minutes"); duration_ComboBox.SelectedIndex = duration_ComboBox.Items.IndexOf(“ 0分钟”);
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.