Detecting user selection in ComboBox.DropDownClosed in winforms

a 夏天 提交于 2019-12-06 15:08:42

I tried my best to see if I could make this work, but I'll be damned if I wasn't pulling my hair out after 30 minutes of it. If you were open to a little bit of change, you could try using the ListBox control. It has a "TopIndex" property that scrolls to the index you want but never actually makes a selection. See below code:

    private void listBox_SelectedIndexChanged(object sender, EventArgs e) {
        ListBox lbx = sender as ListBox;
        if (lbx != null) {
            switch (lbx.Name) {
                case "listBox1": 
                    listBox2.TopIndex = lbx.SelectedIndex; 
                    listBox2.SelectedIndex = -1; 
                    listBox3.TopIndex = 0; 
                    listBox4.TopIndex = 0; 
                    break;
                case "listBox2": 
                    listBox3.TopIndex = lbx.SelectedIndex; 
                    listBox3.SelectedIndex = -1;
                    listBox4.TopIndex = 0;
                    break;
                case "listBox3": 
                    listBox4.TopIndex = lbx.SelectedIndex; 
                    listBox4.SelectedIndex = -1; 
                    break;
            }
        }
    }

with 4 different ListBox controls all using that on their SelectedIndexChanged event. Let me know if that works or not. If not I can go back to the ComboBoxes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!