编辑一个文本框单元格的DataGridView就好像它是一个正常的文本框(在按下箭头没有跳跃) [英] Edit a TextBox cell in DataGridView as if it were a normal TextBox (no jumping on pressing arrows)

查看:238
本文介绍了编辑一个文本框单元格的DataGridView就好像它是一个正常的文本框(在按下箭头没有跳跃)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多行,在一个DataGridView(自动换行)文本框列。这将是伟大的,是能够将它们编辑为正常文本框,即当我按向下箭头,我想插入符号的文本框内往下走一条线,我不希望它跳转到下一个行,因为它一般不会即可。同样,我想在按下输入创建文本框小区内的新行,但它不是完成编辑

I have "multiline" (wordwrapping) textbox columns in a DataGridView. It would be great to be able to edit them as normal TextBoxes, that is, when I press down arrow, I want the caret to go down one line within the textbox, I don't want it to jump to the next row, as it normally does. Likewise, I want that pressing enter creates a new line within the textbox cell, but it instead finishes editing.

否则说:我想覆盖某些按键(或keydowns)的正常行为,使用户可以编辑文本框细胞就好像它是一个正常的文本框,然后导航里面带有箭头和创造新线与输入。

Otherwise said: I want to override normal behaviour of some keypresses (or keydowns), so that user can edit a textbox cell as if it were a normal textbox, and navigate inside it with arrows and create new lines with enter.

我试图操纵在DataGridView中的keydown事件,但没有奏效。

I tried manipulating keydown events in DataGridView, but it didn't work.

感谢您的任何意见或建议。

Thank you for any ideas or comments.

推荐答案

这个问题,在这里给我一个方法来解决它。下面是代码:

This question here showed me a way to solve it. Here is the code:

class MyDataGridView : DataGridView
{

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((keyData == Keys.Enter) && (this.EditingControl != null))
        {
            //new behaviour for Enter
            TextBox tb = (TextBox)EditingControl;
            int pos = tb.SelectionStart;
            tb.Text = tb.Text.Remove(pos, tb.SelectionLength);
            tb.Text = tb.Text.Insert(pos, Environment.NewLine);
            tb.SelectionStart = pos + Environment.NewLine.Length;
            tb.ScrollToCaret();
            //and do nothing else
            return true;
        }
        else if ((keyData == Keys.Up) && (this.EditingControl != null))
        {
            //programmatically move caret up
            //(look at related question to see how)
            return true;
        }
        else if ((keyData == Keys.Down) && (this.EditingControl != null))
        {
            //programmatically move caret down
            //(look at related question to see how)
            return true;
        }
        //for the rest of the keys, proceed as normal
        return base.ProcessCmdKey(ref msg, keyData);
    }
}



所以这是DataGridView中的一个简单变化,它的工作原理。我只有

So this is a simple change of DataGridView and it works. I only had to


  • 创建这个新类

  • 更改两个来自DesignerClass 线条中使用MyDataGridView代替的DataGridView(声明和初始化)

  • create this new class, and
  • change two lines from the DesignerClass to use MyDataGridView instead of DataGridView (declaration and initialization)

和其他一切。预期一样

相关问题:的如何通过编程移动插入记号的上下一行

这篇关于编辑一个文本框单元格的DataGridView就好像它是一个正常的文本框(在按下箭头没有跳跃)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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