Richedit style formatting changes on its own

不羁的心 提交于 2019-12-11 01:35:56

问题


(Someone edit the title if you can understand and define my problem better.)

The problem which I am having is with style formatting of a RichEdit "reverting" back to the default "nothing" aka [] and then back to whatever I set it to, bold or italic for example.

The thing that is at fault - I assume, since I have no idea how it is breaking things - is a procedure (REMainLinesCheck) that checks for amount of lines in the RichEdit and deletes the first one until a certain point is reached (to show a maximum of 14 lines at once) like so:

while REMain.Lines.Count > 14 do
  REMain.Lines.Delete(0); 

I have 6 occurrences of the above procedure in other procedures that add lines to the RichEdit, but none of them change RichEdit.SelAttributes.Style but one, which was adding only one Bold line like so:

REMain.SelAttributes.Style := [fsBold];
REMain.Lines.Add('something');
REMainLinesCheck;

So I have removed all occurrences except that one and started poking around, it didn't take long to see that it was working in fact fine, regular and bold lines where being added normally and excess lines where being deleted - no problems. But as soon as I reintroduced REMainLinesCheck procedure into another procedure (for clarity purposes, lets call it Proc3Lines, because that's what it does: adds 3 lines and then calls the check for excess lines), every line that follows this Proc3Lines that should be Bold is not... From what I have experienced here it seems that REMainLinesCheck does something in Proc3Lines, since without it everything is fine.

Obviously it's not a circle of procedures that call each other, but the other parts of the code have nothing to do with this RichEdit, not to mention that I don't change RichEdit.SelAttributes.Style anywhere for REMain except that one place that I have shown, there is another RichEdit in the same unit that I do change its line's style like that, but that cannot possibly be related in any way... could it? (No it does not, I just checked.)

Basically: what the hell Delphi? It cannot get any simpler than this and I am still managing to fail, can someone explain and/or fix this? Ask questions, I'll elaborate as much as I can if something is not clear.


回答1:


To apply a format to a new added line, use the following:

procedure TForm1.Button1Click(Sender: TObject);
var
  LineIndex: Integer;
begin
  LineIndex := RichEdit1.Lines.Add('Something');
  RichEdit1.SelStart := RichEdit1.Perform(EM_LINEINDEX, LineIndex, 0);
  RichEdit1.SelLength := RichEdit1.Perform(EM_LINELENGTH, RichEdit1.SelStart, 0);
  RichEdit1.SelAttributes.Style := [fsBold];
end;



回答2:


This has worked for me:

procedure TformStart.Proc;
var
endtxtpos: integer;
begin
  endtxtpos := Length(REMain.Text);
  REMain.Lines.Add('something');
  REMain.SelStart := endtxtpos-(REMain.Lines.Count-1);
  REMain.SelLength := Length('something');
  REMain.SelAttributes.Style := [fsBold];
end;

But since I don't know any better, please criticize and suggest how I can do it better.



来源:https://stackoverflow.com/questions/10645033/richedit-style-formatting-changes-on-its-own

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!