【问题标题】:How can I emulate key press with copy & paste in an Infragistics UltraWinGrid?如何在 Infragistics UltraWinGrid 中通过复制和粘贴来模拟按键?
【发布时间】:2010-01-28 23:47:07
【问题描述】:

我正在使用 Infragistics UltraWinGrid(Win 9.1 版)。默认行为是允许用户在单元格中键入文本。当从 Excel 电子表格复制多个单元格时,只有第一个单元格的数据会被粘贴到 UltraWinGrid 中。

通过 CellClickAction.CellSelect 将 UltraWinGrid 单元格设置为不可编辑,可以轻松更改粘贴多个单元格的行为;不幸的是,当您这样做时,您可能不会在单元格中输入数据。

所以我尝试使用 InitializeLayout、KeyDown 和 KeyPress 的事件来修改这些设置。

    private void ugridQuoteSheet_InitializeLayout(object sender, InitializeLayoutEventArgs e)
    {
        e.Layout.Override.AllowMultiCellOperations = AllowMultiCellOperation.All;
        e.Layout.Override.CellClickAction = CellClickAction.CellSelect;
    }

    //Event used to circumvent the control key from choking in
    //the KeyPress event. This doesn't work btw.
    private void ugridQuoteSheet_KeyDown(object sender, KeyEventArgs e)
    {
        UltraGrid grid = (UltraGrid)sender;

        if (e.Control == true)
        {
           e.SuppressKeyPress = true;
        }
    }

    // This event comes after the KeyDown event. I made a lame attempt to stop
    // the control button with (e.KeyChar != 22). I lifted some of this from 
    // the Infragistics post: http://forums.infragistics.com/forums/p/23690/86732.aspx#86732
    private void ugridQuoteSheet_KeyPress(object sender, KeyPressEventArgs e)
    {
        UltraGrid grid = (UltraGrid)sender;
        if ((grid != null) && (grid.ActiveCell != null) && (!grid.ActiveCell.IsInEditMode) && (e.KeyChar != 22))
        {
            grid.PerformAction(UltraGridAction.EnterEditMode);
            EditorWithText editor = (EditorWithText)grid.ActiveCell.EditorResolved;
            editor.TextBox.Text = e.KeyChar.ToString();
            editor.TextBox.SelectionStart = 1;
        }
    }

    // This puts the grid in CellSelect mode again so I won't edit text.
    private void ugridQuoteSheet_AfterCellUpdate(object sender, CellEventArgs e)
    {
        this.ugridQuoteSheet.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect;
    }

我现在可以再次在单元格中键入值。问题是,当我按 [ctrl][v] 进行粘贴时,KeyPressEventArgs.KeyChar 是 22 并且没有“v”。您可以在 ugridQuoteSheet_KeyPress 委托中看到我试图规避此问题的徒劳尝试。什么是事件处理和 CellClickAction 设置的正确组合,以允许复制粘贴和输入 UltraWinGrid 的单元格?

【问题讨论】:

    标签: .net infragistics copy-paste ultrawingrid


    【解决方案1】:

    仔细阅读之前提到的帖子(http://forums.infragistics.com/forums/p/23690/86732.aspx#86732 ) 我已经能够解决这个问题。

    这一切都可以在设置 UltraWinGrid.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect; 后的 KeyPress 事件中处理。当然是在 InitializeLayout 事件中。

        private void ugridQuoteSheet_KeyPress(object sender, KeyPressEventArgs e)
        {
            UltraGrid grid = (UltraGrid)sender;
    
            if (!Char.IsControl(e.KeyChar) && grid != null && grid.ActiveCell != null &&
                grid.ActiveCell.EditorResolved is EditorWithText && !grid.ActiveCell.IsInEditMode)
            {
                grid.PerformAction(UltraGridAction.EnterEditMode);
                EditorWithText editor = (EditorWithText)grid.ActiveCell.EditorResolved;
                editor.TextBox.Text = e.KeyChar.ToString();
                editor.TextBox.SelectionStart = 1;
            }
        }
    

    我不知道如何处理同时按键,[ctrl][v]。 Char.IsControl(e.KeyChar) 在这里起到了作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2017-01-25
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多