簡體   English   中英

Winform ComboBox-無法設置默認值

[英]Winform ComboBox- Default Value Cannot Be Set

我有一個簡單comboBox在我的控制Winform 我想將comboBox的項目之一設置為將在表單加載時顯示的默認項目:

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

我的確在comboBox中有0分鍾的項目,但是從加載開始,該字段仍然為空。
有任何想法嗎?

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;
        }
    }
}

代替設置Combo.SelectedItem ,而是設置Combo.SelectedIndex

duration_ComboBox.SelectedIndex = duration_ComboBox.Items.IndexOf(“ 0分鍾”);

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM