C# 区分单击下拉列表三角形和单击其他任何位置的组合框

C# 区分单击下拉列表三角形和单击其他任何位置的组合框,c#,winforms,events,combobox,C#,Winforms,Events,Combobox,如何以编程方式区分用户单击下拉三角形(按钮)还是单击组合框上的任何其他位置? 我使用的是Visual Studio 2010,而不是excel。我使用的组合框没有建议答案中提到的事件和成员:ComboDropDown、CellClick和GetCellRect() 我有一个带有组合框的应用程序。我必须能够区分用户单击组合框按钮(以打开下拉列表)和用户单击显示的项目或下拉列表中的项目。我问这个问题是因为我不熟悉组合框事件,我想快速简便地解决问题。 在我看到没有答案后,我决定更彻底地调查这些组合框事

如何以编程方式区分用户单击下拉三角形(按钮)还是单击组合框上的任何其他位置?
我使用的是Visual Studio 2010,而不是excel。我使用的组合框没有建议答案中提到的事件和成员:ComboDropDown、CellClick和GetCellRect()
我有一个带有组合框的应用程序。我必须能够区分用户单击组合框按钮(以打开下拉列表)和用户单击显示的项目或下拉列表中的项目。

我问这个问题是因为我不熟悉组合框事件,我想快速简便地解决问题。
在我看到没有答案后,我决定更彻底地调查这些组合框事件,并尝试自己回答我的问题。
结果如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace ComboBoxClickEvents
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.ClientSize = new Size(833, 650);

            _allFiredEvents = new ObservableCollection<string>();

            InitializeComboBox();
            PopulateComboBox();
            InitializeTextBox();
            PopulateAllCorrespondingEventsNamesByUserClickAction();
            PopulateUserActionByAllCorrespondingEventsNames();
            _allEventsSequences = _userClickActionByAllCorrespondingEventsNames.Keys.ToList();
        
            _allFiredEvents.CollectionChanged += new NotifyCollectionChangedEventHandler(delegate(object sender, NotifyCollectionChangedEventArgs e)
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    List<HashSet<string>> allHashSets = _userClickActionByAllCorrespondingEventsNames.Keys.ToList();
                    HashSet<string> currentSequenceOfEventsNames = new HashSet<string>(); // Contains the names of all the events that fired as a result of a user click action.
                    for (int i = 0; i < _allFiredEvents.Count; i++)
                    {
                        string currentEventName = _allFiredEvents[i];
                        currentSequenceOfEventsNames.Add(currentEventName);
                    }
                    HashSet<string> identicalSequenceOfEventsNames = FindHashSetWithTheSameItemsInListOfHashSets(allHashSets, currentSequenceOfEventsNames);
                    if (identicalSequenceOfEventsNames != null)
                    {
                        string userClickAction = _userClickActionByAllCorrespondingEventsNames[identicalSequenceOfEventsNames];
                        int position = userClickAction.IndexOf(" or ");
                        if (position >= 0)
                        {
                            if (comboBox1.Text == "")
                            {
                                userClickAction = userClickAction.Substring(position + 4);
                            }
                            else
                            {
                                userClickAction = userClickAction.Substring(0, position);
                            }
                        }
                        userClickAction = "*****" + userClickAction;
                        textBox1.AppendText(userClickAction + Environment.NewLine);
                        _allFiredEvents.Clear();
                    }
                }
            }
            );
            InitializeButtonExit();
        }

        private ComboBox comboBox1;
        private Button buttonExit;
        private List<HashSet<string>> _allEventsSequences;
        private static ObservableCollection<string> _allFiredEvents;
        private static Dictionary<string, HashSet<string>> _allCorrespondingEventsNamesByUserClickAction;
        private static Dictionary<HashSet<string>, string> _userClickActionByAllCorrespondingEventsNames;
        TextBox textBox1;

        private void InitializeComboBox()
        {
            comboBox1 = new ComboBox();
            comboBox1.Location = new Point(20, 20);
            comboBox1.MouseDown += new MouseEventHandler(ComboBox1_MouseDown);
            comboBox1.Click += new EventHandler(ComboBox1_Click);
            comboBox1.MouseClick += new MouseEventHandler(ComboBox1_MouseClick);
            comboBox1.MouseUp += new MouseEventHandler(ComboBox1_MouseUp);
            comboBox1.DropDown += new EventHandler(ComboBox1_DropDown);
            comboBox1.SelectionChangeCommitted += new EventHandler(ComboBox1_SelectionChangeCommitted);
            comboBox1.DropDownClosed += new EventHandler(ComboBox1_DropDownClosed);
            comboBox1.SelectedValueChanged += new EventHandler(ComboBox1_SelectedValueChanged);
            comboBox1.SelectedIndexChanged += new EventHandler(ComboBox1_SelectedIndexChanged);
            comboBox1.TextChanged += new EventHandler(ComboBox1_TextChanged);
            this.Controls.Add(comboBox1);
        }

        private void PopulateComboBox()
        {
            comboBox1.Items.Add("First Item");
            comboBox1.Items.Add("Second Item");
            comboBox1.Items.Add("Third Item");
            comboBox1.Items.Add("Fourth Item");
            comboBox1.Items.Add("Fifth Item");
        }

        private HashSet<string> FindHashSetWithTheSameItemsInListOfHashSets(List<HashSet<string>> allHashSets, HashSet<string> soughtHashSet)
        {
            HashSet<string> currentHashSet = null;
            bool hashSetWithTheSameNumberOfItemsFound = false;
            bool identicalHashSetFound = false;
            int hashSetsCounter = 0;
            while (hashSetsCounter < allHashSets.Count && !identicalHashSetFound)
            {
                do
                {
                    currentHashSet = allHashSets[hashSetsCounter];
                    if (currentHashSet.Count != soughtHashSet.Count)
                    {
                        hashSetsCounter++;
                    }
                    else
                    {
                        hashSetWithTheSameNumberOfItemsFound = true;
                    }
                }
                while (hashSetsCounter < allHashSets.Count && currentHashSet.Count != soughtHashSet.Count);
                bool discrepancyFound = false;
                int itemsCounter = 0;
                if (hashSetWithTheSameNumberOfItemsFound)
                {
                    itemsCounter = 0;
                    foreach (string item in currentHashSet)
                    {
                        if (soughtHashSet.Contains(item))
                        {
                            itemsCounter++;
                        }
                        else
                        {
                            discrepancyFound = true;
                            break;
                        }
                    }
                }
                if (hashSetWithTheSameNumberOfItemsFound && !discrepancyFound)
                {
                    identicalHashSetFound = true;
                }
                else
                {
                    hashSetsCounter++;
                }
            }
            if (!identicalHashSetFound)
            {
                currentHashSet = null;
            }

            return currentHashSet;
        }
    
        private void PopulateAllCorrespondingEventsNamesByUserClickAction()
        {
            _allCorrespondingEventsNamesByUserClickAction = new Dictionary<string, HashSet<string>>();
            HashSet<string> allCorrespondingEventsNames = new HashSet<string>();
            allCorrespondingEventsNames.Add("ComboBox1_MouseDown");
            allCorrespondingEventsNames.Add("ComboBox1_Click");
            allCorrespondingEventsNames.Add("ComboBox1_MouseClick");
            allCorrespondingEventsNames.Add("ComboBox1_MouseUp");
            _allCorrespondingEventsNamesByUserClickAction.Add("Default Item Clicked In The TextBox or The Empty TextBox Is Clicked", allCorrespondingEventsNames);

            allCorrespondingEventsNames = new HashSet<string>();
            allCorrespondingEventsNames.Add("ComboBox1_DropDown");
            allCorrespondingEventsNames.Add("ComboBox1_MouseDown");
            allCorrespondingEventsNames.Add("ComboBox1_Click");
            allCorrespondingEventsNames.Add("ComboBox1_MouseClick");
            allCorrespondingEventsNames.Add("ComboBox1_MouseUp");
            _allCorrespondingEventsNamesByUserClickAction.Add("Drop-Down Button Clicked", allCorrespondingEventsNames);

            allCorrespondingEventsNames = new HashSet<string>();
            allCorrespondingEventsNames.Add("ComboBox1_MouseDown");
            allCorrespondingEventsNames.Add("ComboBox1_Click");
            allCorrespondingEventsNames.Add("ComboBox1_MouseClick");
            allCorrespondingEventsNames.Add("ComboBox1_MouseUp");
            allCorrespondingEventsNames.Add("ComboBox1_SelectionChangeCommitted");
            allCorrespondingEventsNames.Add("ComboBox1_DropDownClosed");
            allCorrespondingEventsNames.Add("ComboBox1_SelectedValueChanged");
            allCorrespondingEventsNames.Add("ComboBox1_SelectedIndexChanged");
            _allCorrespondingEventsNamesByUserClickAction.Add("Default Item Is Clicked In The Drop-Down List, when the Drop-Down List was already open", allCorrespondingEventsNames);

            allCorrespondingEventsNames = new HashSet<string>();
            allCorrespondingEventsNames.Add("ComboBox1_SelectionChangeCommitted");
            allCorrespondingEventsNames.Add("ComboBox1_DropDownClosed");
            allCorrespondingEventsNames.Add("ComboBox1_SelectedValueChanged");
            allCorrespondingEventsNames.Add("ComboBox1_SelectedIndexChanged");
            _allCorrespondingEventsNamesByUserClickAction.Add("Default Item Is Clicked In The Drop-Down List immediately after clicking the Drop-Down Button", allCorrespondingEventsNames);

            allCorrespondingEventsNames = new HashSet<string>();
            allCorrespondingEventsNames.Add("ComboBox1_MouseDown");
            allCorrespondingEventsNames.Add("ComboBox1_Click");
            allCorrespondingEventsNames.Add("ComboBox1_MouseClick");
            allCorrespondingEventsNames.Add("ComboBox1_MouseUp");
            allCorrespondingEventsNames.Add("ComboBox1_SelectionChangeCommitted");
            allCorrespondingEventsNames.Add("ComboBox1_DropDownClosed");
            allCorrespondingEventsNames.Add("ComboBox1_TextChanged");
            allCorrespondingEventsNames.Add("ComboBox1_SelectedValueChanged");
            allCorrespondingEventsNames.Add("ComboBox1_SelectedIndexChanged");
            _allCorrespondingEventsNamesByUserClickAction.Add("An Item (Not The Default Item) Is Clicked In The Drop-Down List, when the Drop-Down List was already open", allCorrespondingEventsNames);

            allCorrespondingEventsNames = new HashSet<string>();
            allCorrespondingEventsNames.Add("ComboBox1_SelectionChangeCommitted");
            allCorrespondingEventsNames.Add("ComboBox1_DropDownClosed");
            allCorrespondingEventsNames.Add("ComboBox1_TextChanged");
            allCorrespondingEventsNames.Add("ComboBox1_SelectedValueChanged");
            allCorrespondingEventsNames.Add("ComboBox1_SelectedIndexChanged");
            _allCorrespondingEventsNamesByUserClickAction.Add("An Item (Not The Default Item) Is Clicked In The Drop-Down List immediately after clicking the Drop-Down Button", allCorrespondingEventsNames);
        }

        private void PopulateUserActionByAllCorrespondingEventsNames()
        {
            _userClickActionByAllCorrespondingEventsNames = new Dictionary<HashSet<string>, string>();
            HashSet<string> allCorrespondingEventsNames = _allCorrespondingEventsNamesByUserClickAction["Default Item Clicked In The TextBox or The Empty TextBox Is Clicked"];
         
_userClickActionByAllCorrespondingEventsNames.Add(allCorrespondingEventsNames,“单击下拉按钮”)

_userClickActionByAllCorrespondingEventsNames.Add(allCorrespondingEventsNames,“当下拉列表已打开时,在下拉列表中单击默认项”)

_userClickActionByAllCorrespondingEventsNames.Add(allCorrespondingEventsNames,“单击下拉按钮后立即在下拉列表中单击默认项”)

_userClickActionByAllCorrespondingEventsNames.Add(allCorrespondingEventsNames,“当下拉列表已经打开时,在下拉列表中单击一个项目(不是默认项目”)

_userClickActionByAllCorrespondingEventsNames.Add(allCorrespondingEventsNames,“单击下拉按钮后立即在下拉列表中单击一个项目(不是默认项目”); }


你能说得更具体些吗?你在找什么?你想知道下拉列表何时显示吗?@Jimi我有一个带有组合框的应用程序。我必须能够区分用户点击组合框按钮(为了打开下拉列表,用户可以单击显示的项目或下拉列表中的项目。这是一个非常特殊的要求。您通常不需要知道这些,组合框的标准操作并不是基于这些细节。您应该解释您试图实现的目标-您认为需要此功能的原因:可能吧有一种不同的方式不强迫您使用NativeWindow对象或P/Invoking(无原因)。
            allCorrespondingEventsNames = _allCorrespondingEventsNamesByUserClickAction["Drop-Down Button Clicked"];
         
            allCorrespondingEventsNames = _allCorrespondingEventsNamesByUserClickAction["Default Item Is Clicked In The Drop-Down List, when the Drop-Down List was already open"];
         
            allCorrespondingEventsNames = _allCorrespondingEventsNamesByUserClickAction["Default Item Is Clicked In The Drop-Down List immediately after clicking the Drop-Down Button"];
         
            allCorrespondingEventsNames = _allCorrespondingEventsNamesByUserClickAction["An Item (Not The Default Item) Is Clicked In The Drop-Down List, when the Drop-Down List was already open"];
         
            allCorrespondingEventsNames = _allCorrespondingEventsNamesByUserClickAction["An Item (Not The Default Item) Is Clicked In The Drop-Down List immediately after clicking the Drop-Down Button"];
         
        private void InitializeTextBox()
        {
            textBox1 = new TextBox();
            textBox1.Location = new Point(comboBox1.Location.X + comboBox1.Width + 20, 20);
            textBox1.Name = "textBox1";
            textBox1.Multiline = true;
            textBox1.ScrollBars = ScrollBars.Vertical;
            textBox1.Size = new Size(650, 530);
            this.Controls.Add(textBox1);
        }

        private void InitializeButtonExit()
        {
            buttonExit = new Button();
            buttonExit.Name = "buttonExit";
            buttonExit.Text = "Exit";
            buttonExit.Size = new Size(85, 32);
            buttonExit.Location = new Point((this.ClientSize.Width - buttonExit.Width) / 2, this.ClientSize.Height - buttonExit.Height - 20);
            buttonExit.Click += new EventHandler(ButtonExit_Click);
            this.Controls.Add(buttonExit);
        }
    
        private void ComboBox1_MouseDown(object sender, MouseEventArgs e)
        {
            string eventName = "ComboBox1_MouseDown";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }
    
        private void ComboBox1_Click(object sender, EventArgs e)
        {
            string eventName = "ComboBox1_Click";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }
    
        private void ComboBox1_MouseClick(object sender, MouseEventArgs e)
        {
            string eventName = "ComboBox1_MouseClick";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }

        private void ComboBox1_MouseUp(object sender, MouseEventArgs e)
        {
            string eventName = "ComboBox1_MouseUp";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }

        private void ComboBox1_DropDown(object sender, EventArgs e)
        {
            string eventName = "ComboBox1_DropDown";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }

        private void ComboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string eventName = "ComboBox1_SelectionChangeCommitted";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }

        private void ComboBox1_DropDownClosed(object sender, EventArgs e)
        {
            string eventName = "ComboBox1_DropDownClosed";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }

        private void ComboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            string eventName = "ComboBox1_SelectedValueChanged";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }

        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string eventName = "ComboBox1_SelectedIndexChanged";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }

        private void ComboBox1_TextChanged(object sender, EventArgs e)
        {
            string eventName = "ComboBox1_TextChanged";
            _allFiredEvents.Add(eventName);
            textBox1.AppendText(eventName + Environment.NewLine);
        }

        private void ButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}