替换换行符后如何在Delphi中维护Cursor位置

时间:2016-05-16 09:50:06

标签: delphi replace cursor

我有一个名字= dbmemSummary的DBMemo和一个方法ReplaceLineBreaks,它将从DB Memo中删除额外的换行符。我在设置光标位置方面遇到了问题。

这是方法片段 -

procedure ReplaceLineBreaks;
var
  Save_Cursor: TCursor;
  aOldTextList: TStringList;
  aNewTextList: TStringList;
  i, : Integer;
begin
  inherited;
  aOldTextList := TStringList.Create;
  aNewTextList := TStringList.Create;
  try
    Save_Cursor := Screen.Cursor;
    aOldTextList.text := dbmemDisciplineSummary.text;
    for i := 0 to aOldTextList.Count - 1 do begin
      if (i = 0) and( Trim(aOldTextList[i]) <> '') then
        aNewTextList.Add(aOldCN_TextList[i])
      else if not ((i<>0) and (Trim(aOldTextList[i - 1]) = '') and (Trim(aOldTextList[i]) = '')) then
        aNewTextList.Add(aOldTextList[i]);
    end;
    cdsPTClinicalNotesCNTEXT.AsString := aNewTextList.Text;
  finally
    Screen.Cursor := Save_Cursor;
    FreeAndNil(aOldTextList);
    FreeAndNil(aNewTextList);
  end;
end;

但它没有将光标设置回同一位置......有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您需要使用备忘录的SelStartSelLength属性,而不是Screen.Cursor属性:

procedure ReplaceLineBreaks;
var
  Save_SelStart, Save_SelLength: Integer;
  aOldTextList: TStringList;
  aNewTextList: TStringList;
  i, : Integer;
begin
  inherited;
  aOldTextList := TStringList.Create;
  try
    aNewTextList := TStringList.Create;
    try
      aOldTextList.Text := dbmemDisciplineSummary.Text;
      for i := 0 to aOldTextList.Count - 1 do begin
        if (i = 0) and( Trim(aOldTextList[i]) <> '') then
          aNewTextList.Add(aOldCN_TextList[i])
        else if not ((i<>0) and (Trim(aOldTextList[i - 1]) = '') and (Trim(aOldTextList[i]) = '')) then
          aNewTextList.Add(aOldTextList[i]);
      end;
      Save_SelStart := dbmemDisciplineSummary.SelStart;
      Save_SelLength := dbmemDisciplineSummary.SelLength;
      try
        cdsPTClinicalNotesCNTEXT.AsString := aNewTextList.Text;
      finally
        dbmemDisciplineSummary.SelStart := Save_SelStart;
        dbmemDisciplineSummary.SelLength := Save_SelLength;
      end;
    finally
      FreeAndNil(aNewTextList);
    end;
  finally
    FreeAndNil(aOldTextList);
  end;
end;