2016-05-16 67 views
1

我有一個名爲dbmemSummary的DBMemo和一個方法ReplaceLineBreaks,它將從數據庫備註中刪除多餘的換行符。我面臨着設置光標位置的問題。如何在替換換行符後在Delphi中保持遊標位置

這是方法片段 -

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

TCursor是鼠標指針,而不是備忘錄插入符號,其中一些人稱之爲光標。您需要查看便條位置的備忘SelStart屬性。你需要跟蹤,當你刪除線。就我個人而言,我將使用拆分備忘錄文本到selstart位置的左側和右側,並分別用StringReplace刪除行。 – MikeD

回答

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; 
+0

它不考慮已被刪除的行數。如果我們在那裏有額外的空白行空格,則執行此操作。它將被刪除,但光標不會指向用戶編輯文本的相同位置。而不是它捕獲光標的確切位置並將其放在同一位置,而不考慮多餘的行刪除。 –