按下CloseUpKey.Key时弹出Popup时防止RepositoryItemSearchLookUpEdit

时间:2017-02-07 12:44:47

标签: winforms devexpress xtragrid

我使用RepositoryItemSearchLookUpEdit。其CloseUpKey属性设置为space

result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);

我只想将此快捷方式用于打开弹出窗口,而用于关闭弹出窗口。

我怎样才能做到这一点?

UPDATE ------------------------

首先我创建一个RepositoryItemSearchLookUpEdit对象

var result = new RepositoryItemSearchLookUpEdit();
result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);
result.KeyDown += repositoryItemLookUpEdit_KeyDown;
result.CloseUp += repositoryItemLookUpEdit_CloseUp;
result.QueryCloseUp += repositoryItemLookUpEdit_QueryCloseUp; 

private void repositoryItemLookUpEdit_QueryCloseUp(object sender, System.ComponentModel.CancelEventArgs e)
    {
        var edit = sender as SearchLookUpEdit;
        KeyEventArgs k = new KeyEventArgs(edit.Properties.CloseUpKey.Key);
        AttachKeyPressEvent(k);
        if (k.KeyCode == edit.Properties.CloseUpKey.Key)
            e.Cancel = true;
    }

并将其传递给网格列:

grdListView.Columns["yyy"].ColumnEdit = result

如何使用这些events实现这一目标,而无需创建后代SearchLookUpEdit

1 个答案:

答案 0 :(得分:0)

更新:

问题是CloseUp事件(在那里你可以得到关于特写键的必要信息)发生在QueryCloseUp事件之后(你可以在那里预先关闭关闭事件)。当QueryCloseUp发生时,KeyPress,KeyDown和KeyUp事件似乎也不会发生,因此它们无法被覆盖。所以我尝试了这个,我创建了一个自定义的KeyEventHandler并在QueryCloseUp事件期间触发了他,以便获得按下键的必要信息,如果关键事件就是那个,则取消事件。这是我的代码试试看它是否适合你

       public partial class Form1 : Form
       {
         public Form1()
    {
        InitializeComponent();
        //Here you can add your grid control as you have created
        DataTable dt = new DataTable();
        dt.Columns.Add("ID"); //use your own names and types
        gridControl1.DataSource = dt;

        var result = new RepositoryItemSearchLookUpEdit();
        result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);
        result.QueryCloseUp += new CancelEventHandler(repositoryItemLookUpEdit_QueryCloseUp);
        ((gridControl1.MainView as GridView).Columns["ID"] as GridColumn).ColumnEdit = result;
    }

    private static readonly object myQueryCloseUp = new object();

    public event KeyEventHandler MyQueryCloseUp
    {
        add { Events.AddHandler(myQueryCloseUp, value); }
        remove { Events.RemoveHandler(myQueryCloseUp, value); }
    }
    protected virtual void AttachKeyPressEvent(KeyEventArgs e)
    {
        KeyEventHandler handler = (KeyEventHandler)Events[myQueryCloseUp];
        if (handler != null)
            handler(this, e);
    }
    //Here you add your own Handler implementation
    public void repositoryItemLookUpEdit_QueryCloseUp(object sender, CancelEventArgs e)
    {
        KeyEventArgs k = new KeyEventArgs((sender as SearchLookUpEdit).Properties.CloseUpKey.Key);
        AttachKeyPressEvent(k);
        if (k.KeyCode == (sender as SearchLookUpEdit).Properties.CloseUpKey.Key)
            e.Cancel = true;
    }

}