如何让 DataGridView 组合框一键显示其下拉列表? [英] How do I get DataGridView comboboxes to display their drop down list in one click?

查看:23
本文介绍了如何让 DataGridView 组合框一键显示其下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我将EditOnEnter"设置为 true 后,如果我不点击组合框的向下箭头部分,DataGridViewComboBoxCell 仍然需要点击两次才能打开.

After I set "EditOnEnter" to be true, the DataGridViewComboBoxCell still takes two clicks to open if I don't click on the down arrow part of the combo box.

有人知道如何解决这个问题吗?我有自己使用的 DataGridView 类,因此我可以使用一些我希望的智能事件处理程序轻松地在系统范围内解决这个问题.

Anyone have any clue how to fix this? I've got my own DataGridView class that I use, so I can easily fix this issue system-wide with a few smart event handlers I hope.

谢谢.

推荐答案

由于您已经将 DataGridViewEditMode 属性设置为EditOnEnter",你可以覆盖它的 OnEditingControlShowing 确保组合框获得焦点后立即显示下拉列表的方法:

Since you already have the DataGridView's EditMode property set to "EditOnEnter", you can just override its OnEditingControlShowing method to make sure the drop-down list is shown as soon as a combo box receives focus:

public class myDataGridView : DataGridView
{

    protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
    {
        base.OnEditingControlShowing(e);

        if (e.Control is ComboBox) {
            SendKeys.Send("{F4}");
        }
    }

}

每当 DataGridView 控件中的编辑控件获得输入焦点时,上面的代码都会检查它是否为组合框.如果是这样,它实际上是按下" F4 键,这会导致下拉部分展开(在任何组合框具有焦点时尝试!).这有点像黑客,但它的作用就像一个魅力.

Whenever an edit control in your DataGridView control gets the input focus, the above code checks to see if it is a combo box. If so, it virtually "presses" the F4 key, which causes the drop-down portion to expand (try it when any combo box has the focus!). It's a little bit of a hack, but it works like a charm.

这篇关于如何让 DataGridView 组合框一键显示其下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆