检测用户选择的ComboBox.DropDownClosed中的WinForms用户、ComboBox、WinForms、DropDownClosed

2023-09-04 04:02:56 作者:流年乱了沉浮

我在UI中有几个组合框每个有类似的条目(数字)的一个长长的清单。当用户选择从组合框之一中的项目,我知道用户将选择从其他组合框的项具有相似的值(但可能不相同)。因此,在用户选择的值,以避免强迫用户做大量的滚动,我谨以pre-滚动未来组合框下拉到最后选定值附近(在此下拉菜单还没有选择)。

I have several comboboxes in a UI each with a long list of similar entries (numbers). When the user selects an item from one of the comboboxes, I know the user will choose an entry with a similar value (but likely not the same) from the other comboboxes. Thus, after the user has selected a value, to help avoid forcing the user to do a lot of scrolling, I would like to "pre-scroll" the next combobox dropdown to the vicinity of the last selected value (when this dropdown does not already have a selection).

我已使用做到了这一点迄今

I have accomplished this thus far by using

combobox.SelectedItem = myLastSelectedItem;

一combobox.DropDown事件处理中。

inside a combobox.DropDown event handler.

然后,下拉关闭时,我需要能够检测如果用户点击从下拉与否的项目。如果用户没有点击一个项目,然后我必须选择的值恢复到它以前(无)。用户可以点击了myLastSelectedItem或不同的项目(因此,我不能只在当前所选项目比较myLastSelectedItem,因为他们可能是,即使用户没有点击一样)。 SelectedValueChange,SelectedIndexChange,框TextChanged的DropDownClosed活动结束后所有被解雇,所以我不能使用它们。 MouseClick事件没有得到所有被解雇。

Then, when the dropdown closes, I need to be able to detect if the user clicked on an item from the dropdown or not. If the user did not click on an item, then I must reset the selected value to what it was before (nothing). The user could have clicked on myLastSelectedItem or a different item (thus, I can't just compare the current selected item to myLastSelectedItem, as they may be the same even if the user did click). SelectedValueChange, SelectedIndexChange, TextChanged all get fired after the DropDownClosed event, thus I cannot use them. The MouseClick event does not get fired at all.

因此​​,如何检测用户在组合框中点击下拉菜单上的项目(而不是下拉关闭,因为用户点击它之外,或pressed逃脱)?

Thus, how do I detect that the user clicked on an item in a combobox dropdown (as opposed to the dropdown closing because the user clicked outside of it, or pressed escape)?

推荐答案

我尽我所能,看看我能做出这样的工作,但如果我不拉我的头发出来的30分钟后我会诅咒。如果你是开放的变化一点点,你可以尝试使用ListBox控件。它有一个TopIndex属性,滚动到你想要的,但从来没有真正进行选择的索引。见下面code:

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

4种不同的ListBox控制所有使用他们的SelectedIndexChanged事件。让我知道是否可行与否。如果不是我能回到组合框。

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.