Reputation: 2661
I have a simple comboBox
control on my Winform
.
I would like to set one of the items of comboBox, as the default item that will be shown on form load:
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.
Any ideas?
Upvotes: 0
Views: 260
Reputation: 1147
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;
}
}
}
Upvotes: 1
Reputation: 164
Instead of setting Combo.SelectedItem
, set Combo.SelectedIndex
.
duration_ComboBox.SelectedIndex = duration_ComboBox.Items.IndexOf("0 minutes");
Hope this helps.
Upvotes: 0