[英]Is it possible to avoid a TRichEdit losing its focus when clicking a button (e.g. Bold)?
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. 使用delphi和丰富的编辑功能,我需要按照我正在编写的编辑器的方式来复制内容,当您选择文本并按加粗按钮时,文本将保持选中状态,而不是取消选择并失去焦点。
How can I achieve this? 我该如何实现?
Thank you. 谢谢。
OK, now I think I see the issue. 好的,现在我想我看到了问题。 You have a TRichEdit
and a TButton
. 您有一个TRichEdit
和一个TButton
。 Then you do something like 然后你做类似的事情
procedure TForm1.Button1Click(Sender: TObject);
begin
with RichEdit1.SelAttributes do
Style := Style + [fsBold];
end;
and you are annoyed by the fact that the Rich Edit control loses its focus when you click Button1
. 当您单击Button1
时,Rich Edit控件失去了焦点,这一点使您感到恼火。 Normally you use a TToolButton
in a TToolbar
as the 'bold' button. 通常,您将TToolButton
中的TToolbar
用作“粗体”按钮。 This will not make the editor lose its focus, because a TToolButton
is not a windowed control. 这不会使编辑器失去焦点,因为TToolButton
不是窗口控件。
If you do not wish to use a TToolBar
(or any equivalent control), simply use a TSpeedButton
instead of a TButton
. 如果您不想使用TToolBar
(或任何等效控件),只需使用TSpeedButton
而不是TButton
。
The normal way of doing this, however, is to use a TActionList
. 但是,通常的方法是使用TActionList
。 Drop such a control on your form, and then add a new action, call it ActnBold
or something. 在窗体上放一个这样的控件,然后添加一个新动作,将其ActnBold
或其他名称。 Set the caption to 'Bold'
, the hint to 'Make the selection bold.'
将标题设置为'Bold'
,将提示设置为'Make the selection bold.'
, add the shortcut Ctrl+B
, and write ,添加快捷键Ctrl+B
,然后编写
with RichEdit1.SelAttributes do
Style := Style + [fsBold];
in its OnExecute
event. 在其OnExecute
事件中。 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
. 然后,只需将控件的Action
属性设置为ActnBold
,就可以将此操作关联到任何按钮,速度按钮,工具栏按钮,菜单项ActnBold
。
If you really, really want to use a windowed control, such as a TButton
, then you can do 如果您确实要使用窗口控件(例如TButton
,则可以
procedure TForm1.Button1Click(Sender: TObject);
begin
with RichEdit1.SelAttributes do
Style := Style + [fsBold];
RichEdit1.SetFocus;
end;
but it isn't beautiful (IMHO). 但它并不漂亮(恕我直言)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.