首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在StringGrid中插入新行

在StringGrid中插入新行
EN

Stack Overflow用户
提问于 2016-01-08 00:38:26
回答 1查看 1.3K关注 0票数 2

我有一个StringGrid,它能够使用OnDraw方法绘制新的线条。

它工作得很好,但现在我希望用户可以使用键盘输入一个新行。目前,多行文本必须复制到StringGrid中。

每次按VK_RETURN时,StringGrid都会保留编辑模式。我要怎么做才能避免这种情况?

对于新的业务,我想要Ctrl+Return,就像Skype中的那样。

代码语言:javascript
复制
procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) and (ssCtrl in Shift) then // Ctrl+Return = New Line
  begin
    // TODO: Do NOT cancel edit mode
    // TODO: Insert #13#10 at current cursor position
  end;
end;
EN

回答 1

Stack Overflow用户

发布于 2016-01-08 02:51:01

TStringGrid的内置单元编辑器不支持多行文本(它是TCustomMaskEdit (单行)的后代)。您必须使用单独的UI控件(如TMemo )来编辑多行文本。要做到这一点,有几种不同的方法:

  1. 禁用网格的内置编辑器(从网格的goEditing属性中移除Options标志),并在TStringGrid附近放置一个TMemo。当用户选择一个单元格(可选地单击一个按钮/菜单)时,将其当前文本分配到备忘录中。如果用户进行任何更改(可选择地单击另一个按钮/菜单),则将新文本分配回单元格。
  2. 上面的一个变体:设计一个单独的TForm,其中有一个TMemo和保存按钮。当用户选择一个单元格(或按一个按钮/菜单)时,将该单元格的当前文本分配给表单的备注,然后通过调用其ShowModal()方法来显示表单。如果用户单击“保存”按钮,则通过将其ModalResult属性设置为mrOk来关闭窗体。如果ShowModal()返回mrOk,则将备忘录的当前文本分配给正在编辑的单元格,然后Invalidate()网格以触发重绘。如果ShowModal()返回任何其他内容,则什么也不做。
  3. 上面的一个变体:让网格的内置编辑器启用,然后按照上面的方式设计一个单独的表单,然后配置TStringGrid以自动触发表单,而不必以任何方式更改您的UI的其余部分。
代码语言:javascript
复制
1. derive a new class from `TStringGrid` and have it override the virtual `GetEditStyle()` method to return `esEllipsis` (it returns `esSimple` by default), and override the virtual `CreateEditor()` method to return a new instance of a `TInplaceEditList`-derived class (it returns a `TInplaceEdit` object by default).  This will cause a cell to display a push button instead of an edit field when being edited.
2. Have your `TInplaceEditList`-derived class own an instance of your Form, and then override the virtual `UpdateContents()` method to retrieve the current cell text and assign it to the Form's Memo, and override the virtual `DoEditButtonClick()` method to show the Form modally and respond to the return value of `ShowModal()` accordingly.

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34667709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档