簡體   English   中英

選擇另一個時取消選擇列表框

[英]Unselect listbox when other one is selected

我有兩個類似此照片的列表框:

在此處輸入圖片說明

如您所見,雙方都被選中。 我想知道當單擊另一個列表時如何取消選擇一個列表。 那可能嗎?

以下代碼可能會幫助您解決問題,但是您也可以在函數的selectedIndexChanged事件上添加clearSelected函數。

    namespace StackoverFlow4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1.Text = "UnSelected";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.Text = "Selected";
            pictureBox1.BackgroundImage = StackoverFlow4.Properties.Resources.tick;
            button1.Text = "Clear Selected";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.ClearSelected();
            listBox2.ClearSelected();
            button1.Text = "UnSelected";
            pictureBox1.BackgroundImage = StackoverFlow4.Properties.Resources.cross;
            pictureBox2.BackgroundImage = StackoverFlow4.Properties.Resources.cross;
        }

        private void listBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            pictureBox2.BackgroundImage = StackoverFlow4.Properties.Resources.tick;
            button1.Text = "Clear Selected";
        }
    }
}

首先兩者都未被選中 在此處輸入圖片說明 清除列表(如果要選擇) 在此處輸入圖片說明 如果兩者都選中,則清除兩者 在此處輸入圖片說明

我在這里太新了,為壞處和物品感到抱歉

您只需要在相應的列表框上為SelectedIndexChange添加一個事件偵聽器,如下所示:

    // Have Box1 deselect any Box2 inputs on index change.
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
        listBox2.ClearSelected();
    }

    // Have Box2 deselect any Box1 inputs on index change.
    private void listBox2_SelectedIndexChanged(object sender, EventArgs e) {
        listBox1.ClearSelected();
    }

要檢出的其他資源: 取消選擇列表框中的所有項目

暫無
暫無

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

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