简体   繁体   English

是否可以避免单击按钮(例如,粗体)时TRichEdit失去焦点?

[英]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.

相关问题 Delphi:点击孩子时失去父母的关注 - Delphi : losing focus from parent when clicking on child 使用DefineProperties替换TPersistent属性,例如TFont - Use DefineProperties to replace TPersistent properties e.g. TFont 如何在TForm的边界之外显示控件(例如TListbox) - How to display a control (e.g. TListbox) beyond the borders of a TForm 失去焦点时保持InPlaceEditor高亮显示 - Keep InPlaceEditor highlight when losing focus 计算开工率? 例如每秒检查X - Calculating a operational rate? E.g. Checking X per second 如何调用网络服务方法进行测试,例如从浏览器 - How to call a webservice method for testing, e.g. from a browser RTTI访问VCL的私有方法,例如TCustomForm.SetWindowState - RTTI access to private methods of VCL, e.g. TCustomForm.SetWindowState 如何将只有十个字符的字符串(例如“ 12345 * 45688”)拆分为数组 - How to split a string of only ten characters e.g.“12345*45688” into an array 如何使用SuperObject序列化包含点(例如IP地址)的JSON密钥? - How to serialize JSON key containing dots (like e.g. IP address) with SuperObject? 如何在每个项目文件夹中保存dcu文件(例如:。\ $(平台)\ $(配置)\ $(ProjectFilename)? - How to keep dcu files in per project folder (e.g.: .\$(Platform)\$(Config)\$(ProjectFilename)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM