我有一个StringGrid,它能够使用OnDraw方法绘制新的线条。
它工作得很好,但现在我希望用户可以使用键盘输入一个新行。目前,多行文本必须复制到StringGrid中。
每次按VK_RETURN时,StringGrid都会保留编辑模式。我要怎么做才能避免这种情况?
对于新的业务,我想要Ctrl+Return,就像Skype中的那样。
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;发布于 2016-01-08 02:51:01
TStringGrid的内置单元编辑器不支持多行文本(它是TCustomMaskEdit (单行)的后代)。您必须使用单独的UI控件(如TMemo )来编辑多行文本。要做到这一点,有几种不同的方法:
goEditing属性中移除Options标志),并在TStringGrid附近放置一个TMemo。当用户选择一个单元格(可选地单击一个按钮/菜单)时,将其当前文本分配到备忘录中。如果用户进行任何更改(可选择地单击另一个按钮/菜单),则将新文本分配回单元格。TForm,其中有一个TMemo和保存按钮。当用户选择一个单元格(或按一个按钮/菜单)时,将该单元格的当前文本分配给表单的备注,然后通过调用其ShowModal()方法来显示表单。如果用户单击“保存”按钮,则通过将其ModalResult属性设置为mrOk来关闭窗体。如果ShowModal()返回mrOk,则将备忘录的当前文本分配给正在编辑的单元格,然后Invalidate()网格以触发重绘。如果ShowModal()返回任何其他内容,则什么也不做。TStringGrid以自动触发表单,而不必以任何方式更改您的UI的其余部分。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.
https://stackoverflow.com/questions/34667709
复制相似问题