是否可以避免单击按钮(例如,粗体)时TRichEdit失去焦点? [英] Is it possible to avoid a TRichEdit losing its focus when clicking a button (e.g. Bold)?
问题描述
使用delphi和丰富的编辑功能,我需要按照我正在编写的编辑器的方式复制内容,当您选择文本并按加粗按钮时,该文本将保持选中状态,而不是取消选择并失去焦点。
Using delphi and rich edit, I need to replicate something along the lines of this very editor I'm writing in, when you select a text and press on the Bold button, the text remains selected instead of unselecting and losing focus.
我该怎么实现?
谢谢。
推荐答案
好的,现在我认为是问题所在。您有一个 TRichEdit
和一个 TButton
。然后,您可以执行以下操作:
OK, now I think I see the issue. You have a TRichEdit
and a TButton
. Then you do something like
procedure TForm1.Button1Click(Sender: TObject);
begin
with RichEdit1.SelAttributes do
Style := Style + [fsBold];
end;
您会被Rich Edit控件在单击<$ c时失去焦点而感到烦恼$ c> Button1 。通常,您在 TToolbar
中使用 TToolButton
作为粗体按钮。这不会使编辑器失去焦点,因为 TToolButton
不是窗口控件。
and you are annoyed by the fact that the Rich Edit control loses its focus when you click Button1
. Normally you use a TToolButton
in a TToolbar
as the 'bold' button. This will not make the editor lose its focus, because a TToolButton
is not a windowed control.
如果您这样做不希望使用 TToolBar
(或任何等效控件),只需使用 TSpeedButton
而不是 TButton
。
If you do not wish to use a TToolBar
(or any equivalent control), simply use a TSpeedButton
instead of a TButton
.
通常的做法是使用 TActionList
。在窗体上放一个这样的控件,然后添加一个新动作,将其命名为 ActnBold
之类。将标题设置为'粗体'
,提示设置为'将选择内容设为粗体。'
,添加快捷键 Ctrl + B
,然后使用RichEdit1编写
The normal way of doing this, however, is to use a TActionList
. Drop such a control on your form, and then add a new action, call it ActnBold
or something. Set the caption to 'Bold'
, the hint to 'Make the selection bold.'
, add the shortcut Ctrl+B
, and write
with RichEdit1.SelAttributes do
Style := Style + [fsBold];
在其 OnExecute
事件中。然后,您只需将控件的 Action
属性设置为关联即可将该操作与任何按钮,速度按钮,工具栏按钮,菜单项等相关联。到 ActnBold
。
in its OnExecute
event. Then you can associate this action to any button, speed button, toolbar button, menu item, ..., simply by setting the control's Action
property to ActnBold
.
如果您确实想使用窗口控件,例如 TButton
,那么您可以
If you really, really want to use a windowed control, such as a TButton
, then you can do
procedure TForm1.Button1Click(Sender: TObject);
begin
with RichEdit1.SelAttributes do
Style := Style + [fsBold];
RichEdit1.SetFocus;
end;
但它并不漂亮(IMHO)。
but it isn't beautiful (IMHO).
这篇关于是否可以避免单击按钮(例如,粗体)时TRichEdit失去焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!