【问题标题】:DevExpress select all when clicked on GridView cell (TextEdit)DevExpress 在单击 GridView 单元格 (TextEdit) 时全选
【发布时间】:2019-12-18 13:37:07
【问题描述】:

我需要这样做,所以当用户在网格视图中单击带有 TextEdit 的单元格时,它将在 textedit 中全选。我尝试了很多我可以在互联网上找到的方法,但没有一个效果很好。

"EditorShowMode = MouseUp" 方式会破坏一切,例如当您单击具有 checkedit 的单元格时;它选择了单元格,然后您需要再次单击才能实际单击 CheckEdit。

"Use EditorShowMode = MouseUp and manually handle other things on MouseDown" 只是 ew。不适用于所有类型的控件。

“在 ShownEditor 事件上更改选择长度等”方式也不起作用,实际上它在单击时选择文本,但它不会覆盖默认功能,因此选择会立即改变。还尝试了 SelectAll 方法,但它有一些我不记得的问题(可能根本不起作用)。

我确实尝试了很多东西,但找不到完全好的方法。请告诉我您是否可以在不破坏网格中其他类型控件的情况下获得一种工作方式。

【问题讨论】:

  • 我建议为此使用附加的布尔属性。你考虑过吗?
  • 我不知道

标签: c# .net devexpress


【解决方案1】:

Pavel 在 DevExpress 支持上的回答(效果很好):

实现这一点的最简单方法是使用 GridView.ShownEditor 事件来订阅活动编辑器的 MouseUp 事件。然后,选择 MouseUp 事件处理程序中的所有文本并分离此处理程序以避免随后的文本选择。

private void GridView_ShownEditor(object sender, EventArgs e)
{
    GridView view = sender as GridView;
    if (view.ActiveEditor is TextEdit)
        view.ActiveEditor.MouseUp += ActiveEditor_MouseUp;
}

private void ActiveEditor_MouseUp(object sender, MouseEventArgs e)
{
    BaseEdit edit = sender as BaseEdit;
    edit.MouseUp -= ActiveEditor_MouseUp;
    edit.SelectAll();
}

【讨论】:

    【解决方案2】:

    您可以使用 GridView CustomRowCellEdit 事件并设置文本编辑器的事件,例如 Mouse Up。设置 RepositoryItemTextEdit MouseUp 事件可以按照示例设置。

    例子:

    private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
    {
        if (e.RepositoryItem is DevExpress.XtraEditors.Repository.RepositoryItemTextEdit)
        {
            DevExpress.XtraEditors.Repository.RepositoryItemTextEdit rep = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
            rep.ReadOnly = false;
            rep.MouseUp += rep_MouseUp;
            e.RepositoryItem = rep;
        }       
    
    }
    void rep_MouseUp(object sender, MouseEventArgs e)
    {
        DevExpress.XtraEditors.TextEdit te = sender as DevExpress.XtraEditors.TextEdit;
        te.SelectAll();
    }
    

    【讨论】:

    • 这确实有效,但是.. 如果我愿意,它不允许我选择文本的特定部分,我的意思是它应该只在您第一次单击 textedit 时才有效。我尝试以相同的方式使用“Enter”事件,没有做任何事情。嗯..
    • 有什么想法吗?嗯。 .
    【解决方案3】:

    您应该为 TextEdit 处理 Enter 事件

    private void myRepositoryItemTextEdit_Enter(object sender, EventArgs e)  
    {  
        var editor = (DevExpress.XtraEditors.TextEdit)sender;  
        BeginInvoke(new MethodInvoker(() =>  
        {  
            editor.SelectionStart = 0;  
            editor.SelectionLength = editor.Text.Length;  
        }
    }
    

    【讨论】:

    • 不幸的是,即使事件被调用并改变了选择长度,它也没有做任何视觉上的事情。
    • 似乎需要 BeginInvoke 调用。我已经更新了答案
    • 好的,但是效果不好。它选择直到我点击的字符。还尝试了 SelectAll,但这只是从文本的末尾开始并选择直到我单击的字符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多