【问题标题】:Positioning cursor in a GridViewComboBoxColumn (Telerik)在 GridViewComboBoxColumn 中定位光标 (Telerik)
【发布时间】:2014-07-12 15:20:15
【问题描述】:

我正在使用一个 RadGridView,它里面有一个视图 GridViewComboBoxColumn 。 编辑器本身是一个自定义编辑器,基于:RadDropDownListEditor。

我目前正在尝试实现它,以便按向左或向右箭头不会影响单元格或选择项,而是在编辑器内移动光标。因此我的问题是如何访问那里的光标位置。

   class CustomizedDropDownEditor : RadDropDownListEditor
{
    public override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == System.Windows.Forms.Keys.Left || e.KeyCode == System.Windows.Forms.Keys.Right)
        {
            //Customized left right arrow key behaviour

        }
        else
        {
            base.OnKeyDown(e);
        }
    }

我已经尝试了一些方法,但没有找到可以访问编辑器文本框或其中的 selectionstart 的方法。

编辑: 尽管上面的代码拦截了键,但左箭头键仍会导致单元格离开(尽管右箭头键不会导致此结果)。是否有可能避免这种情况?如果可以,如何避免?

Tnx.

【问题讨论】:

    标签: c# winforms telerik datagridcomboboxcolumn


    【解决方案1】:

    将属性转换为RadDropDownListEditorElement 后,您可以访问编辑器的EditorElement 属性中的文本。如果您想在同一个覆盖中执行此操作:

    class CustomizedDropDownEditor : RadDropDownListEditor
    {
        public override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Left || e.KeyCode == System.Windows.Forms.Keys.Right)
            {
                //Customized left right arrow key behaviour
    
                int selectionStart = ((RadDropDownListEditorElement)this.EditorElement).SelectionStart;
    
                int selectionLength = ((RadDropDownListEditorElement)this.EditorElement).SelectionLength;
    
            }
            else
            {
                base.OnKeyDown(e);
            }
        }
    }
    

    或者如果你想从其他地方做,你可以通过网格的ActiveEditor 属性做同样的事情(虽然我不认为你会想做很多其他事情,因为编辑器当然会关闭并丢失您的文本选择!):

    private void RadGridView1_OnMouseLeave(object sender, EventArgs e)
    {
        int selectionStart = ((RadDropDownListEditorElement)((CustomizedDropDownEditor)radGridView1.ActiveEditor).EditorElement).SelectionStart;
    }
    

    This Telerik article 给出了一个在EndEdit 事件触发时访问文本的示例,您可能也对此感兴趣。

    【讨论】:

    • tnx 主要工作(设置 selectionstart 那里 +1 如果右键,或 -1 如果左键(下降到值 0 或最多 text.length)但刚刚发现左键仍然触发它离开了单元格(在向左移动 1 个字符后)。你碰巧知道如何停止这种行为吗?
    • 确保您也以完全相同的方式覆盖OnKeyUp,以击败基本行为。
    • tnx 运行良好。我会将该答案标记为已接受并投赞成票。似乎最初的赏金已经到期(由于首先我的 RL 问题因此无法对其进行测试),我需要 200 个代表才能在那里为您发放奖励(似乎没有办法解决)。 tnx 仍然。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2010-10-17
    • 2010-12-05
    相关资源
    最近更新 更多