public partial class SyncRichTextBox : UserControl
{
public int SelectionOffset
{
get { return richTextBoxEx1.SelectionCharOffset; }
set { SetOffset(value, false); }
}
public int SelectionOffsetPlus
{
set { SetOffset(value, true); }
}
void SetOffset(int offset, bool IsGrow)
{
int start = SelectionStart;
int len = SelectionLength;
RichTextBox r = new RichTextBox();
r.SelectedRtf = SelectedRtf;
for (int i = 0; i < len; i++)
{
r.Select(i, 1);
int oldOffset = r.SelectionCharOffset;
int newOffset = offset;
if (IsGrow)
newOffset = oldOffset + offset;
r.SelectionCharOffset = newOffset;
}
r.SelectAll();
string rtf = r.SelectedRtf;
r.Dispose();
if (SelectedRtf == rtf)
return;
string action = "";
if (IsGrow)
action = "PlusSelectionOffset";
else
action = "SetSelectionOffset";
if (SetSelectedRtf(rtf, action))
{
Undobuf buf = Data.GetUndobuf();
buf.newSelectionStart = start;
buf.newSelectionLength = len;
SelectionStart = start;
SelectionLength = len;
}
}
}