mercredi 11 mars 2015

Comment/Uncomment function in RichEditControl

i have created an app that will comment or uncomment highlighted line or current line and color the line w/ green. i have 2 buttons 1 for comment and one for uncomment. this is my code



DocumentRange lineRange = null;
bool isSelectionLocked = false;

void SetLineRangeFormatting(DocumentRange currentLineRange, System.Drawing.Color rangeColor)
{
CharacterProperties cp = richEditControl1.Document.BeginUpdateCharacters(currentLineRange);
cp.ForeColor = rangeColor;
richEditControl1.Document.EndUpdateCharacters(cp);
}

DocumentRange GetNewLineRange(DocumentPosition caret)
{
isSelectionLocked = true;
DocumentPosition currentPosition = richEditControl1.Document.CaretPosition;

StartOfLineCommand startOfLineCommand = new StartOfLineCommand(richEditControl1);
EndOfLineCommand endOfLineCommand = new EndOfLineCommand(richEditControl1);

startOfLineCommand.Execute();

int start = richEditControl1.Document.CaretPosition.ToInt();

endOfLineCommand.Execute();

int length = richEditControl1.Document.CaretPosition.ToInt() - start;

DocumentRange range = richEditControl1.Document.CreateRange(start, length);
DocumentRange range2 = richEditControl1.Document.CreateRange(start, length + 1);

string text = richEditControl1.Document.GetText(range2);

richEditControl1.Document.CaretPosition = currentPosition;
isSelectionLocked = false;

if (text.EndsWith(Environment.NewLine))
{
return range2;
}
else
{
return range;
}
}

private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (isSelectionLocked) return;
if (!lineRange.Contains(richEditControl1.Document.CaretPosition))
{
//SetLineRangeFormatting(lineRange, System.Drawing.Color.Transparent);
lineRange = GetNewLineRange(richEditControl1.Document.CaretPosition);
SetLineRangeFormatting(lineRange, System.Drawing.Color.Green);
}
}

private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{

}


but it doesn't work properly please help guys i am newbie, i research it in devexpress but i didn't find a way to do that thanks, the support there was not answering my question


Aucun commentaire:

Enregistrer un commentaire